Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am having a problem sending emails when I add header information. However when I just remove the header parameter it works. What is wrong? Is it the code? Or some setting I need to change on the web server admin panel to say "Allow-Headers" or something? I am trying to send to hotmail in case this has any relavance in determining the problem. Any help would be greatly appreciated. Thanks.

Below Doesn't Send Email:

<?php

    $to      = '[email protected]';
    $subject = 'the subject';
    $message = 'hello';
    $headers = 'From: [email protected]';

    mail($to, $subject, $message, $headers);

?>

Below Sends Email:

<?php

    $to = '[email protected]';
    $subject = 'the subject';
    $message = 'hello';
    $headers = 'From: [email protected]';

    mail($to, $subject, $message);

?>
share|improve this question
Try sending to Gmail and look it up in Spam. Hotmail has a tendency to discard emails that do not respect the RFC standards. – tntu Jun 16 at 15:32
Try adding \n in the end of $headers, but it's just a guess. – Petr R. Jun 16 at 15:32
@PetrR. Actually he should add \r\n but the server should also add it if it's not present. – tntu Jun 16 at 15:33
Adding \r\n to both subject and headers should theoretically work. However, you should consider adding a From:... in there (as part of your headers). Most email services/clients will most probably consider it as Spam, without it. – Fred Jun 16 at 15:37
1  
seriously, you should use a library like phpMailer for sending mail via php. The built-in mail() function has a lot of shortcomings. – Spudley Jun 16 at 15:42
show 2 more comments

1 Answer

I use these headers in my php mailing function and it works well. Note: I also use a third party mail-routing service to avoid having my mails marked as coming from a spammy IP. You might want to look into that also.

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

I also use the optional fifth parameter to mail() to set the envelope address, e.g.:

$parameters = '-f '.$from.'@foo.net';

so the final call is:

mail($to, $subject, $message, $headers, $parameters);
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.