Complete mail header

2019-01-10 16:13发布

问题:

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?

回答1:

    $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";


回答2:

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.



回答3:

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.



回答4:

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.



回答5:

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: http://tools.ietf.org/html/rfc5322#section-3.6

For MIME, look here: http://tools.ietf.org/html/rfc2045#section-3



回答6:

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.



标签: php email