The online racing simulator
[OT] PHP Mail() Function
(14 posts, started )
[OT] PHP Mail() Function
Got a strange thing happening and I can't understand why... Using the example in the PHP manual I've created a function to send HTML mail.


<?php 
function email($to$bcc$subject$messageGET){
// message
$message '
<html>
<head>
  <title>'
.$subject.'</title>
</head>
<body>
'
.$messageGET.'
</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: $to' "\r\n";
$headers .= 'From: Aberdeen Excise License Holders Association <[email protected]>' "\r\n";

if(
$bcc !== ""){
$headers .= 'Bcc: $bcc' "\r\n";
}

// Mail it
return(mail($to$subject$message$headers));
?>

Attached in the image is the message I get, the mail send fine but it makes a copy on the server and places this message on the screen.

" /home/dave/dead.letter... Saved message in /home/dave/dead.letter"

Any help appreciated, been playing with it for a while now trying to get it to stop spitting that message out to no avail.

Keiran
Attached images
arggh!.jpg
Usually you'll get this is the server isn't configured properly. Usually it's sendmail (there could be other MTAs which I've not used that produce it, but I'm not aware of which, if any, that do) which produces these files, and it will produce them when it doesn't know how to deliver your email.
You need \n instead of \r\n on Linux.
Quote from the_angry_angel :Usually you'll get this is the server isn't configured properly. Usually it's sendmail (there could be other MTAs which I've not used that produce it, but I'm not aware of which, if any, that do) which produces these files, and it will produce them when it doesn't know how to deliver your email.

So will I have to change values in the php config to sort this?

Currently
sendmail_from = no value
and there is a sendmail_path

It's weird as it only does this with the headers set for HTML emails, if I do a plain text email it doesn't give me this message or save a copy on the server.

It does send the emails fine, just want to get rid of the message as it doesn't look very good for joe public to see.

Cheers T-RonX for the pointer .
If it's your box take a look in /var/log/mail.log to see whats happening in more detail. The fact that mail() works with plain text means that it should be ok, which implies either you've got sendmail rejecting emails which contain html, or something is going arye. The dead.letter file should also contain more information as to whats going on.
Something I have just noticed;
// Additional headers
$headers .= 'To: $to' . "\r\n";

For obvious reasons that's not going to do what you think and should be
$headers .= 'To: '.$to."\r\n";

and may well be what's causing the problem. It shouldn't override the rcpt to though.. Right now it's the only issue I can see with the code, so I'd fall back to believing something funky is happening with the MTA, so I'd check the logs if that doesnt pan out..

Edit: T-RonX, I've only just noticed your post. Whilst you would be correct in saying that usually textfiles are \n (LF) only under unix-like OS', the SMTP specs dictate that \r\n (CRLF) should be used.

<?php 
function email($to$bcc$subject$messageGET){
    
# To Header
    
$headers "To: {$to}\r\n";
    
$headers .= "From: Aberdeen Excise License Holders Association <[email protected]>\r\n";
    
# Message
    
$message  "<html>\r\n";
    
$message .= "    <head>\r\n";
    
$message .= "        <title>{$subject}</title>\r\n";
    
$message .= "    </head>\r\n";
    
$message .= "    <body>\r\n";
    
$message .= "        {$messageGET}\r\n";
    
$message .= "    </body>\r\n";
    
$message .= "</html>\r\n";
    
# Additional Headers
    
$headers .= "MIME-Version: 1.0\r\n";
    
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
    
# Additional Headers : Add BCC Header?
    
if ($bcc !== '')
        
$headers .= "Bcc: {$bcc}\r\n";
    
# Mail It
    
return mail($to$subject$message$headers);
}
?>

Quote from the_angry_angel :
Edit: T-RonX, I've only just noticed your post. Whilst you would be correct in saying that usually textfiles are \n (LF) only under unix-like OS', the SMTP specs dictate that \r\n (CRLF) should be used.

Hmm ok, changing to \n solved some very strange problems on my Linux web server once. The headers were in the subject line and the body was completely corrupted.
Quote from Dygear :

<?php 
function email($to$bcc$subject$messageGET){
    
# To Header
    
$headers "To: {$to}\r\n";
    
$headers .= "From: Aberdeen Excise License Holders Association <[email protected]>\r\n";
    
# Message
    
$message  "<html>\r\n";
    
$message .= "    <head>\r\n";
    
$message .= "        <title>{$subject}</title>\r\n";
    
$message .= "    </head>\r\n";
    
$message .= "    <body>\r\n";
    
$message .= "        {$messageGET}\r\n";
    
$message .= "    </body>\r\n";
    
$message .= "</html>\r\n";
    
# Additional Headers
    
$headers .= "MIME-Version: 1.0\r\n";
    
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
    
# Additional Headers : Add BCC Header?
    
if ($bcc !== '')
        
$headers .= "Bcc: {$bcc}\r\n";
    
# Mail It
    
return mail($to$subject$message$headers);
}
?>


That sorted it, thanks Dygear
to be totally anal, i trim the $headers variable at the end, to remove any linebreaks at the end of the headers. I think it doesn't matter much with HTML mails, but with plain text, the ending headers linebreak turns into a leading linebreak at the start of the mailbody. At least on our server.
Quote from keiran :That sorted it, thanks Dygear

I must be having a thick day, I've been staring at this and only just realised that it was down to quotations
Quote from keiran :That sorted it, thanks Dygear

Great, I did not test the code, so I am glad it worked out for you. Like angle said down the bottom, it can be hard to realize the little issues with the script, PHP is so very fickle when it comes to it's quotes and how it handles variables in strings. I really can't recommend the method of putting your variables in the string via a complex variable string. - Fou ... 'Complex (curly) syntax'. But I recommend reading the whole page.

Quote from Victor :With plain text, the ending headers linebreak turns into a leading linebreak at the start of the mailbody. At least on our server.

Great point, but I wonder if that is a standard for your version of PHP, or is it PHP as a whole?

Quote from the_angry_angel :I must be having a thick day, I've been staring at this and only just realised that it was down to quotations

As they say, it's always the little things.
Quote from Dygear :eat point, but I wonder if that is a standard for your version of PHP, or is it PHP as a whole?

right, i just tested it on my windows box at home, using my ISP mailserver - it didn't happen there.
I always use the latest Release version of PHP.
So maybe it's MTA dependant, rather than PHP related. PHP would just forward the mail data as-is to the MTA who would then decide what to do about trailing header linebreaks.
We use Exim btw.

Soooo like i said, kinda anal all this
Quote from Dygear :As they say, it's always the little things.

It's interesting how the MTA dealt with the incorrect To and Bcc headers tbh. My boxes almost exclusively use exim 4 and the broken code behaves properly, where as the one I have running (an old) version of sendmail doesn't play nicely. You have to love tech sometimes

[OT] PHP Mail() Function
(14 posts, started )
FGED GREDG RDFGDR GSFDG