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;
}
}