7

I m using phpmailer for sending emails in my website. My code works fine but sometimes in email message body contains exclamation mark at random places. My code is as follows:

$mail->SetFrom(FROM_EMAIL,FROM_NAME); //emailid of sender(admin)                
$mail->Subject = 'Subject here.'; //subject of email
$mail->AddAddress(Address here); //emailid of user(recipient)
$content = 'some html code here';

$mail->MsgHTML($content); //this is body of email
$mail->Send();

This works fine. But can't find why exclamation comes sometimes. Thanks in advance...

CodeWarrior
  • 763
  • 3
  • 14
  • 33

4 Answers4

9

I think it's because the email messages can't have more than 998 characters on one line.

Try adding,

$mail->WordWrap = 50;
Muthu Kumaran
  • 17,682
  • 5
  • 47
  • 70
  • 1
    check this out, http://www.tinyfrogsoftware.com/the-story-of-the-rogue-exclamation-mark-or-how-i-lost-4-hours-of-my-life/ Hope it helps. – Muthu Kumaran Oct 11 '12 at 13:49
  • 3
    or try adding `$content = wordwrap($content, 50);` after `$content = 'some html code here';` – Muthu Kumaran Oct 11 '12 at 13:51
  • Could you explain why this is the case or a reference to why it can't handle more than 998 characters? – Stefan Dunn Sep 10 '15 at 10:03
  • @StefanDunn The 998 character limit is due to limitations in many implementations which send, receive, or store Internet Message Format messages that simply cannot handle more than 998 characters on a line. For more details [RFC 2822](http://tools.ietf.org/html/rfc2822#section-2.1.1) – Muthu Kumaran Sep 17 '15 at 06:27
  • Tinyfrogsoftware link was dead. Here's a live archive version - https://web.archive.org/web/20100424013747/http://www.tinyfrogsoftware.com/the-story-of-the-rogue-exclamation-mark-or-how-i-lost-4-hours-of-my-life/ – Merlyn Morgan-Graham Jul 08 '17 at 08:04
4

I know this is late but there is an alternate solution that worked for me:

Use this line to encode your entire message using base64:

$message = chunk_split(base64_encode($message));

Then, append this your header:

$headers .= "Content-Transfer-Encoding: base64\r\n\r\n";

That will tell the mail client that your message is base64 encoded.

karancan
  • 2,152
  • 4
  • 23
  • 35
2

if you are using PHPmailer then only one line of code should help:

$mail = new PHPMailer();
$mail->Encoding = 'base64';

this will do Content-Transfer-Encoding: base64 and chunk_split(base64_encode($message)) internally.

phvish
  • 149
  • 1
  • 8
0

I had this problem also, after long searching I have found that you should wordwrap your HTML

$emailContent = '<p>some large html</p>';
$mail->msgHTML(wordwrap($emailContent, 50));
webmaster
  • 1,960
  • 24
  • 29