Difference between #2 and #5 of
How to send emails using SMTP

Changes

Title unchanged

How to send emails using SMTP

Category unchanged

How-tos

Yii version changed

2.0

Tags changed

yii2, email, email, yii2,mailer, smtp

Content changed

<a href="https://twitter.com/robregonm" class="twitter-follow-button">Follow @robregonm</a> [Ver en Español](http://www.yiiframework.com/wiki/657/como-enviar-emails-usando-smtp/)
 
 
To send emails from Yii2 is pretty straightforward, since it now uses Swiftmailer for that purpose.

Configuration
[...]
In your config file just add (it has been already added if you created your project using composer):


```php
'components' => [ ... 'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
'transport' => [
[...]
Something ike this will work:


```php

'transport' => [
'class' => 'Swift_SmtpTransport',
[...]
Use this code for sending emails from your application:


```php
Yii::$app->mailer->compose()
->setFrom('somebody@domain.com')
->setTo('myemail@yourserver.com')
[...]
In some cases you might want to use templates for email rendering, so, in that case you just need to do something like:


```php
Yii::$app->mailer->compose('@app/mail-templates/email01', [/*Some params for the view */])
->setFrom('from@domain.com')
->setTo('someemail@server.com')
[...]
Or, if you want to use one template for HTML rendering and another for text, do something like this:


```php
Yii::$app->mailer->compose(['html' => '@app/mail-templates/html-email-01', 'text' => '@app/mail-templates/text-email-01'], [/*Some params for the view */])
->setFrom('from@domain.com')
->setTo('someemail@server.com')
->setSubject('Advanced email from Yii2-SwiftMailer')
->send();
```
[...]
6 0
11 followers
Viewed: 137 751 times
Version: 2.0
Category: How-tos
Written by: robregonm
Last updated by: Wade Shuler
Created on: Apr 29, 2014
Last updated: 5 years ago
Update Article

Revisions

View all history