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' => [
...
'mail
er' => [
'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->mail
er->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->mail
er->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->mail
er->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();
```[...]