0

I need some advice over the following case:

I have configured the config/web.php file as follows

'components' => [
    ...
    'mailer' => [
            'class' => 'yii\swiftmailer\Mailer',

            'useFileTransport' => false,
            'transport' => [
                    'class' => 'Swift_SmtpTransport',
                    'host' => 'smtp.gmail.com',
                    'username' => 'myusername',
                    'password' => 'mypassword',
                    'port' => '587',
                    'encryption' => 'tls',
                        ],
                   ],

Also in the Controller:

$message = Swift_Message::newInstance()
                          ->setSubject('...')
                          ->setFrom(['mymail'])
                          ->setTo(['recipient'])
                          ->setBody('......');

$transport = Swift_SmtpTransport::newInstance();

$mailer = Swift_Mailer::newInstance($transport);

$mailer->send($message);

I get the Error: 'Connection could not be established with host ' (Swift_TransportException)

Any ideas? Thank you.

YorKal
  • 73
  • 1
  • 11

3 Answers3

0

Since you have got this configured as Yii component use it properly.
See documentation example:

Yii::$app->mailer->compose('contact/html', ['contactForm' => $form])
    ->setFrom('from@domain.com')
    ->setTo($form->email)
    ->setSubject($form->subject)
    ->send();

Also: see this SO question.

Community
  • 1
  • 1
Bizley
  • 17,392
  • 5
  • 49
  • 59
0

In your config set it as

 'mailer' => [
        'class' => 'yii\swiftmailer\Mailer',
        // 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.
            'transport' => [
                'class' => 'Swift_SmtpTransport',
                'host' => 'smtp.gmail.com',
                'username' => 'yourusername',
                'password' => 'yourpassword',
                'port' => '587',
                'encryption' => 'tls',
            ]

    ],
  • In your controller you can compose the email.

     Yii::$app->mailer->compose('@app/mail/layouts/reset',['model' =>$model])
     ->setTo('to@example.com')
     ->setFrom('from@example.com')
     ->setSubject('Subject Here')
     ->setTextBody('You can either render the layout or declare your body here')
     ->send();
    
  • In the above code I am using layouts. You can use layouts and pass variables to the layout and use them. If you dont want to use layouts that works as well just call the compose method

    Yii::$app->mailer->compose()

Mohan Prasad
  • 682
  • 1
  • 9
  • 34
0

I did the following and worked:

$transport = Swift_SmtpTransport::newInstance(Yii::$app->components['mailer']['transport']['host'], Yii::$app->components['mailer']['transport']['port']);
$transport->setUsername(Yii::$app->components['mailer']['transport']['username']);
$transport->setPassword(Yii::$app->components['mailer']['transport']['password']);
YorKal
  • 73
  • 1
  • 11