<?php
namespace app\service;
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use think\facade\Config;
class Email
{
public $error;
public function send($email,$subject,$body)
{
try{
$result = filter_var($email, FILTER_VALIDATE_EMAIL);
if(!$result){
throw new \Exception("请输入正确的邮箱");
}
$config = Config::get('smtp');
$mail=new PHPMailer(true);
//Server settings
$mail->SMTPDebug = SMTP::DEBUG_OFF; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = $config['smtp_host']; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
//smtp登录的账号 这里填入字符串格式的qq号即可
$mail->Username = $config['smtp_email']; // SMTP username
//smtp登录的密码 使用生成的授权码(通过qq邮箱活得的授权码)
$mail->Password = $config['smtp_email_pwd']; // SMTP password
//设置使用ssl加密方式登录鉴权
$mail->SMTPSecure = 'ssl'; // Enable TLS encryption, `ssl` also accepted
//设置ssl连接smtp服务器的远程服务器端口号,以前的默认是25,但是现在新的好像已经不可用了 可选465或587
$mail->Port = $config['smtp_port']; // TCP port to connect to
//Recipients
$mail->setFrom($config['smtp_email'],'昵称');
//设置收件人邮箱地址 该方法有两个参数 第一个参数为收件人邮箱地址
// 第二参数为给该地址设置的昵称 不同的邮箱系统会自动进行处理变动 这里第二个参数的意义不大
$mail->addAddress($email); // Add a recipient
//$mail->addAddress('ellen@example.com'); // Name is optional
//答复
$mail->addReplyTo($config['smtp_email'], '昵称');
//$mail->addCC('cc@example.com');
//$mail->addBCC('bcc@example.com');
//Attachments 附件
//$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
//$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
//Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = $subject;
$mail->Body = $body;
// $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$result = $mail->send();
return true;
}catch (\Exception $e){
$this->error = $e->getMessage();
return false;
}
}
}
暂无评论