在thinkPHP目录下的conf文件夹里的convention.php里面配置好邮箱信息
'MAIL_SMTP' => TRUE, 'MAIL_HOST' => 'XXXXX',//邮件发送SMTP服务器 'MAIL_SMTPAUTH' => TRUE, 'MAIL_USERNAME' => 'XXXXX',//SMTP服务器登陆用户名 'MAIL_PASSWORD' => 'XXXXX',//SMTP服务器登陆密码 'MAIL_SECURE' => 'tls', 'MAIL_CHARSET' => 'utf-8', 'MAIL_ISHTML' => TRUE, //封装sendMail方法 function sendMail($to, $subject, $content) { vendor('PHPMailer.class#phpmailer'); vendor('SMTP'); $mail = new \PHPMailer(); //实例化 // 装配邮件服务器 if (C('MAIL_SMTP')) { $mail->IsSMTP(); //启动SMTP } $mail->Host = C('MAIL_HOST'); //SMTP服务器地址 $mail->SMTPAuth = C('MAIL_SMTPAUTH'); //启用SMTP认证 $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->SMTPSecure = 'ssl'; $mail->Port = 465; $mail->AddAddress($to); $mail->FromName = 'train';//设置每行字符长度 $mail->IsHTML(C('MAIL_ISHTML'));//是否是HTML字样 $mail->Subject = $subject;// 邮件标题信息 $mail->Body = $content;//邮件内容 // 发送邮件 if (!$mail->Send()){ return FALSE; } else { return TRUE; } //return $mail->Send(); }
在PHP文件里这样写:
public function send(){ if(sendMail('123456@qq.com','你好!邮件标题','这是一篇测试邮件正文!')){ echo '发送成功!'; } else{ echo '发送失败!'; } } }