1

I have been using PHPMailer for quite a while with no problems until recently when our company migrated from an on premiss Exchange server to Office365. We use PHPMailer because it allows us to send attachments. we actually received a response from Office365 that states "Your message contains invalid characters (bare line feed characters) which the email servers at NameOfCompanyServer.com don't Support".

So my question is, how can I remove the "bare line feed characters" when using PHPMailer? Or is there another third party PHP supported mailer that I can use to send attachments?

here is a sample of my PHP script

<?php
require_once("phpMailer\class.phpmailer.php");
require_once("phpMailer\class.smtp.php");
$mail             = new PHPMailer();
$body             = file_get_contents('OFFICE365.txt');
$body             = eregi_replace("[\]",'',$body);

$mail->IsMail();
$mail->SMTPDebug  = 3;
$mail->Host       = "companyOffice365-domainhere.mail.protection.outlook.com"; 
$mail->Port       = 25; 
$mail->Username   = "";
$mail->Password   = ""; 
$mail->SMTPSecure = 'tls';
$mail->SetFrom('someone@companyOffice365-domainhere.com', 'Mailer');
$mail->AddReplyTo("NoReply@companyOffice365-domainhere.com","");
$mail->Subject    = "Email Test via IsMail, using OFFICE365";
$mail->MsgHTML($body);
//Recipients
$address = "emailaddress@somewhere.com";

if(!$mail->Send()) {
  echo "Mailer Error: " . $mail->ErrorInfo;
} else {
  echo "Message sent!";
}
?>

hope someone can help here, thanks B

  • I think you need a look at the raw email to see if you can detect what bare line feed characters Office365 is talking about. Is it in your 'OFFICE365.txt' file, or is it in stuff that PHPMailer created? – KIKO Software Jul 05 '18 at 14:01
  • Could it be that the regular expression in `$body = eregi_replace("[\]",'',$body);` is invalid? I'm far from an expert on this, but https://regex101.com tells me: "[ Character class missing closing bracket". – KIKO Software Jul 05 '18 at 14:03
  • You're using an old version of PHPMailer. This issue was fixed in 6.0. You shouldn't be using any `ereg_*` functions - they are long obsolete. – Synchro Jul 05 '18 at 14:06
  • how can i check which version of PHPMailer i am using? – Bobby-Irvine Jul 06 '18 at 03:04
  • I checked the verision i am user and it is 5.2 – Bobby-Irvine Jul 06 '18 at 03:14
  • where do i find version 6.0 and how to install PHPMailer on Windows server 2008? – Bobby-Irvine Jul 06 '18 at 03:15

1 Answers1

0

Looking at the example, I don't see an attachment nor do I see bare line feeds in any of the header information (eg the from and to addresses) so the source of the bare line feeds would appear to be the file that you are importing as the body, the following php statement will replace the bare line feeds in $body with the standard compliant "end of line" sequence (carriage return followed by line feed)

$body=preg_replace('/(?<!\r)\n/',"\r\n",$body);

This regex uses a "look around" to find line feeds to replace. Look arounds come in four flavors, positive look ahead, negative look ahead, positive look back and negative look back. We are going to use negative look back in this case. That is we are looking for a line feed that is NOT preceded by a carriage return. The parenthesis declare a look around, more specifically a left parenthesis followed by a question mark (required) followed by the a look around expression followed by a closing right parenthesis. If the question mark is followed by a less than sign as it is in my solution, then it is a look back, otherwise it is a look ahead. Next comes either an exclamation mark making it a negative look around or an equals sign making it a positive look around. In this case we used the exclamation mark. Finally comes the character(s) to look for. In our case, a carriage return. So it says find every line feed that is not preceded by a carriage return and replace it with a carriage return line feed sequence.

Ted Cohen
  • 1,017
  • 10
  • 17