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?
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.
Answer: Enter the line of text but don't press Return/Enter. Press Shift and Alt and Enter.
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"; } ?>
You must be using double quotes, and you use \r\n to make a new line.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With