首页 » 网站建设 » php登录qq邮箱技巧_以PHP门面模式实现简单的邮件发送

php登录qq邮箱技巧_以PHP门面模式实现简单的邮件发送

访客 2024-11-08 0

扫一扫用手机浏览

文章目录 [+]

举例:

门面模式就相称于电脑主机,用户要打开某个运用程序,只须要知道两步。
打开开机按钮,电脑开机后再打开运用。
开机按钮就相称于一个门面,里面的开机须要调用不同的模块,比如硬件自检,选择启动盘,加载勾引,加载内核,OS初始化,启动指定级任务等,以下也通过发邮件的例子描述门面一模式。

涉及:

1. call_user_func函数的利用2. 非常类的自定义处理3. 类的分层封装4. 发邮件功能的实现与配置

php登录qq邮箱技巧_以PHP门面模式实现简单的邮件发送

编码:

1. 必须先composer require phpmailer/phpmailer安装依赖库。

php登录qq邮箱技巧_以PHP门面模式实现简单的邮件发送
(图片来自网络侵删)

2. 创建扩展类目录,里面包括独立的配置文件,门面角色类,邮件功能类,校验类,非常类。

3. 独立的配置类,包括smtp做事地址,端口,中转邮箱账号,授权码,邮件发送者昵称(唯一标识)。

<?php/ @Notes: 邮箱SMTP做事配置 @Interface getCondition @Return mixed @Author: bqs @Time: 2020/8/31 10:15 /return [ 'smtp_server' => 'smtp.qq.com', // QQ邮箱开启的smtp 'smtp_port' => 465, // QQsmtp做事端口 'smtp_user' => '2652364582@qq.com', // 北桥苏邮箱 'smtp_pwd' => 'ynxdedefduuhecbj', // SMTP做事开启后授权码 'email_id' => '酷D' // 邮件发送者的唯一标识(自定义的昵称)];

4. 门面角色类,也便是客户直接调用的,只有一个发送方法,但是该方法须要调用校验和实际发送的方法实现。

<?php/ @Notes: 邮件门面 @Interface getCondition @Return mixed @Author: bqs @Time: 2020/8/31 13:10 /namespace mail;use think\Container;use mail\facade\MailException;use mail\facade\Mail;use mail\facade\Validate;class MailFacade{ protected $error; public static function __callStatic($method, $params) { //return (new static)->{$method}(...$params); return call_user_func([new MailFacade(),$method],$params); } / @Notes: 面向客户的邮件发送调用 @Author: bqs @Time: 2020/8/31 13:33 @Interface send @param $params @Return boolean 成功|失落败 / private function send($params) { // 校验参数 $validate = Validate::make(__FUNCTION__); $res = $validate->check($params); if (!$res) { // 抛出自定义非常 throw new MailException($validate->getError(),422); return false; } // 发送邮件 $mail = new Mail(); $res = $mail->send($params); return $res; }}

5. 自定义非常类,可以在门面角色中以该类抛出,然后在客户调用中以该类捕捉,以下自定义了缺点的输出。

<?php/ @Notes: 邮件发送校验器 @Interface getCondition @Return mixed @Author: bqs @Time: 2020/8/31 13:03 /namespace mail\facade;class MailException extends \Exception{ public function errorMessage() { return "mail error: ".$this->getMessage(); }}

6. 校验器,紧张判断客户调用传入的参数。

<?php/ @Notes: 邮件发送校验器 @Interface getCondition @Return mixed @Author: bqs @Time: 2020/8/31 13:03 /namespace mail\facade;class Validate{ protected $error; protected $type; // 方法名 public function __construct($type) { $this->type = $type; } // 创建验证器工具 public static function make($type) { return new self($type); } // 与实际传入的参数做校验 public function check($params = []) { if (empty($params)) { $this->error = "参数不敷,造孽要求"; } $this->error = call_user_func([new self($this->type),$this->type],$params); return $this->error ? false : true; } // 发送参数校验 public function send($params) { $res = ""; // 邮件 if (!isset($params[0]) || empty($params[0])) { return "邮箱不能为空"; } $email = []; if (is_array($params[0])) { $email = $params[0]; }else { $email[0] = $params[0]; } foreach ($email as $key => $val) { if (!preg_match("/^[A-Za-z0-9\u4e00-\u9fa5]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/",$val)) { return "邮箱格式禁绝确"; } } // 邮件标题 if (!isset($params[1]) || !$params[1]) { return "邮件标题不能为空"; } if (!isset($params[2]) || !$params[2]) { return "邮件内容不能为空"; } return $res; } // 获取缺点信息 public function getError() { return $this->error; }}

7. 实际的邮件发送,须要利用phpmail库。

<?php/ @Notes: 邮件实际发送 @Interface getCondition @Return mixed @Author: bqs @Time: 2020/8/31 13:03 /namespace mail\facade;use PHPMailer\PHPMailer\PHPMailer;class Mail{ protected $config = []; public function __construct() { $this->config = include(dirname(__DIR__) . "../config/mail_config.php"); } / @Notes: 发邮件 @Author: bqs @Time: 2020/8/31 13:07 @Interface send @Return mixed / public function send($params) { $to = $params[0]; // 吸收者 $subject = $params[1]; // 邮件标题 $content = $params[2]; // 邮件内容 $emails = new PHPMailer(); $emails->CharSet = 'UTF-8'; //设定邮件编码,默认ISO-8859-1,如果发中文此项必须设置,否则乱码 $emails->isSMTP(); //Enable SMTP debugging // 0 = off (for production use) // 1 = client messages // 2 = client and server messages $emails->SMTPDebug = 0; //调试输出格式 //$emails->Debugoutput = 'html'; //smtp做事器 $emails->Host = $this->config['smtp_server']; //端口 - likely to be 25, 465 or 587 $emails->Port = $this->config['smtp_port']; if ($emails->Port === 465) $emails->SMTPSecure = 'ssl';// 利用安全协议 //Whether to use SMTP authentication $emails->SMTPAuth = true; //发送邮箱 $emails->Username = $this->config['smtp_user']; //密码 $emails->Password = $this->config['smtp_pwd']; //Set who the message is to be sent from $emails->setFrom($this->config['smtp_user'], $this->config['email_id']); //回答地址 //$emails->addReplyTo('replyto@example.com', 'First Last'); // 吸收邮件方 if (is_array($to)) { foreach ($to as $v) { $emails->addAddress($v); } } else { $emails->addAddress($to); } $emails->isHTML(true);// send as HTML //标题 $emails->Subject = $subject; //HTML内容转换 $emails->msgHTML($content); //Replace the plain text body with one created manually //$emails->AltBody = 'This is a plain-text message body'; //添加附件 //$emails->addAttachment('images/phpmailer_mini.png'); //send the message, check for errors return $emails->send(); }}

8. 客户调用部分。

// 测试发邮件的门面 public function sendMail() { try { $res = \mail\MailFacade::send(["1641181271@qq.com"], "测试标题", "测试内容"); var_dump($res); die; } catch (MailException $e) { // 捕捉自定义非常类抛出 var_dump($e->errorMessage()); die; } catch (\Exception $e) { var_dump($e->getMessage()); die; } }

9. 返回true后查看邮件是否吸收。

环境哀求:

实现邮件发送是须要特定的环境和干系的配置才能实现,以下就以实现成功发送补充的操作。

第一步:

打开网址https://github.com/PHPMailer/PHPMailer/ 下载PHPMailer,PHPMailer 须要 PHP 的 sockets 扩展支持,而登录 QQ 邮箱 SMTP 做事器则必须通过 SSL 加密的, PHP 还得包含 openssl 的支持。

第二步:利用 phpinfo() 函数查看 socket 和 openssl 扩展信息(wamp server 默认启用了该扩展)。

openssl 如果没有开启请打开php.ini文件进行开启

首先检讨php.ini中;extension=php_openssl.dll是否存在, 如果存在的话去掉前面的注释符‘;’, 如果不存在这行,那么添加extension=php_openssl.dll。

PHPMailer 核心文件

第三步:QQ 邮箱设置

所有的主流邮箱都支持 SMTP 协议,但并非所有邮箱都默认开启,您可以在邮箱的设置里面手动开启。

第三方做事在供应了账号和密码之后就可以登录 SMTP 做事器,通过它来掌握邮件的中转办法。

第四步:开启 SMTP 做事

选择 IMAP/SMTP 做事,点击开启做事

第五步:验证密保

发送短信“配置邮件客户端”至1069-0700-69

第六步:获取授权码

标签:

相关文章

php为无色透明技巧_水货钻石其实也还行

从各种钻石中,可以看到大大小小的“包裹体” 图片来源:参考文献包裹体的种类多样。比钻石形成更早的包裹体,叫“原生包裹体”;与钻石同...

网站建设 2024-12-19 阅读0 评论0

phpstudy发送gbk技巧_php的文件上传

这里首先声明一下这一章的内容比较多,比较难,你要抱着和自己去世磕的态度。细微之处不放过,多敲多练是王道。 学习就像爬山,得一步一步...

网站建设 2024-12-19 阅读0 评论0