Outlook receives PHP HTML e-mail as code? - php

I made an HTML e-mail send with PHP, but my client receives this as pure code. Here is the PHP code to send the mail:
$subject = 'HTML e-mail test';
$message = '<html>
<body>
<h1>TEST</h1>
</body>
</html>';
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= "To: <".$to.">\r\n";
$headers .= "From: <".$email.">\r\n";
$mailb = mail($to, $subject, $message, $headers);
It works fine for me, but they receive it as:
Content-type: text/html; charset=iso-8859-1
To: <email#email.com>
From: <email#email.com>
<html>
<body>
<h1>TEST</h1>
</body>
</html>
Is there something wrong with my headers? Or is it their Outlook?
How can I fix this problem?
Thanks in advance!

I see:
Content-typetext/html; charset=iso-8859-1
where I should see:
Content-type: text/html; charset=iso-8859-1
Is that a cut/paste error? In your code, or in your result?
Note that you probably want to include both text/plain and text/html types in a multipart message, since HTML-only mail often gets high spam scores (using SpamAssassin, SpamBouncer, etc).
UPDATE #1:
It appears that the \r\n is being interpreted as two newlines instead of one. Different platforms may have different bugs in their implementation of SMTP. It's possible that you can get away with changing your line endings to just \n. If that works with your system, don't rely on it, as it may not work on a different system.
Also, consider switching to a different method for sending mail. phpMailer and SwiftMailer are both recommended over PHP's internal mail() function.
UPDATE #2:
Building on Incognito's suggestion, here's code you might use to organize your headers better, as well as create a plaintext part:
filter_var($to, FILTER_VALIDATE_EMAIL) or die("Invalid To address");
filter_var($email, FILTER_VALIDATE_EMAIL) or die("Invalid From address");
$subject = 'HTML e-mail test';
$messagetext = 'TEST in TEXT';
$messagehtml = '<html>
<body>
<h1>TEST</h1>
<p>in HTML</p>
</body>
</html>';
// We don't need real randomness here, it's just a MIME boundary.
$boundary="_boundary_" . str_shuffle(md5(time()));
// An array of headers. Note that it's up to YOU to insure they are correct.
// Personally, I don't care whether they're a string or an imploded array,
// as long as you do input validation.
$headers=array(
'From: <' . $email . '>',
'MIME-Version: 1.0',
'Content-type: multipart/alternative; boundary="' . $boundary . '"',
);
// Each MIME section fits in $sectionfmt.
$sectionfmt = "--" . $boundary . "\r\n"
. "Content-type: text/%s; charset=iso-8859-1\r\n"
. "Content-Transfer-Encoding: quoted-printable\r\n\r\n"
. "%s\n";
$body = "This is a multipart message.\r\n\r\n"
. sprintf($sectionfmt, "html", $messagehtml)
. sprintf($sectionfmt, "plain", $messagetext)
. "--" . $boundary . "--\r\n";
$mailb = mail($to, $subject, $body, implode("\r\n", $headers));
I'm not saying this is The Right Way to do this by any means, but if the strategy appeals to you, and you think you can maintain code like this after not having looked at it for 6 or 12 months (i.e. it makes sense at first glance), then feel free to use or adapt it.
Disclaimer: this is untested, and sometimes I make typos ... just to keep people on their toes. :)

One good way to be sure is to change your headers to that of this:
$to = 'bob#example.com';
$subject = 'Website Change Reqest';
$headers = "From: " . strip_tags($_POST['req-email']) . "\r\n";
$headers .= "Reply-To: ". strip_tags($_POST['req-email']) . "\r\n";
$headers .= "CC: susan#example.com\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
Also, double check the rest of your code by comparing to this:
http://css-tricks.com/sending-nice-html-email-with-php/

I would recommend not using PHP's mail() function. Yes, it can send an email, but for anything with any kind of complexity, it just makes things difficult.
I'd suggest using a class such as phpMailer, which will give you a lot more flexibility. It makes it much easier to do things like sending HTML emails, adding attachments, and using alternative MTAs.

I've had this issue myself, it was fixed by using \n instead of \r\n.
I only use \r\n for the last line in the header.

The problem is that some antivirus email scanners treat anything following the mime header as body text.
The fix is to make you mime header the last one.
I just had the same problem with one of my clients and it had me whacked for a while.

This is a problem with Microsoft Outlook (Windows).
The solution is to remove the "\r" at the end of the headers and just use "\n".

Related

Wierd characters at the end of any email: PHP MAIL

Well I am using the PHP MAIL function and for some reason every email it sends has a weird;

This is at the end of any email as I said, I am not quite sure why this is happening.
$from = 'From: support#phycraft.co.uk';
$to = $user_email; // Send email to our user
$subject = 'PhyCraft Support Ticket :: Closed :: ' . $t_subject; // Give the email a subject
$message = '
Hello '. $username.'.
Your support ticket '.$t_subject.' has been closed due to being inactive for 48 hours.
If you still need help with the ticket please reopen the ticket by replying to it.
~PhyCraft
';
$headers = 'From:support#phycraft.co.uk' . "\r\n"; // Set from headers
mail($to, $subject, $message, $from); // Send our email
I can't see what in the code woud make that appear to be honest.
Most issues with php's mail() function are problems with the MTA and not with PHP itself. I've never heard of this before making it even more likely it's a MTA issue.
You've not provided any useful information beyond the PHP code. What OS is this on (mail() on MSWindows is very different from everyhere else). Do you control the server? What MTA is configured? Have you tried sending an email from the command line?
The extra stuff at the end looks like HTML - is this byte for byte what's in the email or what you see in your mail client?
BTW it's not a good idea to explicitly put "\r\n" at the end of your headers - but you seem to have forgotten to add them as a parameter. Also, your missing a space between "From:" and the email address.
Can you try it with following $headers ( only \n ).
$headers = "MIME-Version: 1.0\n";
$headers .= "Content-Type: text/plain; charset = \"ISO-8859-1\";\n";
$headers .= "Content-Transfer-Encoding: 8bit\n";
$headers .= "From: support#phycraft.co.uk\n";
$headers .= "\n";
mail($to, $subject, $message, $headers);
and 2. try it without
$headers .= "Content-Transfer-Encoding: 8bit\n";

PHP Mail Send when using a tag

PHP mail sending problem when using a tag, it doesn't come to new line.
HERE is my code having same problem
$subject = 'Watch Out Our Colorful Web Design Presentation';
$headers = "From: " . $email . " \r\n";
$headers .= "Reply-To: ". $email . "\r\n";
$headers .= "Bcc: test#test.com\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$message = "Watch Out Our Colorful Web Design Presentation.\r\n";
$message .= "<a href='http://www.stackoverflow.com'>CLICK HERE</a>\r\n";
mail($to, $subject, $message, $headers);
Mail send successfully but having problem in \r\n. It doesn't take new line. I tried br tag too. But it goes in junk mail.
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
You're sending a HTML email. This means that you should be using HTML instead of newlines. To avoid having your emails placed in the junk folder, you should read some of the many Stackoverflow topics on the subject.
You have to use <br> tag for new line.
You can't use \n for new line for html printing.
https://bugs.php.net/bug.php?id=9542
As others have said, you need to be using HTML line breaks <br /> if you are sending an e-mail with Content-Type: text/html. The newlines/carriage returns will be interpreted in the source of the message as line breaks but they will probably not be rendered as HTML.
When sending e-mail from PHP I would always suggest using an e-mail class rather than PHP's native mail functions.
I tend to use SwiftMailer. It has the advantage that all mail headers are sanitized and escaped to avoid header injection exploits that could potentially fire out spam through your script. Also it's easier to use a variety of e-mail transports. There's also a great decorator plugin which can send thousands of messages with customised strings, useful for doing things like "Dear {first_name} {surname}" or customised unsubscribe/tracking links.
Here's some sample code for SwiftMailer just in case you are interested...
// START SWIFTMAILER
require_once($swiftmailer_path);
$swift_transport = Swift_SendmailTransport::newInstance($sendmail_cmd);
$swift = Swift_Mailer::newInstance($swift_transport);
$swift_msg = Swift_Message::newInstance($swift_transport);
$swift_msg->setMaxLineLength(150);
$swift_msg->setFrom( array('NoReply#domain.com' => 'MyWebsiteName'));
$swift_msg->addTo($user);
$swift_msg->setSubject($subject);
$swift_msg->setBody($msg_html, 'text/html');
$swift_msg->addPart($msg_txt, 'text/plain');
// SEND E-MAIL
if ($swift_result = $swift->send($swift_msg)) {
// SENT SUCCESSFULLY
} else {
// ERROR - E-MAIL NOT SENT
}

Outlook 2007 receives html mail as source with headers, others MUAs work fine. Why?

I have a couple of simple forms that send an html-only email. Most clients (Gmail, Lotus Notes 8, hotmail/live, windows live mail, outlook express) receive the emails just fine, but Outlook 2007 does not.
The code looks like this:
$data="
<html>
<body>
<strong><u>$sub</u></strong><br><br>
<strong>Name:</strong> {$_POST["nombre"]}<br><br>
<strong>Phone:</strong>{$_POST["telefono"]}<br><br>
<strong>Email:</strong> {$_POST["email"]}<br><br>
<strong>Subject:</strong> {$_POST["asunto"]}<br><br>
<strong>Question:</strong> {$_POST["consulta"]}</strong>
</body>
</html>";
$header = "Reply-To: $from\r\n";
$header .= "From: \"".$_POST["nombre"]."\" <$from>\r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-Type: text/html; charset=iso-8859-1\r\n";
$enviado = mail($destino,$sub,$data,$header);
($from is the only part of the message validated)
The message received by the customer looks like this:
Content-Type: text/html; charset=iso-8859-1
From: Consulta de "Boss" <boss#myfirm.com>
Reply-To: boss#myfirm.com
X-Mailer: PHP/
<strong><u>Solicitud de envĂ­o de recetas -
CLIENT</u></strong><br><br><strong>Nombre y Apellido:</strong>
Boss<br><br><strong>Email:</strong>
boss#myfirm.com<br><br><br>
Any ideas?
Have you tried sending multipart email, when doing this we never had issues with outlook 2k3 and 2k7 (excepts poor HTML rendering)
<?php
$header = "From: Sender <sen...#domain.org>\r\n";
$header .= "Reply-to: Sender <blabla...#domain.net>\r\n";
$header .= "X-Mailer: Our Php\r\n";
$boundary = "==String_Boundary_x" .md5(time()). "x\r\n";
$boundary2 = "==String_Boundary2_y" .md5(time()). "y\r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-Type: multipart/related;\r\n";
$header .= " type="multipart/alternative";\r\n";
$header .= " boundary="$boundary";\r\n";
$message = "If you read this, your email client doesn't support MIME\r\n";
$message .= "--$boundary\r\n";
$message .= "Content-Type: multipart/alternative;\r\n";
$message .= " boundary="$boundary2";\r\n";
$message .= "--$boundary2\r\n";
$message .= "Content-Type: text/plain; charset=\"iso-8859-1\"\r\n";
$message .= "Content-Transfer-Encoding: 7bit\r\n";
$message .= "Alternative message in plain text format.\r\n";
$message .= "--$boundary2\r\n";
$message .= "Content-Type: text/html; charset=\"iso-8859-1\"\r\n";
$message .= "Content-Transfer-Encoding: 7bit\r\n";
$message .= "<html><body><p>HTML formatted message</p></body></html>";
You can replace boundaries with whatever you want, but they must be unique.
For more powerful and flexible email sending in php I suggest to use SwiftMailer
EDIT : as Outlook 2007 has a really dumb HTML renderer, you can also try fixing your markup, there is a </font> never opened in your example, dunno if it's the real mail or a typo in question.
I had a very similar problem, try removing the /r from your returns and use only /n. Outlook andd hotmail have trouble with /r/n.
I confirm the experience with Exchange janmoesen has shared.
Had to change CRLF in headers to just LF, then it started working.
(Thank you Microsoft, once again, for having me work 40% time extra.
Also a real thank you to janmoesen for pointing this! This search is over.)
I encountered the same problem with Outlook 2007.
The answer is simple : replace \r\n by \n
I have had trouble with Exchange (not just Outlook) and CRLF in headers with similar results. Basically, we were sending mails (using PHP on Debian with Postfix) with CRLF-separated headers, which would get mangled in Exchange upon arrival. When I changed those \r\n to simply \n, the problem was gone. ("RFCs be damned!", eh?)
YMMV, obviously, since it is not clear whether your other mail clients connect to the same server as Outlook, or use separate servers altogether.
If the message is in HTML you need to identify it as such:
$header .= "Content-Type: text/html; charset=iso-8859-1\r\n";
I have always had better luck with MIME encoded HTML mails. Even if there is just one part, I typically use multipart/mixed and explicitly set the content type (text/html). I'm not very familiar with PHP, but the PEAR::Mail_Mime package looks like a candidate.
http://pear.php.net/package/Mail_Mime
Outlook shouldn't have a problem handling it. (emphisis on shouldn't).
There are lots of problems with HTML email in Outlook 2007.
http://www.molly.com/2007/01/18/what-happened-with-html-and-css-in-outlook-2007/
http://fixoutlook.org/
http://www.developertutorials.com/tutorials/html/microsoft-complicates-html-emails-with-outlook-2007-070130/page1.html
and so on.

PHP line breaks don't seem to work

I have the following code that sends a message using the mail() function. Everything works fine except the line breaks at the end of each line don't seem to work. As you can see I am using " \r\n " hoping that this will give me a line break but it doesn't I have added <br> to get the break, but I would rather not use that in case someone doesn't have an HTML email client.
<?php
$to = 'user#example.com'; // Was a valid e-Mail address, see comment below
$name = $_REQUEST['name'] ;
$email = $_REQUEST['email'] ;
$subject = $_REQUEST['subject'] ;
$message = $_REQUEST['message'] ;
$content = 'Name: '.$name."\r\n";
$content .= 'Email: '.$email."\r\n";
$content .= 'Subject: '.$subject."\r\n";
$content .= 'Message: '.$message."\r\n";
$headers = 'MIME-Version: 1.0' ."\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' ."\r\n";
// Additional headers
$headers .= 'To: iVEC Help <help#ivec.com>'. "\r\n";
$headers .= 'From: '.$name.' <'.$email.'>' . "\r\n";
mail( $to, $subject, $content, $headers);
?>
<p> You sent it ... good work buddy </p>
<p> <?php '$name' ?> </p>
You're sending it as HTML - change the content type to text/plain and it should work.
The problem is that you say, in your headers, that the mail is an HTML email:
$headers .= 'Content-type: text/html; charset=iso-8859-1' ."\r\n";
If you want to ensure compability with both HTML and text clients, consider using the Multipart content type. This can be achieved in many different ways, but one simple way is to use a library that can do this for you. For example, you can use PEAR:Mail_Mime
I'm not an expert in this area, but my guess would be that you're setting the content type to text/html, which implies html-rendering (which means line breaks are translated to a space). If you're not using any HTML-elements (which appear not to), try setting the content type to text/plain.
As mentioned before, either set it to text/plain or add a HTML break for line breaks:
$content = 'Name: '.$name."</br>\r\n";

Is my email header correct?

Is the following "From" header incorect?
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Additional headers
$headers .= 'From: Mail Master <mail#mailmaster.com>' . "\r\n";
if(sendEmailNow($email, $subject, $body, $headers)){
I get the error from my mail server. It says "Mail from error: Syntax error".
Thanks all for any help.
Update
I have stripped down the SendEmailNow function to the below and I get the same error:
//send an email
function sendEmailNow($email, $subject, $body, $headers){
if (mail($email, $subject, $body, $headers)) {
##check email
##code to say email sent - compare with the number registered
return true;
}
else {
##code to report an error
return false;
}
}
Update 2
Problem solved. I am running this on a windows machine using PHP 5. Like the correct answer chosen and the comments have said. Some mail servers have trouble understanding what I had previously. But what worked for me was this:
$headers .= 'From: mail#mailmaster.com' . "\r\n";
A Google search for the error message suggests that some SMTP servers fail to parse your syntax for the From header. Can you try the following syntax to rule out this possibility?
From: mail#mailmaster.com
Unless the body is empty, you may need an additional CRLF to terminate the headers. Without knowing the API I can't say much more.
Not sure if this'll help, but here's what the output headers look like in my Python app in pure-text:
Content-Type: multipart/alternative;
boundary="10.254.26.130.1.1364.1241389770.060.1"
From: User1 <user1#domain1.com>
To: User2 <user2#domain1.com>,User3 <user3#domain.com>
Subject: Actual subject
MIME-Version: 1.0
--10.254.26.130.1.1364.1241389770.060.1
Content-Type: text/plain;
charset="us-ascii"
Content-Transfer-Encoding: quoted-printable
actual text content starting here...
Again, without knowing all the content/headers it's hard to say, but I'd guess either a) You've left out the trailing CRLFs before the content, or b) one of the earlier headers missing its CRLF.
Apologies if that takes you in a wildly wrong direction. :)
From: "User1" <user1#domain1.com>
The From header requires quotes for the name part.
If you set the "$userEmail" variable in your form, you can have it from them and a reply to. The $email_id is your email it is sent to.
$to=$email_id;
$headers = "From: " . strip_tags($userEmail) . "\r\n";
$headers .= "Reply-To: ". strip_tags($userEmail) . "\r\n";

Categories