700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > PHP使用QQ邮箱发送邮件无需SMTP服务器

PHP使用QQ邮箱发送邮件无需SMTP服务器

时间:2021-01-14 04:20:05

相关推荐

PHP使用QQ邮箱发送邮件无需SMTP服务器

首先需要开启openssl扩展

然后在GitHub上下载PHPMailer:/PHPMailer/PHPMailer

封装发送邮件的类

<?php// 引入PHPMailer的核心文件require_once 'PHPMailer/src/PHPMailer.php';require_once 'PHPMailer/src/SMTP.php';use PHPMailer\PHPMailer\PHPMailer;use PHPMailer\PHPMailer\SMTP;class QQMailer{public static $HOST = ''; // QQ 邮箱的服务器地址public static $PORT = 465; // smtp 服务器的远程服务器端口号public static $SMTP = 'ssl'; // 使用 ssl 加密方式登录public static $CHARSET = 'UTF-8'; // 设置发送的邮件的编码private static $USERNAME = '123456@'; // 授权登录的账号private static $PASSWORD = '*********'; // 授权码private static $NICKNAME = 'Tom'; // 发件人的昵称/*** @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;}/*** 加载配置* [loadConfig description]*/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; // 发送的邮件的编码}/*** 添加附件* @param $path [附件路径]*/public function addFile($path){$this->mailer->addAttachment($path);}/*** 发送邮件* @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(); // 发送邮件}}

最后测试一下

// 测试// 实例化 QQMailer 类$mailer = new QQMailer(true);// 邮件标题$title = '测试邮件';// 邮件内容$content = <<< EOF<p align="center">测试效果如下</p>EOF;// 添加附件$mailer->addFile('123.png');// 发送QQ邮件$mailer->send('7891011@', $title, $content);

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。