<?php

class Mailer
{ private static $obj;
private static $config; public static function getMailer()
{ if (!is_object(self::$obj)) {
self::$config = [
'class' => 'Swift_SmtpTransport',
'host' => 'smtp.163.com',
'username' => 'xxx@163.com',
'password' => 'xxx',
'port' => '994',
'encryption' => 'ssl', //ssl tls
]; self::$obj = \Yii::createObject([
'class' => 'yii\swiftmailer\Mailer',
'viewPath' => '@common/mail',
'useFileTransport' => false,
'transport' => self::$config,
]);
} return self::$obj;
} public static function send($toEmail, $subject, array $compose)
{
$user = \Wskm::getUser(); if ($compose) {
        //同时设置2种内容,让用户的偏好自己选择
self::getMailer()->compose(
//['html' => 'passwordResetToken-html', 'text' => 'passwordResetToken-text'], ['user' => $user]
//['html' => 'passwordResetToken-html'], ['user' => $user]
$compose
);
}else{
self::getMailer()->setBody('My <em>amazing</em> body', 'text/html'); self::getMailer()->addPart('My amazing body in plain text', 'text/plain');
}
//https://swiftmailer.symfony.com/docs/messages.html //addTo addCc addBcc
//$message->setTo(['some@address.tld', 'other@address.tld']);
//$message->setCc([
// 'person1@example.org', 'person2@otherdomain.org' => 'Person 2 Name',
//]); //->attach(Swift_Attachment::fromPath('my-document.pdf')->setFilename('cool.jpg')) /*
// Create the message
$message = new Swift_Message('My subject'); // Set the body
$message->setBody(
'<html>' .
' <body>' .
' Here is an image <img src="' . // Embed the file
$message->embed(Swift_Image::fromPath('image.png')) .
'" alt="Image" />' .
' Rest of message' .
' </body>' .
'</html>',
'text/html' // Mark the content-type as HTML
);
*/ /*
* 验证
use Egulias\EmailValidator\EmailValidator;
use Egulias\EmailValidator\Validation\RFCValidation; $validator = new EmailValidator();
$validator->isValid("example@example.com", new RFCValidation());
*/ /*
* 加密
$smimeSigner = new Swift_Signers_SMimeSigner();
$smimeSigner->setSignCertificate('/path/to/certificate.pem', ['/path/to/private-key.pem', 'passphrase']);
$message->attachSigner($smimeSigner);
*/ /*
* 回执
$MESSAGE->setReadReceiptTo('你@地址。 TLD ');
*/ /**
* ->setCharset('iso-8859-2'); 编码
* ->setPriority(2); 设置优先级,1-5
*/ return self::getMailer()->compose(
//['html' => 'passwordResetToken-html', 'text' => 'passwordResetToken-text'], ['user' => $user]
['html' => 'passwordResetToken-html'], ['user' => $user]
)
->setFrom([ self::$config['username'] => 'test robot'])
->setTo($toEmail)
->setSubject($subject)
->send();
} }

yii 邮箱封装的更多相关文章

  1. YII 自封装的批量修改的mysql操作类

    <?php /** * Created by PhpStorm. * User: yufen * Date: 2018/8/31 * Time: 9:54 */ namespace app\ba ...

  2. YII Framework学习教程-YII的异常处理

    异常无处不在,作为程序员,活着就是为了创造这些异常,然后修复这些异常而存在的.YII框架封装了PHP的异常,让异常处理起来更简单. 使用 YII处理错误和异常的配置方法: 你可以在入口文件中定义YII ...

  3. YII model模型和登陆详解

    模型是 CModel 或其子类的实例.模型用于保持数据以及与其相关的业务逻辑. 模型是单独的数据对象.它可以是数据表中的一行,或者一个用户输入的表单. 数据对象的每个字段对应模型中的一个属性.每个属性 ...

  4. yii2 session的使用方法

    yii2打开session use yii\web\Session; $session = Yii::$app->session; // check if a session is alread ...

  5. IntelliJ IDEA 2017版 spring-boot 2.0.5 邮件发送简单实例 (三)

    一.搭建SpringBoot项目 详见此文:https://www.cnblogs.com/liuyangfirst/p/8298588.html 注意: 需要添加mail依赖的包,同时还添加了lom ...

  6. Yii2 session的使用方法(1)

    yii2打开session use yii\web\Session; $session = Yii::$app->session; // check if a session is alread ...

  7. yii1 session

    在 Yii框架中使用session 的笔记: 首先,在Yii框架中,你不需要像标准PHP代码那样使用session_start(),在Yii框架中,autoStart 属性缺省被设置为true,所以, ...

  8. 第12.6节 Python标准库其他内置模块导览

    一. 文本处理服务 string模块 : 常见的字符串操作 difflib模块: 计算差异的辅助工具 textwrap模块: 文本自动换行与填充,能够格式化文本段落,以适应给定的屏幕宽度: unico ...

  9. yii框架中邮箱激活(数字签名)

    控制器: //发送邮箱,激活账号   public function actionEmail()   {        $email=Yii::$app->request->get('em ...

随机推荐

  1. VSS(Virtual Switching System)

    一.虚拟交换系统(VSS) VSS是一种网络虚拟化技术,讲两台Catalyst 6500系列交换机组合为单一虚拟交换机,从而提高运营效率.增强不间断通信,并将系统带宽容量扩展到1.4Tbps.在初始阶 ...

  2. jeecg自定义按钮使用exp属性不起作用

    为什么要写这篇文章? 之前写过一篇类似的文章 jeecg笔记之自定义显示按钮exp属性,但是有些小伙伴留言参考后不起作用,当时我的 jeecg 版本为3.7.5,最终以版本不同,暂时搁浅了.今天,重新 ...

  3. php7.2连接Sqlserver2008 r2

    下载Sql Server PHP扩展 Microsoft Drivers for PHP for SQL Server https://github.com/Microsoft/msphpsql/re ...

  4. ngnix 反向代理来解决前端跨域问题

    1.定义 跨域是指a页面想获取b页面资源,如果a.b页面的协议.域名.端口.子域名不同,所进行的访问行动都是跨域的,而浏览器为了安全问题一般都限制了跨域访问,也就是不允许跨域请求资源.注意:跨域限制访 ...

  5. LeetCode-7-反转整数-c# 版本

    c# 版本 // 给定一个 32 位有符号整数,将整数中的数字进行反转. public class Solution { public int Reverse(int x) { / // 边界判断 / ...

  6. C++ 用三元组表示法存储稀疏矩阵

    若有一个矩阵(m*n),其中非0元素个数远少于数值为0的元素个数,若开辟一个m*n大空间,来存储这样一个很多元素值为0的矩阵,浪费空间,于是我们只存储这些非0的元素的下标及数值 用一个结构体——三元组 ...

  7. 爬虫----scrapy账号登录豆瓣,并且重定向到电影界面,获取界面信息

    Request:这是url重定向 FormRequest:这是表单提交,就是登录界面时,输入账号.密码,点击登陆的过程 # -*- coding: utf-8 -*-import scrapyfrom ...

  8. 用html5实现音频播放器

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...

  9. 005-CSS让页脚始终在底部不论页面内容多少

    让页脚始终在页面底部,不论页面内容是多或者少页脚始终在页面底部. 方案一: <!DOCTYPE html> <html> <head> <meta chars ...

  10. react知识总结

    用于构建用户界面的 JavaScript 库 JSX语法 style let style = { color: 'r'+'ed', fontSize: '30px' } let jsx = <d ...