Home > Backend Development > PHP Tutorial > Sample code sharing for mailbox encapsulation in Yii framework

Sample code sharing for mailbox encapsulation in Yii framework

黄舟
Release: 2023-03-14 14:26:01
Original
1645 people have browsed it

Sample code sharing about mailbox encapsulation in 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();
    }

}
Copy after login

The above is the detailed content of Sample code sharing for mailbox encapsulation in Yii framework. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
Still no success
From 1970-01-01 08:00:00
0
0
0
admins.php/admins/account/loginNot Found
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template