PHPMailer类下载地址:
示例代码
demo.php
require_once 'Mail.php';
$conf = [
'host' => 'smtp.163.com',
'port' => '587',
'user' => '',
'password' => '',
'setEmail' => '',
'setName' => '',
'replyEmail' => '',
'replyName' => ''
];
$email = new Mail($conf);
$toMail = "";
$toName = "";
$subject = "视网于晚间针对此事发表文章";
$content ="
央视网于晚间针对此事发表文章,呼吁年轻演员应懂得珍惜机遇,同时提醒首先应该有好的德行,而后才能有好的艺
";$attachment = [];
$result = $email->sendMail($toMail, $toName, $subject, $content, $attachment);
if ($result) {
echo $result;
} else {
echo 'false';
}
Mail.php
PHPMailer版本 6.0.5
use PHPMailer\PHPMailer\PHPMailer;
require 'PHPMailer-master/src/PHPMailer.php';
require 'PHPMailer-master/src/SMTP.php';
require 'PHPMailer-master/src/Exception.php';
class Mail
{
private $mail;
protected $_host = ''; //SMTP服务器地址
protected $_port = 25; //端口号
protected $_user = ''; //SMTP登录邮箱
protected $_pass = ''; //SMTP登录密码
protected $_setEmail = ''; //发件人邮箱
protected $_setName = ''; //发件人名称
protected $_replyEmail = ''; //回复邮箱
protected $_replyName = ''; //回复名称
public function __construct(array $conf)
{
try {
$this->_host = $conf['host'];
$this->_port = $conf['port'];
$this->_user = $conf['user'];
$this->_pass = $conf['password'];
isset($conf['setEmail']) ? $this->_setEmail = $conf['setEmail'] : $this->_setEmail = "";
isset($conf['setName']) ? $this->_setName = $conf['setName'] : $this->_setName = "";
isset($conf['replyEmail']) ? $this->_replyName = $conf['replyEmail'] : $this->_replyName = "";
isset($conf['replyName']) ? $this->_replyName = $conf['replyName'] : $this->_replyName = "";
$this->mail = new PHPMailer(true);
//Server settings
$this->mail->isSMTP(); // 启用SMTP服务
$this->mail->SMTPDebug = 0; // Debug模式。0: 关闭,1: 客户端消息,2: 客户端和服务器消息,3: 2和连接状态,4: 更详细
$this->mail->Debugoutput = 'html'; // Debug输出类型。`echo`(默认),`html`,或`error_log`
$this->mail->Host = $this->_host; // SMTP服务器地址
$this->mail->Port = $this->_port; // 端口号
$this->mail->SMTPAuth = true; // SMTP登录认证
$this->mail->SMTPSecure = 'ssl'; // SMTP安全协议 设置安全验证方式为ssl/tls
$this->mail->CharSet = "UTF-8"; //字符集
$this->mail->Encoding = "base64"; //编码方式
$this->mail->Username = $this->_user; // SMTP登录邮箱
$this->mail->Password = $this->_pass; // SMTP登录密码
//Content
$this->mail->isHTML(true); // Set email format to HTML
//Recipients
$this->mail->setFrom($this->_setEmail, $this->_setName); // 发件人邮箱和名称
if (!empty($this->_replyEmail)) {
$this->mail->addReplyTo($this->_replyEmail, $this->_replyName);// 回复邮箱和名称
}
} catch (Exception $e) {
return $this->mail->ErrorInfo;
}
}
/**
* 发送邮件
* @param string $toMail 收件人地址
* @param string $toName 收件人名称
* @param string $subject 邮件主题
* @param string $content 邮件内容,支持html
* @param array $attachment 附件列表。文件路径或路径数组
* @return mixed 成功返回true,失败返回错误消息
*/
public function sendMail($toMail, $toName, $subject, $content, $attachment = [])
{
try {
$this->mail->addAddress($toMail, $toName); //收件人(地址,昵称)
$this->mail->Subject = $subject; //邮件主题
$this->mail->Body = $content; //邮件内容
if (count($attachment) > 0) {
foreach ($attachment as $value) {
$this->mail->addAttachment($value); //添加附件
}
}
if (!$this->mail->send()) {
return $this->mail->ErrorInfo;
} else {
return true;
}
} catch (Exception $e) {
return $this->mail->ErrorInfo;
}
}
}