Ryan
Ryan

Reputation: 388

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?

Upvotes: 9

Views: 23695

Answers (6)

Miljan Marković
Miljan Marković

Reputation: 81

Simply make nl2br() on the string ... but out of phpmailer... and then write a string into the body" "; and no need to use preg_replace or something like that.

I will explain step-by-step

$string = nl2br($_POST["message"]);

after that in the body of the php mailer write just like this

$mail->Body = "$string";

And that is.. so simple...OK

Upvotes: 8

Dan
Dan

Reputation: 235

This is an old question but the solutions provided did not work for me. However the following did work so I thought it might be worth sharing.

$body = nl2br($body);

Upvotes: 2

Aleksandar Lazovic
Aleksandar Lazovic

Reputation: 51

$body = stripcslashes(isset($body) 
? 
preg_replace('#(\\\r|\\\r\\\n|\\\n)#', '<br/>', $body) 
: 
false);
$body = str_replace("<br/><br/>","<br/>",$body);

This works for me!

Upvotes: 4

wicketyjarjar
wicketyjarjar

Reputation: 1815

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.

Upvotes: 15

Goose
Goose

Reputation: 4821

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

Upvotes: 11

Emily Shepherd
Emily Shepherd

Reputation: 1369

It looks like you've saved \r\n into your database as text - PHP will not parse that. You need actually save a new line into your 'message' in the database

Upvotes: 6

Related Questions