tags:

views:

47

answers:

3

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: [email protected] \r\n';
$headers.='Reply-To: [email protected]\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: [email protected]: PHP/5.2.13rnMIME-Version: 1.0

+3  A: 

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

John Smith <[email protected]>

Easy.

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

$headers= "From: The Sending Name <[email protected]>\r\n";
$headers.= "Reply-To: The Reply To Name <[email protected]>\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";
Lucanos
The display name needs to be quoted when it contains a white space character.
Gumbo
@Gumbo: Just tested that. Worked without quotation marks. Not sure whether that is the standard, or just a very flexible/forgiving structure...
Lucanos
@Lucanos: I guess the latter; see [RFC 822](http://tools.ietf.org/html/rfc822#section-6.1).
Gumbo
I suspect quotes would be a good idea in general, though.Thanks for the full, descriptive answer.
RonLugge
A: 
    $to = '[email protected]';
    $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 <[email protected]>' . "\r\n";
    $headers .= 'From: YourName <[email protected]>' . "\r\n";
    $headers.='X-Mailer: PHP/' . phpversion()."\r\n";
    $headers.= "BCC: $emailList";


    mail($to, $subject, $message, $headers);
cmptrwhz
+1  A: 

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: [email protected] \r\n";
$headers.= "Reply-To: [email protected]\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: [email protected]',
    'Reply-To: [email protected]',
    'X-Mailer: PHP/' . phpversion(),
    'MIME-Version: 1.0',
    'Content-type: text/html; charset=iso-8859-1',
    "BCC: $emailList"
);
$headers = implode("\r\n", $headers);
Gumbo