31

I need a set of mail headers to attach to my mail() function in PHP. I send emails with HTML in them, and sometimes services like Yahoo Mail block them. Therefore I need to make sure that I am at least providing the right headers.

My code:

// 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 .= 'From: MyCompany <welcome@mycompany.com>' . "\r\n";  

Is there anything else I should add?

Community
  • 1
  • 1
johnnietheblack
  • 13,050
  • 28
  • 95
  • 133

7 Answers7

49
    $headers  = "From: testsite <mail@testsite.com>\n";
    $headers .= "Cc: testsite <mail@testsite.com>\n"; 
    $headers .= "X-Sender: testsite <mail@testsite.com>\n";
    $headers .= 'X-Mailer: PHP/' . phpversion();
    $headers .= "X-Priority: 1\n"; // Urgent message!
    $headers .= "Return-Path: mail@testsite.com\n"; // Return path for errors
    $headers .= "MIME-Version: 1.0\r\n";
    $headers .= "Content-Type: text/html; charset=iso-8859-1\n";
user816624
  • 15
  • 1
  • 6
Rohit Dubey
  • 1,234
  • 15
  • 15
  • 3
    the only answer here that gave me what I was looking for (and I presume, the original asker of the question) – Jimmery Sep 11 '15 at 10:59
  • The second line is missing a dot `$headers .=` – Onimusha Sep 18 '15 at 21:06
  • 4
    Good answer, but instead of `\n` you should use `PHP_EOL` in PHP. – UbiQue Sep 21 '16 at 10:46
  • 4
    Is it just me or is this answer missing a return after `phpversion()`? I'd rather write this as `$headers .= "X-Mailer: PHP/" . phpversion() . "\r\n"` – oelna Jan 16 '17 at 12:45
  • I prefer to use an array `$headers` instead of the `.=` format. By using `$headers[] = "..."` and then imploding the headers, you get cleaner code. (and don't miss the dots). – Sablefoste Sep 30 '17 at 15:34
  • 2
    Another useful config, to hide things like "from: John Doe via p3plkldnl1234.prod.phk3.somehostingserver.net, would be to use this mail( $to , $subject , $body, $headers, '-f john_doe@mydomain.com' ); – Nookeen Jan 17 '18 at 15:36
7

PHP 7.2+ solution

In current versions of PHP it is possible to pass an array of headers to mail() (as mentioned in the PHP docs), so the code could look a little cleaner. (Sablefoste mentioned this in their comment on the current top answer.)

In case anybody is interested, it could look like this:

$headers = [
    'From' => 'testsite <mail@testsite.com>',
    'Cc' => 'testsite <mail@testsite.com>',
    'X-Sender' => 'testsite <mail@testsite.com>',
    'X-Mailer' => 'PHP/' . phpversion(),
    'X-Priority' => '1',
    'Return-Path' => 'mail@testsite.com',
    'MIME-Version' => '1.0',
    'Content-Type' => 'text/html; charset=iso-8859-1'
];

mail('recipient@host.com', 'My subject', 'My message', $headers);
oelna
  • 2,210
  • 3
  • 22
  • 40
4

Most MUA's insert a lot of extra headers; however, here is sort of the bare minimum you can expect.

To: 
Subject:
Date: 
MIME-Version:
Content-type: 

If you using HTML, then you should probably be using multipart messages--but it's not strictly necessary.

Joseph Tary
  • 1,391
  • 1
  • 8
  • 5
1

The link below could be of some use defining the mandatory headers as:

  • Date: The date the message was originated/written.

  • From: The person "responsible" for the message.

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Malachi
  • 33,142
  • 18
  • 63
  • 96
1

Did the mail really come from 'mycompany.com'? I've had problems with some mail services blocking if it didn't really come from the SMTP server that the mail says it does.

A way around this, for me, was making the from to be automail@mydomainnaim.com and adding a reply-to, being the person who sent the mail using my system.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jasper
  • 1,818
  • 1
  • 13
  • 8
  • nah, that was just the header template i was using...i replaced the values:) – johnnietheblack Feb 19 '09 at 17:01
  • So the script / mailserver is the same as the domain you use in the 'from' ? – Jasper Feb 19 '09 at 17:20
  • the reply-to has an issue where that if the person sending the form is using a free email service and has numbers at the end of their username (niceperson123), it may get hit heavily with SpamAssassin points. It is high enough for it to be rejected or in the spam box. Currently, looking for a solution. Search Freemail_ReplyTo and FREEMAIL_FORGED_REPLYTO – WebDude0482 Mar 12 '21 at 00:06
1

When defining if a sender is a possible spammer, many services check if the domain of the sender looks like a dialup user.

Quote from Wikipedia:

One e-mail anti-spam technique: checking the domain names in the rDNS to see if they are likely from dialup users, dynamically assigned addresses, or other inexpensive internet services. Owners of such IP addresses typically assign them generic rDNS names such as "1-2-3-4-dynamic-ip.example.com." Since the vast majority, but by no means all, of e-mail that originates from these computers is spam, many spam filters refuse e-mail with such rDNS names.

GEOCHET
  • 21,119
  • 15
  • 74
  • 98
Ólafur Waage
  • 68,817
  • 22
  • 142
  • 198
0

The RFCs for both IMF and MIME define the minimal set of headers, so this would be a good place to start.

For IMF, look here: https://www.rfc-editor.org/rfc/rfc5322#section-3.6

For MIME, look here: https://www.rfc-editor.org/rfc/rfc2045#section-3

Community
  • 1
  • 1
james.garriss
  • 12,959
  • 7
  • 83
  • 96