溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

PHP如何借助phpmailer發(fā)送郵件

發(fā)布時(shí)間:2021-06-30 16:24:47 來(lái)源:億速云 閱讀:98 作者:chen 欄目:開(kāi)發(fā)技術(shù)

這篇文章主要講解了“PHP如何借助phpmailer發(fā)送郵件”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來(lái)研究和學(xué)習(xí)“PHP如何借助phpmailer發(fā)送郵件”吧!

本地沒(méi)有發(fā)郵件的服務(wù)器,借助現(xiàn)成的SMTP服務(wù)器發(fā)送郵件是個(gè)不錯(cuò)的選擇,這里使用到的工具是phpmailer ( Version 5.2.0),SMTP服務(wù)器就選gmail和163。

1. 使用gmail發(fā)送的腳本

include("class.phpmailer.php");
include("class.smtp.php");
//獲取一個(gè)外部文件的內(nèi)容
$mail       = new PHPMailer();
$body       = file_get_contents('contents.html');
$body       = eregi_replace("[\]",'',$body);
//設(shè)置smtp參數(shù)
$mail->IsSMTP();
$mail->SMTPAuth  = true;
$mail->SMTPKeepAlive = true;
$mail->SMTPSecure = "ssl";
$mail->Host    = "smtp.gmail.com";
$mail->Port    = 465;
//填寫(xiě)你的gmail賬號(hào)和密碼
$mail->Username  = "yourname@gmail.com";
$mail->Password  = "password";
//設(shè)置發(fā)送方,最好不要偽造地址
$mail->From    = "yourname@gmail.com";
$mail->FromName  = "Webmaster";
$mail->Subject  = "This is the subject";
$mail->AltBody  = $body;
$mail->WordWrap  = 50; // set word wrap
$mail->MsgHTML($body);
//設(shè)置回復(fù)地址
$mail->AddReplyTo("yourname@gmail.com","Webmaster");
//添加附件,此處附件與腳本位于相同目錄下
//否則填寫(xiě)完整路徑
$mail->AddAttachment("attachment.jpg");
$mail->AddAttachment("attachment.zip");
//設(shè)置郵件接收方的郵箱和姓名
$mail->AddAddress("toname@gmail.com","FirstName LastName");
//使用HTML格式發(fā)送郵件
$mail->IsHTML(true);
//通過(guò)Send方法發(fā)送郵件
//根據(jù)發(fā)送結(jié)果做相應(yīng)處理
if(!$mail->Send()) {
 echo "Mailer Error: " . $mail->ErrorInfo;
} else {
 echo "Message has been sent";
}

2.使用163發(fā)送郵件的腳本

只需要更改SMTP配置和賬戶密碼即可,SMTP配置如下

//設(shè)置smtp參數(shù)
//注意這里不需要ssl協(xié)議
$mail->IsSMTP();
$mail->SMTPAuth  = true;
$mail->SMTPKeepAlive = true;
$mail->Host    = "smtp.163.com";
$mail->Port    = 25;

在本地wampserver環(huán)境下測(cè)試通過(guò),需要開(kāi)啟php_openssl 擴(kuò)展。

感謝各位的閱讀,以上就是“PHP如何借助phpmailer發(fā)送郵件”的內(nèi)容了,經(jīng)過(guò)本文的學(xué)習(xí)后,相信大家對(duì)PHP如何借助phpmailer發(fā)送郵件這一問(wèn)題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是億速云,小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI