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

黄舟
发布: 2017-07-27 15:00:50
原创
1833人浏览过

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

代码小浣熊
代码小浣熊

代码小浣熊是基于商汤大语言模型的软件智能研发助手,覆盖软件需求分析、架构设计、代码编写、软件测试等环节

代码小浣熊 51
查看详情 代码小浣熊
<?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框架中关于邮箱封装的示例代码分享的详细内容,更多请关注php中文网其它相关文章!

相关标签:
最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号