Yii2 sends email, yii2 sends email

Source: Internet
Author: User

Yii2 sends email, yii2 sends email

Official documents: http://www.yiiframework.com/doc-2.0/guide-tutorial-mailing.html

When using the Yii2 framework, sometimes you need to send an email. Yiii2 provides the swiftMailer extension;

1. swiftMailer

Swift is a PHP function library that uses fully object-oriented encoding for sending e-mail. Swift does not rely on the mail () function of PHP, because it occupies high server resources when sending multiple emails. Swift can send emails faster and more efficiently by directly connecting to the SMTP server or MTA.

2. installation:

Https://github.com/yiisoft/yii2-swiftmailer

Here are the specific installation methods and usage:

A. Configuration:

Return [//.... 'components' => ['mailer' => ['class' => 'yii \ swiftmailer \ mailer', 'usefiletransport '=> false, 'Transport '=> ['class' => 'SWIFT _ SmtpTransport', 'host' => 'smtp .163.com ', 'username' =>' *** @ 163.com ', 'Password' => '******', // note the following, if you log on to the POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV service in a third-party email address, you must set a logon authorization code. When you log on, the password is the authorization code that is lost, the password entered here also corresponds to the authorization code 'Port' => '25', 'encryption' => 'tls ',], 'messageconfig' => ['charset' => 'utf-8 ', 'from' => ['*** @ 163.com' => '** Customer Service'],];

3. Usage:

$ Mail = Yii: $ app-> mailer-> compose (); $ mail-> setTo ('*** @ 163.com '); // The email address to be sent $ mail-> setSubject ("subject"); // The Email Subject $ mail-> setTextBody ('plain text content '); // publish plain text $ mail-> setHtmlBody ("message content"); // the content of the sent message $ res = $ mail-> send ();

Note: During Batch Sending, you only need to set setTo to prevent re-passing the mailbox array. setTo (['mail1 @ mail.com ', 'mail1 @ mail.com'])

4. Custom Email template:

We will make some configuration during configuration, one of which is

'viewPath' => '@common/mail',

Therefore, we can create a new template file template. php In this directory, and write the content as needed.

Then, when defining:

Yii: $ app-> mailer-> compose ('template', ['params' => 'parameter 1'])

 

That is, the name of the parameter input template file. Of course, the input parameter is also supported, that is, the second parameter.

5. custom layout

Customize layout for emails. If layout is placed under common \ layouts \ mail, the file is layout. php:

Yii: $ app-> mailer-> compose ('template', ['html '=> 'layout', // The key is fixed, value is the template file name 'params' => 'parameter 1'])-> setTo ('** @ 163.com')-> setSubject (***** topic ') -> send ();

6. send an email with an attachment: send an email with an image and a Word document in the form of an attachment.

$ Message = Yii: $ app-> mailer-> compose (); $ message-> attach ('picture access '); $ message-> attachContent ('attachment content', ['filename' => 'test. word', 'contenttype' => 'text/plain ']); $ message-> setTo (' *** @ 163.com '); $ message-> setSubject ('*** topic'); $ message-> send ();

Note: If you want to put images in the email content instead of sending attachments, you can use the template file and input parameters. However, some mailboxes still extract the images and send them as attachments.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.