13

I'm getting exclamation points in random spots in the result of this PHP email function. I read that it's because my lines are too long or I have to encode the email in Base64 but I do not know how to do that.

This is what I have:

$to = "you@you.you";
$subject = "Pulling Hair Out";
$from = "me@me.me";
$headers = "From:" . $from;
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$headers .= "Content-Transfer-Encoding: 64bit\r\n";

mail($to,$subject,$message,$headers); 

How do I fix this so there's no random ! in the result? Thanks!

Tom
  • 917
  • 2
  • 12
  • 23

4 Answers4

15

As stated here: Exclamation Point in HTML Email

The issue is that your string is too long. Feed an HTML string longer than 78 characters to the mail function, and you will end up with a ! (bang) in your string.

This is due to line length limits in RFC2822 https://www.rfc-editor.org/rfc/rfc2822#section-2.1.1

Community
  • 1
  • 1
Lumberjack
  • 488
  • 4
  • 13
  • 3
    I've read this other question "Exclamation Point in HTML Email". I'm not sure exactly how to do this: "The resolution is to change it to base-64 encode the data or add \r\n on my long lines of html code." – Tom Aug 24 '13 at 18:58
  • 3
    @tomuky try `$base64_email_text = rtrim(chunk_split(base64_encode($email_text)));` Source: http://www.brightlemon.com/blog/unexpected-exclamation-marks-while-using-phps-mail-function-0 – sampoh Nov 19 '13 at 12:34
10

Try to use this piece of code:

$to = "you@you.you";
$subject = "Pulling Hair Out";
$from = "me@me.me";
$headers = "From:" . $from;
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$headers .= "Content-Transfer-Encoding: 64bit\r\n";

$finalMessage = wordwrap( $message, 75, "\n" );

mail($to,$subject,$finalMessage,$headers);

The problem is that one line should not be longer than 998 characters. (see also https://stackoverflow.com/a/12840338/2136148)

Community
  • 1
  • 1
knobli
  • 657
  • 1
  • 9
  • 18
  • 2
    This worked perfectly for me. We send large-ish emails to Outlook and Office 365 recipients and the wordwrap command got rid of all the exclamation marks perfectly, without affecting any of the HTML. Everything renders as expected. – Jeff Sereno Jul 18 '18 at 05:00
  • 1
    This line `$finalMessage = wordwrap( $message, 75, "\n" );` solved the problem. I'm sending emails to Gmail and others. Works like a charm. – Giancarlo Benítez May 27 '19 at 17:36
3

You are right, that is because your email is too long. Try replacing with this line in your mail header.

Content-Transfer-Encoding: quoted-printable
FatBat
  • 109
  • 1
  • 4
  • 1
    Not sure if you figured it out yet but I left out this code piece. So the header along with this should fix your problem. `$message = quoted_printable_encode($message)` http://php.net/manual/en/function.quoted-printable-encode.php – FatBat Sep 19 '13 at 17:37
  • I pissed around with numerous answers above and a couple work-arounds of my own, but this was definitely the easiest, quickest fix with no kickback on the output... Added this header: $headers .= "Content-Transfer-Encoding: quoted-printable"."\r\n"; adjusted my html string: $body = quoted_printable_encode($msgtosend); – Owen Parker Sep 22 '17 at 13:35
3

The answers here have the correct information regarding the line length, however none of them provided me with the sufficient code snippet to fix the issue. I looked around and found the best way to do this, here it is;

<?php
// send base64 encoded email to allow large strings that will not get broken up
// ==============================================
$eol = "\r\n";
// a random hash will be necessary to send mixed content
$separator = md5(time());

$headers  = "MIME-Version: 1.0".$eol;
$headers .= "From: Me <info@example.com>".$eol;
$headers .= "Content-Type: multipart/alternative; boundary=\"$separator\"".$eol;
$headers .= "--$separator".$eol;
$headers .= "Content-Type: text/html; charset=utf-8".$eol;
$headers .= "Content-Transfer-Encoding: base64".$eol.$eol;

// message body
$body = rtrim(chunk_split(base64_encode($html)));

mail($email, $subject, $body, $headers);
// ==============================================
Novocaine
  • 4,692
  • 4
  • 44
  • 66
  • The code almost works out. It has an error. The line `$headers .= "Content-Transfer-Encoding: base64".$eol.$eol;` is wrong, it should be `$headers .= "Content-Transfer-Encoding: base64".$eol` and it will work just fine – OfficeYA Jan 30 '17 at 16:52
  • Are you simply removing the second `$eol` in your example? – Novocaine Feb 07 '17 at 10:07