php 发送邮件

目的:使用PHP发送邮件。

参考资料:

https://www.cnblogs.com/woider/p/6980456.html(参考的博客)

https://codeload.github.com/PHPMailer/PHPMailer/zip/master(发送邮件的库)

用的是163邮箱.

第一步,启用客户端授权码。

php 发送邮件

 

 

 

文件目录:

php 发送邮件

 

代码部分

<?php

require_once 'src/PHPMailer.php';
require_once 'src/SMTP.php';
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP; 
class MailerUtil
{
    
    public static $HOST = 'smtp.163.com'; // 163 邮箱的服务器地址
    public static $PORT = 465; // smtp 服务器的远程服务器端口号
    public static $SMTP = 'ssl'; // 使用 ssl 加密方式登录
    public static $CHARSET = 'UTF-8'; // 设置发送的邮件的编码

    private static $USERNAME = '账号'; // 授权登录的账号
    private static $PASSWORD = '授权码'; // 授权登录的密码
    private static $NICKNAME = '发件人的昵称'; // 发件人的昵称

    /**
     * MailerUtil constructor.
     * @param bool $debug [调试模式]
     */
    public function __construct($debug = false)
    {
        $this->mailer = new PHPMailer();
        $this->mailer->SMTPDebug = $debug ? 1 : 0;
        $this->mailer->isSMTP(); // 使用 SMTP 方式发送邮件
    }

    /**
     * @return PHPMailer
     */
    public function getMailer()
    {
        return $this->mailer;
    }

    private function loadConfig()
    {
        /* Server Settings  */
        $this->mailer->SMTPAuth = true; // 开启 SMTP 认证
        $this->mailer->Host = self::$HOST; // SMTP 服务器地址
        $this->mailer->Port = self::$PORT; // 远程服务器端口号
        $this->mailer->SMTPSecure = self::$SMTP; // 登录认证方式
        /* Account Settings */
        $this->mailer->Username = self::$USERNAME; // SMTP 登录账号
        $this->mailer->Password = self::$PASSWORD; // SMTP 登录密码
        $this->mailer->From = self::$USERNAME; // 发件人邮箱地址
        $this->mailer->FromName = self::$NICKNAME; // 发件人昵称(任意内容)
        /* Content Setting  */
        $this->mailer->isHTML(true); // 邮件正文是否为 HTML
        $this->mailer->CharSet = self::$CHARSET; // 发送的邮件的编码
    }

    /**
     * Add attachment
     * @param $path [附件路径]
     */
    public function addFile($path)
    {
        $this->mailer->addAttachment($path);
    }


    /**
     * Send Email
     * @param $email [收件人]
     * @param $title [主题]
     * @param $content [正文]
     * @return bool [发送状态]
     */
    public function send($email, $title, $content)
    {
        $this->loadConfig();
        $this->mailer->addAddress($email); // 收件人邮箱
        $this->mailer->Subject = $title; // 邮件主题
        $this->mailer->Body = $content; // 邮件信息
        return (bool)$this->mailer->send(); // 发送邮件
    }
}
 
// 实例化 MailerUtil
$mailer = new MailerUtil(true);
// 添加附件
//$mailer->addFile('可以发送附件');
// 邮件标题
$to = '收件箱邮箱';
$title = '测试邮件标题';
// 邮件内容
$content = <<< EOF
邮件正文
EOF;
// 发送邮件
$mailer->send($to, $title, $content);