Reputation: 13330
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.
// 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 <[email protected]>' . "\r\n";
Is there anything else I should add?
Upvotes: 34
Views: 99856
Reputation: 2446
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 <[email protected]>',
'Cc' => 'testsite <[email protected]>',
'X-Sender' => 'testsite <[email protected]>',
'X-Mailer' => 'PHP/' . phpversion(),
'X-Priority' => '1',
'Return-Path' => '[email protected]',
'MIME-Version' => '1.0',
'Content-Type' => 'text/html; charset=iso-8859-1'
];
mail('[email protected]', 'My subject', 'My message', $headers);
Upvotes: 9
Reputation: 1294
$headers = "From: testsite <[email protected]>\n";
$headers .= "Cc: testsite <[email protected]>\n";
$headers .= "X-Sender: testsite <[email protected]>\n";
$headers .= 'X-Mailer: PHP/' . phpversion();
$headers .= "X-Priority: 1\n"; // Urgent message!
$headers .= "Return-Path: [email protected]\n"; // Return path for errors
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=iso-8859-1\n";
Upvotes: 53
Reputation: 33710
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.
Upvotes: 1
Reputation: 1858
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 [email protected] and adding a reply-to
, being the person who sent the mail using my system.
Upvotes: 1
Reputation: 13407
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
Upvotes: 1
Reputation: 70001
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.
Upvotes: 1
Reputation: 1461
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.
Upvotes: 4