首頁  >  文章  >  後端開發  >  yii框架中關於郵箱封裝的範例程式碼分享

yii框架中關於郵箱封裝的範例程式碼分享

黄舟
黄舟原創
2017-07-27 15:00:501531瀏覽

yii框架中關於郵箱封裝的範例程式碼分享

<?php

class Mailer
{

    private static $obj;
    private static $config;

    public static function getMailer()
    {

        if (!is_object(self::$obj)) {
            self::$config = [
                &#39;class&#39; => &#39;Swift_SmtpTransport&#39;,
                &#39;host&#39; => &#39;smtp.163.com&#39;,
                &#39;username&#39; => &#39;xxx@163.com&#39;,
                &#39;password&#39; => &#39;xxx&#39;,
                &#39;port&#39; => &#39;994&#39;,
                &#39;encryption&#39; => &#39;ssl&#39;, //ssl tls
            ];

            self::$obj = \Yii::createObject([
                    &#39;class&#39; => &#39;yii\swiftmailer\Mailer&#39;,
                    &#39;viewPath&#39; => &#39;@common/mail&#39;,
                    &#39;useFileTransport&#39; => false,
                    &#39;transport&#39; => self::$config,
            ]);
        }

        return self::$obj;
    }

    public static function send($toEmail, $subject, array $compose)
    {
        $user = \Wskm::getUser();
        
        if ($compose) {
        //同时设置2种内容,让用户的偏好自己选择
            self::getMailer()->compose(
                    //[&#39;html&#39; => &#39;passwordResetToken-html&#39;, &#39;text&#39; => &#39;passwordResetToken-text&#39;], [&#39;user&#39; => $user]
                    //[&#39;html&#39; => &#39;passwordResetToken-html&#39;], [&#39;user&#39; => $user]
                    $compose
            );
        }else{
            self::getMailer()->setBody(&#39;My <em>amazing</em> body&#39;, &#39;text/html&#39;);

            self::getMailer()->addPart(&#39;My amazing body in plain text&#39;, &#39;text/plain&#39;);
        }
        //https://swiftmailer.symfony.com/docs/messages.html
        
        //addTo addCc addBcc
        //$message->setTo([&#39;some@address.tld&#39;, &#39;other@address.tld&#39;]);
        //$message->setCc([
        //    &#39;person1@example.org&#39;, &#39;person2@otherdomain.org&#39; => &#39;Person 2 Name&#39;,
        //]);
        
        //->attach(Swift_Attachment::fromPath(&#39;my-document.pdf&#39;)->setFilename(&#39;cool.jpg&#39;))
        
        /*
        // Create the message
        $message = new Swift_Message(&#39;My subject&#39;);

        // Set the body
        $message->setBody(
        &#39;<html>&#39; .
        &#39; <body>&#39; .
        &#39;  Here is an image <img src="&#39; . // Embed the file
             $message->embed(Swift_Image::fromPath(&#39;image.png&#39;)) .
           &#39;" alt="Image" />&#39; .
        &#39;  Rest of message&#39; .
        &#39; </body>&#39; .
        &#39;</html>&#39;,
          &#39;text/html&#39; // 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(&#39;/path/to/certificate.pem&#39;, [&#39;/path/to/private-key.pem&#39;, &#39;passphrase&#39;]);
            $message->attachSigner($smimeSigner);
         */
        
        /*
         * 回执
            $MESSAGE->setReadReceiptTo(&#39;你@地址。 TLD &#39;);
         */
        
        /**
         * ->setCharset(&#39;iso-8859-2&#39;);    编码
         * ->setPriority(2);                设置优先级,1-5
         */
        
        return self::getMailer()->compose(
                    //[&#39;html&#39; => &#39;passwordResetToken-html&#39;, &#39;text&#39; => &#39;passwordResetToken-text&#39;], [&#39;user&#39; => $user]
                    [&#39;html&#39; => &#39;passwordResetToken-html&#39;], [&#39;user&#39; => $user]
                )
                ->setFrom([ self::$config[&#39;username&#39;] => &#39;test robot&#39;])
                ->setTo($toEmail)
                ->setSubject($subject)
                ->send();
    }

}

以上是yii框架中關於郵箱封裝的範例程式碼分享的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn