HTML Emails Sending in Plain Text? (PHP) - php

Okay okay, I know there are a million of these threads, but I did my best to look through them and could not find one that actually solved the problem for me. Any help is greatly appreciated, as depending on the browser it either hides the whole thing or shows it in plain text.
Here's what I'm using:
$headers = 'From: coupons#madisoncoupons.com\r\n';
$headers .= 'Reply-To: coupons#madisoncoupons.com\r\n';
$headers .= 'MIME-Version: 1.0\r\n';
$headers .= 'Content-type: text/html; charset=ISO-8859-1';
mail($email,$subject,$content,$headers);
$content starts and ends with html and body tags, with a style tag in the beginning of the body with some CSS in it.

You'll want to use the correct quotes. "\r\n" and '\r\n' are two completely different things.
Read up on how PHP handles strings if you're rusty.

as #tadman said above, you have to modify your headers to the following
$headers = 'From: coupons#madisoncoupons.com' ."\r\n";
$headers .= 'Reply-To: coupons#madisoncoupons.com' . "\r\n";

Use this
// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
// More headers
$headers .= 'From: <info#example.com>' . "\r\n";
mail($email,$subject,$content,$headers);

Related

HTML attributes missing in sending email

Sending mail inside wordpress admin. Headers:
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=UTF-8\r\n";
With From/Reply-to headers its didnt work;
In the <a> tag inside the letter all attrs( href, style ) is gone.
Message i get from $_REQUEST, textarea tag where href display correct.
IF i use a constant string as argument for mail, links work properly. Please help.
solved by myself.
textarea send text with escape symbols ( \" instead " ). After removing its work
Please compare second line .
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
Try replacing your second line with:
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
This is taken from PHP.net.

php mail() - "From" header interrupts sending

I got a code like this:
$headers = '';
//$headers .= 'Reply-To: "site\'s name" <newsletter#example.com>'."\r\n";
//$headers .= 'From: "site\'s name" <newsletter#example.com>'."\r\n";
$headers .= 'MIME-Version: 1.0'."\r\n";
$headers .= 'Content-type: text/html; charset=utf-8'."\r\n";
mail($_POST['email'], stripslashes($result['title']), $message, $headers);
As long as those two lines are commented out, it sends emails correctly. Otherwise - it just doesn't. Without giving any error or even notice. It worked (uncommented code) until yesterday and now it stopped. Any ideas, why would it happen? I cannot use Swift or anything like that, because it's not my project.
Edit: incorrect quoting, but that wasn't the problem.
Edit: damn, it was just server admins, they blocked all mails with headers from non-existing accounts. I just had to create such mailbox, and i started working back again...
You have an error on site's name try with correct quoting
$headers .= 'Reply-To: "site\'s name" <newsletter#example.com>'."\r\n";
$headers .= 'From: "site\'s name" <newsletter#example.com>'."\r\n";
You can add header as the code below. Remove unwanted single quotes and double quotes. Just simple write as below.
$headers = "From: site name <newsletter#example.com> \r\n";
$headers .= "Reply-To: site\'s name <newsletter#example.com> \r\n";
$headers .= "MIME-Version: 1.0\r\n";

PHP mail headers is not working properly

I'm sending mails using the PHP mail() function. The mail headers is not working properly.
$charset = mb_detect_encoding($message);
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'From: '.$from . "\r\n";
$headers .= 'Content-type: text/html; charset='.$charset . "\r\n";
$headers .= 'Reply-To: '.$from . "\r\n";
$headers .= 'X-Mailer: php';
In the above code, the only first line is parsed and the later 4 lines are showing in the message body. "From" was not set.
$charset = mb_detect_encoding($message);
$headers = 'From: '.$from . "\r\n";
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset='.$charset . "\r\n";
$headers .= 'Reply-To: '.$from . "\r\n";
$headers .= 'X-Mailer: php';
In the above code, "From" and "MIME" lines are parsed correctly, but the later 3 lines are showing in the message body.
GMail is receiving it correctly.
Have you tried to use \n only instead of \r\n?
http://php.net/manual/en/function.mail.php
Note:
If messages are not received, try using a LF (\n) only. Some Unix mail transfer agents (most notably » qmail) replace LF by CRLF automatically (which leads to doubling CR if CRLF is used). This should be a last resort, as it does not comply with » RFC 2822.
You may use the code below to easy change of end of line in email:
$EEOL = "\n";
$headers = 'From: '.$from . $EEOL;
$headers .= 'MIME-Version: 1.0' . $EEOL;
You should consider using an email class library instead of the regular mail() function in php.
I personally like the free SwiftMailer because of its simplicity and great functions for attachments etc.

send html email using php deliver message as html code

iam sending html message contains table
when i recive message on gmail,hotmail,yahoo account it works fine but when receiving on other client
ie Microsoft outlook it resolve it as html code !!
here is my email headers
'MIME-Version: 1.0' . "\r\n" .
'Content-Type: text/html;charset= UTF-8' . "\r\n" .
'From: me <me#client.com>'
I Always use this function ,and it helps
function sendHTMLemail($HTML,$from,$to,$subject,$fromname)
{
$headers = "From: ".$fromname." <".$from.">\r\n";
$headers.= "Reply-To: ".$fromname." <".$from.">\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$boundary = uniqid("HTMLEMAIL");
// First we be nice and send a non-html version of our email
$headers .= "Content-Type: multipart/alternative;".
"boundary = $boundary\r\n\r\n";
$headers .= "This is a MIME encoded message.\r\n\r\n";
$headers .= "--$boundary\r\n".
"Content-Type: text/plain; charset=ISO-8859-1\r\n".
"Content-Transfer-Encoding: base64\r\n\r\n";
$headers .= chunk_split(base64_encode(strip_tags($HTML)));
// Now we attach the HTML version
$headers .= "--$boundary\r\n".
"Content-Type: text/html; charset=ISO-8859-1\r\n".
"Content-Transfer-Encoding: base64\r\n\r\n";
$headers .= chunk_split(base64_encode($HTML));
// And then send the email ....
mail($to,$subject,"",$headers);
}
I know this isn't an answer for your question, but I suggest using a mailing library, that will allow you to send mails much more easily, and supports features such as attachments, authentication, etc.
I recommend SwiftMailer which works great, is simple and well documented.

sending only a text email instead of html

I have this code at the top of my email php file which sends out emails
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "From: $from <$from>" . "\r\n";
$headers .= 'Cc: matt.e#posnation.com' . "\r\n";
$headers .= 'Bcc: posnation#gmail.com' . "\r\n";
I need to change this to just a plain text email ...what do i change. I was thinking the second line text/html to text/text but i wasnt sure
You want to change the "text/html" to "text/plain".
You can use text/plain
But if you enter html content the tags and all will be shown on the client though ...
But i'm sure there's an alternate way to support both text and html ...
Are you using the built in mail function or an external mail app like phpMailer?
You don't need to change anything.
Technically it should be "text/plain", but if you want, you can just as easily send an "HTML" email with no HTML tags and it will work just fine.

Categories