2

I won't lie. I don't really understand some of the code I have used in the header for this mail function. I have been trying to fix it myself and some of the code is copied from other forum posts etc.

The $email, $subject, and $msg variables are all fine, and emails were sending when I tested them earlier with just these 3 vars. Then I added a header for the "From" section and the sender name was fixed (but emails went into my junk folder - annoying).

Now I am trying to add some html tags to the $msg and have used the last 2 lines on my $header variable as per other forum posts, but this has just stopped emails from getting sent at all. Please advise me on how to fix the issue.

$headers = "From: website <donotreply@website.com>" . PHP_EOL .
"BCC: customer1@hotmail.com" . PHP_EOL . 
"MIME-Version: 1.0 \r\n" . PHP_EOL . 
"Content-Type: text/html; charset=UTF-8' \r\n";

$email = "SomeEmail@hotmail.com";
$subject = "Weekly Newsletter";
mail($email, $subject, $msg, $headers);

Thanks guys I comment section for reminding me to post the error. It says:

Warning: mail(): Multiple or malformed newlines found in additional_header in /path/publishnewsletter.php on line 45

Tunna182
  • 343
  • 3
  • 16

2 Answers2

4

"MIME-Version: 1.0 \r\n" . PHP_EOL . is too many newlines. Don’t use PHP_EOL at all; use \r\n, and only once.

You also have an extra single quote after charset.

$headers =
    "From: website <donotreply@website.com>\r\n" .
    "BCC: customer1@hotmail.com\r\n" . 
    "MIME-Version: 1.0\r\n" . 
    "Content-Type: text/html; charset=UTF-8";
Ry-
  • 218,210
  • 55
  • 464
  • 476
-1

Look at this it might help:

  • Sanitize your headers. No multiple newlines in additional_headers argument. These count as "multiple or malformed newlines": \r\r, \r\0, \r\n\r\n, \n\n, \n\0.
  • Use additional_headers for headers only. Email message (multipart or not, with ir without attachments, etc) belongs in message argument, not in headers.
  • And DO NOT use PHP_EOL

PHP Security Bug report: https://bugs.php.net/bug.php?id=68776
C Code diff how its fixed: http://git.php.net/?p=php-src.git;a=blobdiff;f=ext/standard/mail.c;h=448013a472a3466245e64b1cb37a9d1b0f7c007e;hp=1ebc8fecb7ef4c266a341cdc701f0686d6482242;hb=9d168b863e007c4e15ebe4d2eecabdf8b0582e30;hpb=eee8b6c33fc968ef8c496db8fb54e8c9d9d5a8f9

The Beast
  • 1
  • 1
  • 2
  • 24
  • This is the right answer, but also: A SINGLE \n IS SUFFICIENT. There is no need for any \r whatsoever. FWIW, the mail command also generates a newline if you just have the next line of text on a new line in PHP, literally type your headers out with a return between the lines and without any \n linebreaks at all. It makes it a lot more readable. – joshstrike Jan 03 '16 at 02:31
  • 2
    “Use additional_headers for headers only. Email message (multipart or not, with ir without attachments, etc) belongs in message argument, not in headers.” Is there an indication of this in the question…? – Ry- Jan 03 '16 at 02:31
  • sorry if i posted something worong @RyanO'Hara – The Beast Jan 03 '16 at 02:57
  • 1
    @joshstrike: https://php.net/mail: “Multiple extra headers **should be separated with a CRLF (\r\n)**. … If messages are not received, try using a LF (\n) only. … **This should be a last resort**, as it does not comply with » RFC 2822.” – Ry- Jan 03 '16 at 03:01