tp5.0傳送郵件
阿新 • • 發佈:2018-12-07
1.從https://github.com/PHPMailer/PHPMailer網址下載PHPMailer類,將圖片中的檔案放到tp5.0裡面的extend檔案裡面的PHPMailer裡面的PHPMailer
2.在Index模板中的Index控制器中寫下面的方法
<?php
namespace app\index\controller;
//引用相關的檔案
use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
class Index
{
public function mail(){
$toemail = '[email protected]';//定義收件人的郵箱
$mail = new PHPMailer();
$mail->isSMTP();// 使用SMTP服務
$mail->CharSet = "utf8";// 編碼格式為utf8,不設定編碼的話,中文會出現亂碼
$mail->Host = "smtp.qq.com";// 傳送方的SMTP伺服器地址
$mail->SMTPAuth = true ;// 是否使用身份驗證
$mail->Username = "[email protected]";// 傳送方的QQ郵箱使用者名稱,就是自己的郵箱名
$mail->Password = "eygtxqhwzmuecbba";// 傳送方的郵箱密碼,不是登入密碼,是qq的第三方授權登入碼,要自己去開啟,在郵箱的設定->賬戶->POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服務 裡面
$mail->SMTPSecure = "ssl";// 使用ssl協議方式,
$mail-> Port = 465;// QQ郵箱的ssl協議方式埠號是465/587
$mail->setFrom("[email protected]","發件人");// 設定發件人資訊,如郵件格式說明中的發件人,
$mail->addAddress($toemail,'天秀');// 設定收件人資訊,如郵件格式說明中的收件人
$mail->addReplyTo("[email protected]","Reply");// 設定回覆人資訊,指的是收件人收到郵件後,如果要回復,回覆郵件將傳送到的郵箱地址
//$mail->addCC("[email protected]");// 設定郵件抄送人,可以只寫地址,上述的設定也可以只寫地址(這個人也能收到郵件)
//$mail->addBCC("[email protected]");// 設定祕密抄送人(這個人也能收到郵件)
//$mail->addAttachment("bug0.jpg");// 新增附件
$mail->Subject = "郵件標題";// 郵件標題
$mail->Body = "我就是隨便玩玩!";// 郵件正文
//$mail->AltBody = "This is the plain text純文字";// 這個是設定純文字方式顯示的正文內容,如果不支援Html方式,就會用到這個,基本無用
if(!$mail->send()){// 傳送郵件
echo "Message could not be sent.";
echo "Mailer Error: ".$mail->ErrorInfo;// 輸出錯誤資訊
}else{
echo '傳送成功';
}
}
}
?>
注意:在呼叫方法之前在php.ini配置檔案裡面開啟extension=php_openssl.dll擴充套件