PHP (sendmail / PHPMailer / ezcMailComposer)發送郵件


一. 使用 PHP 內置的 mail() 函數

1. Windows 下

環境:WampServer2.5(Windows 10,Apache 2.4.9,MySQL 5.6.17,PHP 5.5.12)

① 在 Windows 下使用 PHP 內置的 mail() 函數發送郵件,需要先安裝 sendmail(下載地址:http://glob.com.au/sendmail/

把下載下來的 sendmail.zip 解壓到自定義的目錄(我這里是 D:\wamp\bin)

 

② 配置 php.ini 文件(通過 phpinfo 確定 ph.ini 文件真實路徑)

郵件服務器以騰訊郵箱為例,php.ini 文件主要配置

SMTP = smtp.qq.com

smtp_port = 25(郵件服務端口),

sendmail_path = "D:\wamp\bin\sendmail\sendmail.exe -t"

 

③ 配置 sndmail.ini

需要配置:

smtp_server=smtp.qq.com
smtp_port=25

,開啟 log 方便排錯,生成的log文件在sendmail根目錄
error_logfile=error.log 
debug_logfile=debug.log

auth_username=472323087@qq.com
auth_password=你的授權碼
,force_sender 要和auth_username一致
force_sender=472323087@qq.com

說明:

配置項中的 auth_password 不是郵箱的密碼, 而是騰訊郵箱的第三方客戶端授權碼,獲取授權碼的方式是:

登陸 mail.qq.com(472323087@qq.com),選擇“設置” -- “POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服務” -- “生成授權碼”

 

點擊“生成授權碼”,需要發送一條驗證信息,驗證通過之后得到授權碼

 

④ 測試發送郵件:

<?php

// 使用 PHP 內置的 mail() 函數

$to = '472323087@qq.com';
$subject = 'Hello World!';
$body = 'Welcome to China!';
mail($to, $subject, $body);

 

收到郵件:

  

2. Linux 下

環境:LNMP(CentOS 6.6 ,Nginx 1.8.0,MySQL 5.6.23,PHP 5.6.9)

  

① 安裝 sendmail

# yum install sendmail

 

② 啟動 sendmail

# /etc/rc.d/init.d/sendmail start

 

③ 此時可以直接通過 mail 命令來給指定郵箱發送郵件:

[root@localhost ~]# echo 'this is a mail test'|mail -s text dee1566@126.com

這里先用 126 郵箱舉例,騰訊郵箱由於騰訊郵件服務器的限制,不做設置很容造成拒收,后面再說。

  

打開郵件:

  

 

④ 使用 PHP 的 mail() 函數發送郵件

需要修改 php.ini 

smtp_port = 25
sendmail_path = /usr/sbin/sendmail -t

SMTP 不用設置

 

mail.php

<?php

header('Content-type:text/html;charset=utf-8');

// 使用 PHP 內置的 mail() 函數

$to = 'dee1566@126.com';
$subject = 'Hello World!';
$body = 'Welcome to China!';
if(mail($to, $subject, $body)) {
	echo '發送成功';
} else {
	echo '發送失敗';
}

收到郵件:

  

打開郵件:

  

說明:

mail("接受方email", "郵件主題", "正文內容", headers, "from:發送方email");

要修改發件人,可以添加第四個參數

<?php

header('Content-type:text/html;charset=utf-8');

// 使用 PHP 內置的 mail() 函數

$to = (isset($_GET['type']) && $_GET['type'] == 'qq') ? '472323087@qq.com' : 'dee1566@126.com';
$subject = 'Hello World!';
$body = 'Welcome to China!';

$headers   = array();
$headers[] = "MIME-Version: 1.0";
$headers[] = "Content-type: text/plain; charset=iso-8859-1";
$headers[] = "From: dee <472323087@qq.com>";

$from = '472323087@qq.com';

if(mail($to, $subject, $body, implode("\r\n", $headers), $from)) {
	echo '發送成功';
} else {
	echo '發送失敗';
}

此時收到的郵件:

  

打開郵件:

  

 

⑤ 如果接收方 email 是騰訊郵箱的話,很容易顯示發送成功但是實際上根本就沒有發送成功,通過查看日志

[root@localhost sbin]# tail -f /var/spool/mail/root

可能會出現

<<< 550 Mail content denied. http://service.mail.qq.com/cgi-bin/help?subtype=1&&id=20022&&no=1000726
554 5.0.0 Service unavailable

550 Mail content denied 出錯原因:該郵件內容涉嫌大量群發,並且被多數用戶投訴為垃圾郵件

 

編輯  /etc/mail.rc

[root@localhost ~]# vim /etc/mail.rc

 

添加:

set from=472323087\@qq.com  smtp=smtp.qq.com           //郵件來自  
set smtp-auth-user=472323087\@qq.com smtp-auth-password=你的授權碼 smtp-auth=login       //登錄qq SMTP服務器的用戶名和密碼 

此時直接使用 mail 命令發送郵件:

[root@localhost ~]# echo 'this is a mail test'|mail -s title 472323087@qq.com

但還是不能使用 PHP 的 mail 函數給騰訊郵箱發送郵件,查了很多資料,還是沒能解決。

 

總結:

在 Windows 下使用 sendmail 結合 mail() 函數能很容易地給騰訊郵箱發郵件,在 Linux 下騰訊郵箱幾乎一律拒收,連垃圾箱都進不了,其他的比如 126 郵箱就沒有問題,可能還是和主機設置有關。

最終代碼:

<?php

header('Content-type:text/html;charset=utf-8');

// 使用 PHP 內置的 mail() 函數

$to = 'dee1566@126.com';
$subject = 'Hello World!';
$body = 'Welcome to China!';

$headers   = array();
$headers[] = "MIME-Version: 1.0";
$headers[] = "Content-type: text/plain; charset=iso-8859-1";
$headers[] = "From: dee <472323087@qq.com>"; //決定郵件的發件人顯示

$from = '472323087@qq.com';

if(mail($to, $subject, $body, implode("\r\n", $headers), $from)) {
	echo '發送成功';
} else {
	echo '發送失敗';
}

  

  

二. 使用 Zeatcomponent 的 ezcMailComposer 類

文檔地址:http://ezcomponents.org/docs/tutorials/Mail 

下載地址:http://ezcomponents.org/download

下載后解壓壓縮包

 

Zetacomponent ezcMailComposer 類可以與 SMTP 服務器直接通信:

<?php

header('Content-type:text/html;charset=utf-8');

// 使用 Zeccomponent 的 ezcMailComposer 類
// http://ezcomponents.org/docs/tutorials/Mail

require_once 'ezcomponents/Mail/docs/tutorial/tutorial_autoload.php';

$message = new ezcMailComposer();
$message->from = new ezcMailAddress('47232087', 'dee'); //發送郵箱是qq郵箱,例如472323087@qq.com,郵件很容易被拒,sendmail根目錄下error.log中錯誤記錄是:Error: content rejected.http://mail.qq.com/zh_CN/help/content/rejectedmail.html<EOL>
$message->addTo(new ezcMailAddress('472323087@qq.com', 'emperor'));
$message->subject = 'php sendmail';
$body = 'this is a test mail';
$message->plainText = $body;
$message->build();

$host = 'smtp.qq.com';
$username = '472323087@qq.com';
$password = 'niwogqkejpnzbibh';
$port = '25';

$smtpOptions = new ezcMailSmtpTransportOptions();
$smtpOptions->preferredAuthMethod = ezcMailSmtpTransport::AUTH_LOGIN;

$sender = new ezcMailMtaTransport($host, $username, $password, $port, $smtpOptions);
try {
	$sender->send($message);
	echo '發生成功';
} catch(ezcMailTransportException $e) {
	echo $e->getMessage();
}

收到郵件:

  

打開郵件:

 

同樣在 Linux 下同樣會遇到騰訊郵箱直接拒收的問題。

 

三. 使用 PHPMailer 類

PHPMailer 版本 5.2.13

下載地址:https://github.com/Synchro/PHPMailer

Windows 下調試代碼:

<?php

header("content-type:text/html;charset=utf-8"); 

require 'PHPMailer/class.smtp.php';
require 'PHPMailer/class.phpmailer.php';

try { 
	$mail = new PHPMailer(true); 
	$mail->IsSMTP(); 
	$mail->CharSet='UTF-8'; //設置郵件的字符編碼,這很重要,不然中文亂碼 
	$mail->SMTPAuth = true; //開啟認證 
	$mail->Port = 25; 
	$mail->Host = "smtp.qq.cn"; 
	$mail->Username = "472323087@qq.com"; 
	$mail->Password = "你的授權碼"; 
	$mail->IsSendmail(); //windows下開啟;linux下如果沒有sendmail組件就注釋掉,否則出現“Could not execute: /usr/sbin/sendmail”的錯誤提示 
	$mail->AddReplyTo("472323087@qq.com","dee");//回復地址 
	$mail->From = "472323087@qq.com"; 
	$mail->FromName = "472323087@qq.com"; 
	$to = "472323087@qq.com"; 
	$mail->AddAddress($to); 
	$mail->Subject = "phpmailer測試標題"; 
	$mail->Body = "<h1>phpmail演示</h1>這是emperor對phpmailer的測試內容"; 
	$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; //當郵件不支持html時備用顯示,可以省略 
	$mail->WordWrap = 80; // 設置每行字符串的	長度 
	//$mail->AddAttachment("d:/test.jpg"); //可以添加附件 
	$mail->IsHTML(true); 
	$mail->Send(); 
	echo '郵件已發送'; 
} catch (phpmailerException $e) { 
	echo "郵件發送失敗:".$e->errorMessage(); 
} 

  

Linux 下修改 php.ini,注釋 

sendmail_path = /usr/sbin/sendmail -t -i

把程序中的

$mail->IsSendmail();

也注釋,可以完成包括對騰訊郵箱的郵件任務。

注意,郵件服務器盡量不要選騰訊郵箱。

 

參考:

PHP 在windows下配置sendmail,通過 mail() 函數發送郵件

Linux系統PHP使用sendmail發送郵件

linux用mail往qq郵箱發郵件

Sendmail.mc 配置文件詳解

PHPMailer使用教程(PHPMailer發送郵件實例分析)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM