<?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. react创建项目很慢,最后提示fetch failed的解决方法

    $ cnpm install -g create-react-app //创建react全局变量 $ create-react-app my-app //创建一个react项目 国内使用 npm 速度 ...

  2. 浅析MySQL InnoDB的隔离级别

    MySQL InnoDB存储引擎中事务的隔离级别有哪些?对应隔离级别的实现机制是什么? 本文就将对上面这两个问题进行解答,分析事务的隔离级别以及相关锁机制. 隔离性简介 隔离性主要是指数据库系统提供一 ...

  3. UML与软件建模:第一次作业(UML用例图绘制)

    uml第一次作业: 用例图是什么? 用例图我感觉就是把网站中各个用户的动作分解一下,再用rational rose软件把图画出来. 画例图主要分为三个步骤:a 确定系统角色  b 确定用例  c 对用 ...

  4. java的智能提示无法打开

    第一步:选中“window”->“preference”   第二步:选中“java”,并展开   第三步:选中“Editor”,并展开   第四步:选中“Content Assist”,在右侧 ...

  5. 装PIL库

    pip install Pillow -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com

  6. js常用的400个特效

    JavaScript实现可以完全自由拖拽的效果,带三个范例     http://www.sharejs.com/showdetails-501.aspx javascript实现可以自由拖动的树形列 ...

  7. react使用create-react-app创建的项目部署

    一.在所有的项目代码编写完成后,react项目直接部署是无法正常访问的 1.问题一 网页无法正常的浏览器刷新,刷新会报404错,路由找不到页面 2.问题二 路由跳转后,浏览器后退按钮点击后,页面不刷新 ...

  8. org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'multipartResolver': Failed to introspect bean class [org.springframework.web.multipart.commons.CommonsMultipartR

    在用spring mvc 做文件上传的时候出现了这个问题(能看到这篇文章就说明你已经有了那两个包了) 错误:org.springframework.beans.factory.BeanCreation ...

  9. Json数据产生树形结构

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <hea ...

  10. 查看Python、flask 版本

    查看Django版本检查是否安装成功,可以在dos下查看Django版本. 1.输入python 2.输入import django3.输入django.get_version() 查看flask版本 ...