59

I'm trying to create a php script that will handle a mailing list for me using a mySQL database, and I have most of it in place. Unfortunately, I can't seem to get the headers to work right, and I'm not sure what the problem is.

$headers='From: noreply@rilburskryler.net \r\n';
$headers.='Reply-To: noreply@rilburskryler.net\r\n';
$headers.='X-Mailer: PHP/' . phpversion().'\r\n';
$headers.= 'MIME-Version: 1.0' . "\r\n";
$headers.= 'Content-type: text/html; charset=iso-8859-1 \r\n';
$headers.= "BCC: $emailList";

The result I'm getting on the recieving end is:

"noreply"@rilburskryler.net rnReply-To: noreply@rilburskryler.netrnX-Mailer: PHP/5.2.13rnMIME-Version: 1.0

RonLugge
  • 5,086
  • 5
  • 33
  • 61

3 Answers3

136

To have names, as opposed to email addresses shown, use the following:

"John Smith" <johnsemail@hisserver.com>

Easy.

Regarding the broken line breaks, that is because you are enclosing the text in apostrophes rather than quotation marks:

$headers = array(
  'From: "The Sending Name" <noreply@rilburskryler.net>' ,
  'Reply-To: "The Reply To Name" <noreply@rilburskryler.net>' ,
  'X-Mailer: PHP/' . phpversion() ,
  'MIME-Version: 1.0' ,
  'Content-type: text/html; charset=iso-8859-1' ,
  'BCC: ' . $emailList
);
$headers = implode( "\r\n" , $headers );
Luke Stevenson
  • 10,357
  • 2
  • 26
  • 41
  • 9
    The display name needs to be quoted when it contains a white space character. – Gumbo Sep 04 '10 at 21:45
  • 3
    @Gumbo: Just tested that. Worked without quotation marks. Not sure whether that is the standard, or just a very flexible/forgiving structure... – Luke Stevenson Sep 04 '10 at 22:00
  • I guess the latter; see [RFC 822](http://tools.ietf.org/html/rfc822#section-6.1). – Gumbo Sep 04 '10 at 22:23
  • I suspect quotes would be a good idea in general, though. Thanks for the full, descriptive answer. – RonLugge Sep 05 '10 at 02:02
  • This answer is a red herring, because `From: noreply@rilburskryler.net` is a valid header per the BNF in RFC 5322. See Section 3.4. I suspect Gumbo's answer on escaping is the real issue. – james.garriss Mar 26 '13 at 14:51
  • I find it useful to say, that quotation works with double quotes there. Single quotes didn't work for me. I received the email in gmail. – Benjamin Jesuiter Aug 30 '16 at 20:54
  • In situations like this, it is often easier and more robust to build up the headers in an array, then join them all together at the end with the appropriate `"\r\r"`. – Jason Oct 13 '16 at 10:06
  • @Jason: Agreed, that is how I do it normally. I just didn't want to deviate too far from the OP's code in my answer. – Luke Stevenson Oct 13 '16 at 11:59
  • The quotes around the "Display Name" are allowed, but not required (unless it contains an oddball character). From RFC2822: `mailbox = name-addr / addr-spec` and `name-addr = [display-name] angle-addr` and `display-name = phrase` and `phrase = 1*word` and `word = atom / quoted-string` and `atom = [CFWS] 1*atext [CFWS]` and finally `atext` is any one of most of the printable characters. – John Hascall Jan 09 '18 at 22:52
  • @JohnHascall fair point, but better to have an answer which can cater for those oddball characters should they occur, as well as vanilla characters, right? – Luke Stevenson Jan 09 '18 at 22:56
11

Within a single quoted string, only the escape sequences \' and \\ are replaced by ' and \ respectively. You need to use double quotes to have the escape sequences \r and \n to be replaces by the corresponding characters:

$headers = "From: noreply@rilburskryler.net \r\n";
$headers.= "Reply-To: noreply@rilburskryler.net\r\n";
$headers.= "X-Mailer: PHP/" . phpversion()."\r\n";
$headers.= "MIME-Version: 1.0" . "\r\n";
$headers.= "Content-type: text/html; charset=iso-8859-1 \r\n";
$headers.= "BCC: $emailList";

You could also use an array to collect the header fields and put them later together:

$headers = array(
    'From: noreply@rilburskryler.net',
    'Reply-To: noreply@rilburskryler.net',
    'X-Mailer: PHP/' . phpversion(),
    'MIME-Version: 1.0',
    'Content-type: text/html; charset=iso-8859-1',
    "BCC: $emailList"
);
$headers = implode("\r\n", $headers);
Gumbo
  • 643,351
  • 109
  • 780
  • 844
-1
    $to = 'SendersName@domain.com';
    $to .=', ' . $_POST['Femail'];
    $subject = 'Contact Us Form';

// message
$message ="<html>
<head>
<title>Email title</title>
</head>
<body>
<h3>important message follows</h3>
<div>
     you are being brought this email to be safe.
</div>
</body>
</html>";


    // To send HTML mail, the Content-type header must be set
    $headers  = 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
    // Additional headers
    $headers .= 'To: SendersEmailName <SendersEmailName@domain.com>' . "\r\n";
    $headers .= 'From: YourName <YourName@domain.com>' . "\r\n";
    $headers.='X-Mailer: PHP/' . phpversion()."\r\n";
    $headers.= "BCC: $emailList";


    mail($to, $subject, $message, $headers);
cmptrwhz
  • 190
  • 3
  • 14