zoukankan      html  css  js  c++  java
  • 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(); } }
  • 相关阅读:
    MySQL运维案例分析:Binlog中的时间戳
    身边有位“别人家的程序员”是什么样的体验?
    苹果收取30%过路费_你是顶是踩?
    1019 数字黑洞 (20 分)C语言
    1015 德才论 (25 分)C语言
    1017 A除以B (20 分)C语言
    1014 福尔摩斯的约会 (20 分)
    求n以内最大的k个素数以及它们的和、数组元素循环右移问题、求最大值及其下标、将数组中的数逆序存放、矩阵运算
    1005 继续(3n+1)猜想 (25 分)
    爬动的蠕虫、二进制的前导的零、求组合数、Have Fun with Numbers、近似求PI
  • 原文地址:https://www.cnblogs.com/luckcs/p/7240079.html
Copyright © 2011-2022 走看看