Can swiftMailer be used on localhost to send emails?

php, swiftmailer, yii2

Solution

You can send mail through localhost in Yii2 with following config.

'mailer' => [
        'class' => 'yii\swiftmailer\Mailer',
        'useFileTransport' => false,
        'transport' => [
            'class' => 'Swift_SmtpTransport',
            'host' => 'smtp.gmail.com',  
            'username' => 'ENTER_EMAIL_ADDRESS_HERE',
            'password' => 'ENTER_PASSWORD',
            'port' => '587', 
            'encryption' => 'tls', 
        ],
    ]

and in your controller

\Yii::$app->mail->compose('your_view', ['params' => $params])
    ->setFrom([\Yii::$app->params['supportEmail'] => 'Test Mail'])
    ->setTo('to_email@xx.com')
    ->setSubject('This is a test mail ' )
    ->send();

Problem

I am currently not receiving emails using the below configuration and was wondering if's it something to do with my set up maybe missing something or it doesnt work on MAMP localhost? main-local.php in common config directory ``` 'mailer' => [ 'class' => 'yii\swiftmailer\Mailer', 'viewPath' => '@common/mail', // send all mails to a file by default. You have to set // 'useFileTransport' to false and configure a transport // for the mailer to send real emails. 'useFileTransport' => true, ], ``` And then to send the email (which does display a success message) ``` public function submitreview($email) { //return Yii::$app->mailer->compose(['html' => '@app/mail-templates/submitreview']) return Yii::$app->mailer->compose() ->setTo($email) ->setFrom([$this->email => $this->name]) ->setSubject($this->title) ->setTextBody($this->description) ->attachContent($this->file) ->send(); } ```

Original source

Related problems