Вопрос

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 = "noreply@mywebsite.org";
$mail->FromName = "MyWebsite.org";
$mail->AddAddress('recipient@email.com');
$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?

Это было полезно?

Решение 4

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

Другие советы

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

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.

Simply make nl2br() on the string ... but out of phpmailer... and than write string in to 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 php mailer write just like this

$mail->Body = "$string";

And that it .. so simple...OK

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

This works for me!

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);

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top