具体代码如下
composer require phpmailer/phpmailer
<?php
header('content-type:text/html;charset=utf-8;');
set_time_limit(3600);
require "vendor/autoload.php";
$send_res = sendEmail('主题', '内容', '[email protected]');die;
// phpmailer 的使用
// sendEmail('主题', '内容', '收件邮箱', '附件');
function sendEmail($subject, $contents, $to_email, $attach_file='')
{
$mail = new PHPMailer\PHPMailer\PHPMailer();
$mail->isSMTP();
$mail->CharSet = 'utf8'; //设定邮件编码
$mail->Host = 'smtp.exmail.qq.com'; //SMTP服务器
$mail->SMTPAuth = true;
$mail->Username = '[email protected]'; //邮箱用户名
$mail->Password = '密码或者授权码'; //密码或者授权码
$mail->SMTPSecure = "ssl";
$mail->Port = 465; //服务器端口 25 或者465 具体要看邮箱服务器支持
$mail->IsHTML(true);
$mail->setFrom("[email protected]", "jianlong"); //发件人
//$mail->addReplyTo('[email protected]', 'info'); //回复的时候回复给哪个邮箱 建议和发件人一致
$mail->addAddress($to_email);
//$mail->addAddress('[email protected]'); // 可添加多个收件人
//$mail->addCC('[email protected]'); //抄送
//$mail->addBCC('[email protected]'); //密送
if (!empty($attach_file)) {
$file_name = basename($attach_file);
$mail->AddAttachment($attach_file, $file_name); // 发送附件并且重命名
}
$mail->Subject = $subject;
$mail->Body = $contents;
//$mail->AltBody = '如果邮件客户端不支持HTML则显示此内容';
if (!$mail->send()) {
trace($mail->ErrorInfo, 'error');
return 0;
} else {
return 1;
}
}
标签:phpmailer,方法,attach,file,使用,jianlong,mail,com,example
From: https://blog.51cto.com/ding/8349707