这是我的内部电子邮件类,它使用PHP mail()来发送电子邮件:
class Email {
public $from = '';
public $from_name = '';
public $reply_to = '';
private $to = '';
private $subject = '';
private $message = '';
private $attachment = '';
private $attachment_filename = '';
public function __construct($to, $subject, $message, $attachment = '', $attachment_filename = '') {
$this -> to = $to;
$this -> subject = $subject;
$this -> message = $message;
$this -> attachment = $attachment;
$this -> attachment_filename = $attachment_filename;
$this -> from = SITE_EMAIL;
$this -> from_name = SITE_NAME_URL;
$this -> reply_to = SITE_EMAIL;
}
public function mail() {
if (!empty($this -> attachment)) {
$filename = empty($this -> attachment_filename) ? basename($this -> attachment) : $this -> attachment_filename ;
$path = dirname($this -> attachment);
$mailto = $this -> to;
$from_mail = $this -> from;
$from_name = $this -> from_name;
$replyto = $this -> reply_to;
$subject = $this -> subject;
$message = $this -> message;
$file = $path.'/'.$filename;
$file_size = filesize($file);
$handle = fopen($file, "r");
$content = fread($handle, $file_size);
fclose($handle);
$content = chunk_split(base64_encode($content));
$uid = md5(uniqid(time()));
$name = basename($file);
$header = "From: ".$from_name." <".$from_mail.">\r\n";
$header .= "Reply-To: ".$replyto."\r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
$header .= "This is a multi-part message in MIME format.\r\n";
$header .= "--".$uid."\r\n";
$header .= "Content-type:text/plain; charset=iso-8859-1\r\n";
$header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$header .= $message."\r\n\r\n";
$header .= "--".$uid."\r\n";
$header .= "Content-Type: application/octet-stream; name=\"".$filename."\"\r\n"; // use diff. tyoes here
$header .= "Content-Transfer-Encoding: base64\r\n";
$header .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
$header .= $content."\r\n\r\n";
$header .= "--".$uid."--";
if (mail($mailto, $subject, "", $header, '-f'.$this -> from)) {
return true;
} else {
return false;
}
} else {
$header = "From: ".($this -> from_name)." <".($this -> from).">\r\n";
$header .= "Reply-To: ".($this -> reply_to);
if (mail($this -> to, $this -> subject, $this -> message, $header, '-f'.$this -> from)) {
return true;
} else {
return false;
}
}
}
}
每次用户完成订单并通过PayPal支付IPN状态=“已完成”时,IPN会通知我的网站并触发此功能:
function emailBuyerAdminCompleted($ipn_data, $validItemsText, $password) {
$message = "Hi, {$ipn_data['first_name']},
PayPal transaction details
-------------------------------
Buyer name: {$ipn_data['first_name']} {$ipn_data['last_name']}
Transaction ID: {$ipn_data['txn_id']}
Payment gross: {$ipn_data['mc_gross']}
Payment currency: {$ipn_data['mc_currency']}
Payment date: {$ipn_data['payment_date']}
Number of items: {$ipn_data['num_cart_items']}
Product(s) purchased
-------------------------------
$validItemsText
Authentication credentials
-------------------------------
User Name: {$ipn_data['payer_email']}
Password: $password
Regards,
".SITE_NAME." Team
".SITE_URL."/";
$buyerEmail = new Email($ipn_data['payer_email'], 'Product(s) Download Credentials - $'.$ipn_data['mc_gross'].' - '.SITE_NAME_URL, $message);
$buyerEmail -> mail();
$adminEmail = new Email(SITE_EMAIL, 'Product(s) Download Credentials - $'.$ipn_data['mc_gross'].' - '.SITE_NAME_URL, $message);
$adminEmail -> mail();
}
您可以看到我正在向 SITE_EMAIL (info@example.com)发送2封同一邮件正文的电子邮件($ buyerEmail和$ adminEmail),这是我的电子邮件和 $ ipn_data ['payer_email'] 这是买家的PayPal电子邮件。
我的SITE_EMAIL(info@example.com)是在Google Apps上托管的电子邮件,它始终会收到此消息。当我点击邮件标题详细信息时,它正确邮寄并签名我的网站example.com:http://postimg.org/image/jaow7tpwx/
然而,大约30% - 50%的客户抱怨在付款后没有收到消息,我必须手动将相同的消息(我收到的)转发给他们。这令人沮丧,因为:
我试着问他们这件事。有些人说他们在垃圾邮件文件夹中找到了这封邮件(我认为邮件正确邮寄并且签名邮件永远不会在那里结束!)有些人说他们从未在任何地方找到它,即使是在付款数天之后。
知道这里有什么不对吗?
或者有没有比PHP mail()更好的方法?这会有更高的交付率?我最好使用第三方服务还是SMTP?
此处保证交付和不识别垃圾邮件的最佳做法是什么?
答案 0 :(得分:1)
使用PHP mail()
时遇到问题,而某些消息未送达。我猜测使用mail()
时标题并不完美。我发现问题的唯一方法是将PHP Pear Mail库安装到服务器上,并使用SMTP和PEAR发送电子邮件。
您需要包含其他标题,而不仅仅是以下内容。这只是一个如何使用PEAR和smtp发送电子邮件的简单示例。
require_once "Mail.php";
$from = "Your Name <your@email.com>";
$to = 'to@email.com';
$subject = "Some Subject";
$body = "Some Message";
$host = "smtp.yourhost.com";
$port = "25";
$headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject);
$smtp = Mail::factory('smtp',
array ('host' => $host,
'port' => $port,
'auth' => false));
$mail = $smtp->send($to, $headers, $body);
注意:您还可以通过定义用户名&amp;来进行身份验证的smtp。密码和设置auth => true