- 使用composer 安装phpmailer
composer require phpmailer/phpmailer
- common.php写个发送邮件的函数(腾讯邮箱的为例)
/**
* 系统邮件发送函数
* @param string $tomail 接收邮件者邮箱
* @param string $name 接收邮件者名称
* @param string $subject 邮件主题
* @param string $body 邮件内容
* @param string $attachment 附件列表
* @return boolean
* @author static7 <static7@qq.com>
*/
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
function send_mail($to, $name, $subject = '', $body = '', $attachments = []) {
$mail = new PHPMailer(true);
try {
// Server settings
$mail->CharSet = 'UTF-8';
$mail->isSMTP();
$mail->SMTPDebug = 0;
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'ssl';
$mail->Host = 'smtp.exmail.qq.com';
$mail->Port = 465;
$mail->Username = 'static7@qq.com';
$mail->Password = '';
// Sender info
$mail->setFrom('static7@qq.com', 'static7');
$mail->addReplyTo('');
// Recipient
$mail->addAddress($to, $name);
// Attachments
foreach ($attachments as $attachment) {
if (is_file($attachment)) {
$mail->addAttachment($attachment);
}
}
// Content
$mail->isHTML(true);
$mail->Subject = $subject;
$mail->Body = $body;
$mail->send();
return true;
} catch (Exception $e) {
return $e->getMessage();
}
}
- 控制器方法里写发送的内容
/**
* tp5邮件
* @param
* @author staitc7 <static7@qq.com>
* @return mixed
*/
public function email() {
$toemail='static7@qq.com';
$name='static7';
$subject='QQ邮件发送测试';
$content='恭喜你,邮件测试成功。';
dump(send_mail($toemail,$name,$subject,$content));
}
看了就去感受一下…