Showing posts with label yii2 send email from localhost. Show all posts
Showing posts with label yii2 send email from localhost. Show all posts

Tuesday, April 24, 2018

Yii2 send attachment

Yii2 send attachment using swiftmailer

You can send attachment using swiftmailer using below method

Before send an attachment you need to set swiftmailer configuration:
Swiftmailer configuration

$mail = Yii::$app->mailer->compose();

// file path
$mail->attach('path/to/image.jpg');

$mail->send();

Yii2 swiftmailer

Yii2 send email using swiftmailer

Default config in Yii2

'components' => [
                'mailer' => [
                    'class' => 'yii\swiftmailer\Mailer', 
                    'transport' => [
                        'class' => 'Swift_SmtpTransport',
                        'host' => 'localhost',
                        'username' => 'username',
                        'password' => 'password',
                        'port' => '587',
                        'encryption' => 'tls',
                    ],
                ],
            ]

Send email usage:
After mailer component settings, you can use below example to send an email

$mail = Yii::$app->mailer->compose()
    ->setFrom('from@xyz.com')
    ->setTo('to@xyz.com')
    ->setSubject('Subject')
    ->setTextBody('Text')
    ->setHtmlBody('Html data');

     $mail->send();

Recent Update

yii2 session handling

use yii\web\Session; $session = Yii::$app->session; // start session $session->open(); // close session $session->close(); ...

Most Search