Rumah  >  Artikel  >  pembangunan bahagian belakang  >  yii框架中关于邮箱封装的示例代码分享

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

黄舟
黄舟asal
2017-07-27 15:00:501599semak imbas

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

}

Atas ialah kandungan terperinci yii框架中关于邮箱封装的示例代码分享. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn