I have a website in which I send a confirmation mail as part of the registration process.
Some time ago, I had some troubles with the mails I sent since I used no headers (PHP mail function).
Once I put some headers, I've gotten more responses from users, but I suspect that not every message reaches its destination.
How can I be sure that the messages reach their destination?
Which are the headers that can be considered a 'must'?
This is the code of my SendMail function
mail($to,
$subject,
$message,
"MIME-Version: 1.0\n".
"Content-type: text/plain; charset=ISO-8859-1; format=flowder\n".
"Content-Transfer-Encoding: 8bit\n".
"Message-Id: <" . md5(uniqid(microtime())) . "@mysite.com>\n".
"Return-Path: <admin@mysite.com>\n".
"X-Mailer: PHP v".phpversion()."\n".
"From: admin@ mysite.com");
You should use external library for working with e-mails in php like PhpMailer , SwiftMailer or Zend_Mail . All your problems will go away.
The headers need a white space at the bottom to separate the header from main body. Tools like Spam Assassin will give you a big mark down for that.
Also you should use \\r\\n
as a line terminator instead of just \\n
Multiple extra headers should be separated with a CRLF (\\r\\n).
This is a working mail function I'm using for html mail and variable $return is defined to get error report from mail server in case of fail delivery.
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$headers .= 'From: <'.$from.'>' . "\r\n";
$return = '-f'.$from;
@mail($to, $subject, $msg, $headers, $return);
you can see more detail at here sugunan.com
The headers seems quite good to me. The only glitch I see is an extra whitespace in the From header.
I'm sure you already checked it, but just in case ...
"From: admin@ mysite.com");
should be (?)
"From: admin@mysite.com");
The headers look ok, except for the details pointed by @Eineki. Also if you are using Windows you need to send the $to param in the form "user@mail.com" and not "Username ", because it may cause trouble, due to the way the mail() function is implemented on windows platform, the "to" address may be parsed incorrectly.
您应该添加一个Date:
标头(它是RFC5322强制性的),如果没有给出,一些邮件客户端可能会假定 1970 年 1 月 1 日作为电子邮件日期(并且它会在所有其他旧邮件之间丢失)。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.