1、引入PHPMailer包2、基于TP5的方法如下:
public function sendCode(){
$to = input('to');
$ret = rand(1000,9999);
session('vcode', $ret);
$mail = new PHPMailer(true);
try {
//Server settings
$mail->CharSet = 'UTF-8'; //設定郵件編碼,預設ISO-8859-1,如果發中文此項必須設定,否則亂碼
$mail->Encoding = "base64"; //編碼方式
$mail->SMTPDebug = 0; // SMTP調試功能 0=關閉 1 = 錯誤和消息 2 = 消息
$mail->isSMTP(); // Set mailer to use SMTP
$mail->SMTPAuth = true; // 啟用 SMTP 驗證功能
$mail->SMTPSecure = 'ssl'; // Enable TLS encryption, `ssl` also accepted
$mail->Host = 'ssl://smtp.gmail.com:465'; // Specify main and backup SMTP servers
$mail->Port = 25; // SMTP伺服器的端口号
$mail->Username = '[email protected]'; // SMTP username
$mail->Password = 'ycuaixmxabtiuhmx'; // SMTP password
//Recipients
$mail->setFrom('[email protected]', '有乎');
$mail->Subject = "來自有乎的驗證碼(The Verifycode From HuiFeng)"; //标題
$mail->Body = "您的驗證碼為:".$ret."
Your Verifycode Of UUUHO is:".$ret; //内容
$mail->IsHTML(true); //是否啟用html
$mail->AddAddress($to); //收件使用者
$mail->send();
return rand(1000,9999);
} catch (Exception $e) {
return $mail->ErrorInfo;
}
}