轻源码

  • QingYuanMa.com
  • 全球最大的互联网技术和资源下载平台
搜索
猜你喜欢
查看: 8139|回复: 0

yii框架中关于邮箱封装的示例代码分享

[复制链接]

0

主题

0

帖子

1万

积分

钻石会员

Rank: 8Rank: 8

积分
17452
QQ
发表于 2020-3-30 04:30 | 显示全部楼层 |阅读模式
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' => 'xxx@163.com',
                'password' => 'xxx',
                'port' => '994',
                'encryption' => 'ssl', //ssl tls
            ];

            self::$obj = Yii::createObject([
                    'class' => 'yiiswiftmailerMailer',
                    '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 EguliasEmailValidatorEmailValidator;
            use EguliasEmailValidatorValidationRFCValidation;

            $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();
    }

}

回复

使用道具 举报

轻源码让程序更轻更快

QingYuanMa.com

工作时间 周一至周六 8:00-17:30

客服QQ点击咨询

微信公众号

APP客户端

Copyright © 2016-2019 https://www.qingyuanma.com/ 鲁ICP备17014494号

快速回复 返回顶部 返回列表