composer require phpmailer/phpmailer
<?php
namespace app\admin\service;
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
class MailService
{
public static function sendEmail($to,$subject='',$content='',$addAttachment=''){
$openssl_funcs = get_extension_funcs('openssl');
if(!$openssl_funcs){
return ['code'=>0 , 'msg'=>'請先開啟openssl擴充套件'];
}
$config=sysconfig("mail");
$mail = new PHPMailer;
$mail->CharSet = 'UTF-8';
$mail->isSMTP();
$mail->SMTPDebug = 0;
$mail->SMTPAuth = true;
$mail->Debugoutput = 'html';
$mail->Host = $config['mail_smtp_host'];
$mail->Port = $config['mail_smtp_port'];
$mail->SMTPSecure =$config['mail_secure'];
$mail->Username = $config['mail_smtp_user'];
$mail->Password = $config['mail_smtp_pass'];
$mail->setFrom($config['mail_smtp_user'],$config['mail_smtp_name']);
if(is_array($to)){
foreach ($to as $v){
$mail->addAddress($v);
}
}else{
$mail->addAddress($to);
}
$mail->isHTML(true);
$mail->Subject = $subject;
$mail->Body = $content;
if (!empty($addAttachment)){
$mail->addAttachment($addAttachment);
}
try {
$mail->send();
return ['code'=>1 , 'msg'=>'傳送成功'];
}catch (Exception $e){
return ['code'=>0 , 'msg'=>$e->getMessage()];
}
}
}
本作品採用《CC 協議》,轉載必須註明作者和本文連結