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 = 'uuuuho@gmail.com'; // SMTP username
$mail->Password = 'ycuaixmxabtiuhmx'; // SMTP password
//Recipients
$mail->setFrom('xxxxxxxx@gmail.com', '有乎');
$mail->Subject = "来自有乎的验证码(The Verifycode From HuiFeng)"; //标题
$mail->Body = "您的验证码为:".$ret."<br>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;
}
}