Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHPMailer Ignoring \r\n

I am currently having an issue with emails received that were sent from PHPMailer using plain/text emails. I am currently returning the email message from a database by fetching the row and saving into a variable $message. The message while in the database is formatted as such:

This is some email information. \r\n This is some more email information.

The email received is showing the message with the \r\n rather than returning a new line.

My PHPMailer Code looks similar to the following:

$subject = $row['subject'];
$message = $row['message'];

// PHP Mailer
$mail = new PHPMailer();
$mail->From = "[email protected]";
$mail->FromName = "MyWebsite.org";
$mail->AddAddress('[email protected]');
$mail->ContentType = 'text/plain';
$mail->IsHTML(false);
$mail->Subject = $subject;
$mail->Body = $message;
$mail->AltBody = $message;
if(!$email->Send()){
    echo "Mailer Error: " . $mail->ErrorInfo;
} 

My question: How can I get \r\n to format properly using PHPMailer? Is this a PHPMailer setting or am I doing something wrong within my code?

like image 207
Ryan Avatar asked Apr 03 '14 13:04

Ryan


People also ask

Why is my PHP mail function not working?

Make sure the localhost mail server is configuredWithout one, PHP cannot send mail by default. You can overcome this by installing a basic mail server. For Windows you can use the free Mercury Mail. You can also use SMTP to send your emails.

How do you enter a new line in an email?

Answer: Enter the line of text but don't press Return/Enter. Press Shift and Alt and Enter.

How do you check PHP mail () is working?

to check if it is sending mail as intended; <? php $email = "[email protected]"; $subject = "Email Test"; $message = "this is a mail testing email function on server"; $sendMail = mail($email, $subject, $message); if($sendMail) { echo "Email Sent Successfully"; } else { echo "Mail Failed"; } ?>

How do you insert a line break in plain text email?

You must be using double quotes, and you use \r\n to make a new line.


2 Answers

Ryan mentioned this in the comments for the other answer, but I struggled with this and felt this deserved it's own answer.

Removing the AltBody has worked. It looks like this.

$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

This seems to be the case whether $mail->isHTML() is set to true or false. I'm not sure why this seems to be the case. Perhaps the client me and Ryan were sending to accept only plain text emails, and AltBody not only sends plain text, but actually remove line breaks. If you need to send an HTML message that won't show tags if the client can't accept tags, then I suspect you'll either have to live with it or dig into the PHPMailer code

like image 97
Goose Avatar answered Oct 04 '22 14:10

Goose


This might just be a simple case of using single quotes instead of double quotes when originally preparing the message.

Using single quotes will result in \r\n being stored in the database as part of the string, and PHPMailer will then output \r\n in the email instead of an actual line break.

Using double quotes will fix the problem.

For example, $message = 'Line 1\r\nLine 2'; (single quotes) will be output as

Line 1\r\nLine 2

while $message = "Line 1\r\nLine 2"; (double quotes) will be output as

Line 1
Line 2

This works for me, even if using AltBody to send both HTML and Plain Text versions of an email.

like image 29
wicketyjarjar Avatar answered Oct 04 '22 16:10

wicketyjarjar