Heim  >  Artikel  >  Backend-Entwicklung  >  Beispielcode-Freigabe für die Postfachkapselung im Yii-Framework

Beispielcode-Freigabe für die Postfachkapselung im Yii-Framework

黄舟
黄舟Original
2017-07-27 15:00:501531Durchsuche

Beispielcode-Freigabe zur Postfachkapselung im YII-Framework

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

}

Das obige ist der detaillierte Inhalt vonBeispielcode-Freigabe für die Postfachkapselung im Yii-Framework. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn