Skip to main content

THE SWIFTMAILER INTEGRATION FOR THE YII 2 FRAMEWORK

Installation of Swiftmailer

The preferred way to install this extension is through composer.

Either run below code in composer

php composer.phar require --prefer-dist yiisoft/yii2-swiftmailer

or add code to the require section of your composer.json.

"yiisoft/yii2-swiftmailer": "~2.1.0"


Note: Version 2.1 of this extensions uses Swiftmailer 6, which requires PHP 7. If you are using PHP 5, you have to use version 2.0 of this extension, which uses Swiftmailer 5, which is compatible with PHP 5.4 and higher. Use the following version constraint in that case:

"yiisoft/yii2-swiftmailer": "~2.0.0"


Send Mail via SMTP from Yii2 Basic


Open the configuration file /config/web.php and add your email credentials in array element inside components as shown below:

<?php
$params = require(__DIR__ . '/params.php');
$config = [
     //...
     'components' => [
         'mailer' => [
             'class' => 'yii\swiftmailer\Mailer',
              'viewPath' => '@app/mail',
              'transport' => [
                  'class' => 'Swift_SmtpTransport',
                  'host' => 'smtp.yandex.ru',
                  'username' => '<username>@<yourDomain>',
                  'password' => '<userPassword>',
                  'port' => 465,
                  'encryption' => 'ssl',
              ],
              'useFileTransport' => false,
        ],
    ],
    //...
];

To send email, use the following code in any model file:

Yii::$app->mailer->compose()
    ->setFrom('<fromUsername>@<yourDomain>')
    ->setTo('<user@Email>')
    ->setSubject('Your Subject <yourDomain>')
    ->setTextBody('Body Content Here')
    ->setHtmlBody('<p>HTML Content Here</p>')
    ->send();

Send Mail via SMTP from Yii2 Advanced


Send Mail via SMTP from Yii2 advanced is same as basic. Open the configuration file /common/config/main-local.php and add your email credentials in array element inside components as shown below:

    <?php
    return [
        'components' => [
            //...
            'mailer' => [
                 'class' => 'yii\swiftmailer\Mailer',
                  'viewPath' => '@common/mail',
                  'transport' => [
                      'class' => 'Swift_SmtpTransport',
                      'host' => 'smtp.yandex.ru',
                      'username' => '<username>@<yourDomain>',
                      'password' => '<userPassword>',
                      'port' => 465,
                      'encryption' => 'ssl',
                  ],
                  'useFileTransport' => false,
            ],
        ],
    ];

To send email, use the following code in any model file:

Yii::$app->mailer->compose()
    ->setFrom('<fromUsername>@<yourDomain>')
    ->setTo('<user@Email>')
    ->setSubject('Your Subject <yourDomain>')
    ->setTextBody('Body Content Here')
    ->setHtmlBody('<p>HTML Content Here</p>')
    ->send();

Major differences in these examples only the configuration file.

Comments

Popular posts from this blog

Moving posts from one site to another in WordPress

Migrating content from one WordPress site to another is considered a daunting task. Most people resort to copying the content from each of the posts and pages, over to the new site. They would also need to copy details like post author, timestamp and comments. This method will take too long if there are many posts. Also the comments would list the author name as the person who adds it to the new site. Others would probably copy the same from the old database to the new one. This requires some basic technical knowledge about MySQL, and also how WordPress stores its data. We may want to copy the posts from one site to another for various purposes. Either we could have changed the domain, or maybe we have revamped an existing site and need the content to be moved to the new site. It is just two steps away! : Export and Import The solution to this is provided by WordPress itself, and it isn't as scary as you would expect.  WordPress comes with a built-in “Export” tool, and an ...

Dependent dropdown in magento 2

With dynamic dependent form fields, Store owner can easily ask further qualifying questions based on the user’s answers using various conditions. Basically, dependent forms dynamically show/hide or changes the value of form fields based on the user selections. For example, if you want to list out products of a particular category, then we have to create category-products dropdown so that when users select a category, related products will be shown in dropdown option. Here we’ve come up with the custom code in which whenever user picks country option from drop-down, state drop-down will be displayed with respective options based on the dropdown country selection. And also when user picks country and state option, city option will be displayed based on the country and state selection. We have developed this functionality using Ajax code. Firstly, you need to add form to your phtml file with following code. <select name="country" id="country...