{"id":911,"date":"2010-08-17T15:58:46","date_gmt":"2010-08-17T14:58:46","guid":{"rendered":"http:\/\/www.simonbattersby.com\/blog\/?p=911"},"modified":"2010-10-29T13:31:04","modified_gmt":"2010-10-29T12:31:04","slug":"problems-with-from-and-bcc-headers-in-php-mail","status":"publish","type":"post","link":"https:\/\/www.simonbattersby.com\/blog\/2010\/08\/problems-with-from-and-bcc-headers-in-php-mail\/","title":{"rendered":"Problems with From and BCC headers in PHP mail()"},"content":{"rendered":"<p>Had a funny one this week. My wife discovered that nobody on her mailing list had received any emails since we swapped hosting packages back in May. Before the swap things were fine in a Windows server, now clearly not fine on Linux. On further investigation, I spotted that the From: and Bcc: headers weren&#8217;t being split out properly, and hence we were getting the Bcc was included in the From: field like this:<\/p>\r\n\r\n<p><strong>From:<\/strong>mail@mydomain.com Bcc: mail1@test.com, mail2@test.com etc etc<\/p>\r\n\r\n<p>This was my original code:<\/p>\r\n\r\n<pre>$headers  = 'MIME-Version: 1.0' . \"\\n\";\r\n$headers .= \"Content-type: text\/html; charset=utf-8\" . \"\\n\";\r\n$headers .= \"From: Sender &lt;sender@mydomain.co.uk&gt;\".\"\\n \";\r\n$headers .= \"Bcc:\".$bcc.\"\\n\";\r\n\r\nmail($to, $subject, $message,$headers)<\/pre>\r\n\r\n<p>Same result replacing <code>\\n<\/code> with <code>\\r\\n<\/code>. No joy even on <a href=\"http:\/\/www.codingforums.com\/showthread.php?t=202375\" title=\"Coding Forums\">Coding Forums<\/a>, but after a lot of Googling I found the solution, which was to replace the newline characters with the PHP constant <code>PHP_EOL<\/code>:<\/p>\r\n\r\n<pre>$headers  = 'MIME-Version: 1.0' . PHP_EOL;\r\n$headers .= 'Content-type: text\/html; charset=utf-8' . PHP_EOL;\r\n$headers .= \"From: Sender &lt;sender@mydomain.co.uk&gt;\".PHP_EOL;\r\n$headers .= \"Bcc:\".$bcc.PHP_EOL;\r\n\r\nmail($to, $subject, $message,$headers)<\/pre>\r\n\r\n<p>Glad to say this did the trick. PHP version 5.2.13 if anyone&#8217;s interested.<\/p>\r\n\r\n \r\n","protected":false},"excerpt":{"rendered":"<p>Had a funny one this week. My wife discovered that nobody on her mailing list had received any emails since we swapped hosting packages back in May. Before the swap things were fine in a Windows server, now clearly not fine on Linux. On further investigation, I spotted that the From: and Bcc: headers weren&#8217;t [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-911","post","type-post","status-publish","format-standard","hentry","category-web-design-and-build"],"_links":{"self":[{"href":"https:\/\/www.simonbattersby.com\/blog\/wp-json\/wp\/v2\/posts\/911","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.simonbattersby.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.simonbattersby.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.simonbattersby.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.simonbattersby.com\/blog\/wp-json\/wp\/v2\/comments?post=911"}],"version-history":[{"count":0,"href":"https:\/\/www.simonbattersby.com\/blog\/wp-json\/wp\/v2\/posts\/911\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.simonbattersby.com\/blog\/wp-json\/wp\/v2\/media?parent=911"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.simonbattersby.com\/blog\/wp-json\/wp\/v2\/categories?post=911"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.simonbattersby.com\/blog\/wp-json\/wp\/v2\/tags?post=911"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}