在config.php和function.php中的代码如下
config.php
'MAIL_HOST' =>'smtp.qq.com',//smtp服务器的名称 'MAIL_SMTP'=>TRUE, 'MAIL_SMTPAUTH' =>TRUE, //启用smtp认证 'MAIL_USERNAME' =>'8952272@qq.com',//你的邮箱名 'MAIL_FROM' =>'8952272@qq.com',//发件人地址 'MAIL_FROMNAME'=>'mybook',//发件人姓名 'MAIL_PASSWORD' =>'kurjfpidiorgcijh',//邮箱密码 'MAIL_CHARSET' =>'utf-8',//设置邮件编码 'MAIL_ISHTML' =>TRUE, // 是否HTML格式邮件 'MAIL_SECURE' =>'tls',//安全协议 function.php
function sendMail($to, $subject, $content) {
Vendor('PHPMailer.class#phpmailer');
Vendor('PHPMailer.class#smtp');
$mail = new PHPMailer();
// 装配邮件服务器
if (C('MAIL_SMTP')) {
$mail->IsSMTP();
}
$mail->Host = C('MAIL_HOST');
$mail->SMTPAuth = C('MAIL_SMTPAUTH');
$mail->Username = C('MAIL_USERNAME');
$mail->Password = C('MAIL_PASSWORD');
$mail->SMTPSecure = C('MAIL_SECURE');
$mail->CharSet = C('MAIL_CHARSET');
// 装配邮件头信息
$mail->From = C('MAIL_USERNAME');
$mail->AddAddress($to);
$mail->Port = '465';
$mail->SMTPSecure = 'ssl';
// $mail->FromName = 'mybook[云南民族大学]';
$mail->FromName=C('MAIL_FROMNAME');
$mail->IsHTML(C('MAIL_ISHTML'));
// 装配邮件正文信息
$mail->Subject = $subject;
$mail->Body = $content;
// 发送邮件
return $mail->Send();
}
控制器下的
public function index(){
$to='727183480@qq.com';
$title = '123132';
$content = "该验证邮件有效期为30分钟,超时请重新发送邮件。";
if(Mail($to, $subject, $content)){
echo 1;
}else{
echo 212;
}
}