yii 邮箱封装

Posted Ws99

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了yii 邮箱封装相关的知识,希望对你有一定的参考价值。

<?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‘ => ‘[email protected]‘,
                ‘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([‘[email protected]‘, ‘[email protected]‘]); //$message->setCc([ // ‘[email protected]‘, ‘[email protected]‘ => ‘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("[email protected]", 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 邮箱封装的主要内容,如果未能解决你的问题,请参考以下文章

Yii2 Mandrill 邮箱软验证

Yii2片段缓存详解

VSCode自定义代码片段14——Vue的axios网络请求封装

VSCode自定义代码片段14——Vue的axios网络请求封装

VSCode自定义代码片段14——Vue的axios网络请求封装

YII2 Rules 规则大全