Search code examples
phpemailyii2sendmail

Yii2 Send email by any username and password


How can I send an email by any email account and password, like below or by any another way:

$mail->setFrom('[email protected]');
$mail->setPassword('abcxyz');
$mail->setHeader('This is a header');
$mail->setBCC('[email protected]');
$mail->setCC('[email protected]');
$mail->setContent('contents absjadha ');
$mail->send();

I have over 100 emails, and I want to use it to send emails to my users or let them send emails to the others by my tool. Is there any Yii2 plugin or extension that can help me to solve this problem? Providing that all their email and password stored in my database. Thank you in advance!


Solution

  • Put this directly in the Controller

    Yii::$app->mailer->setTransport([
        'class' => 'Swift_SmtpTransport',
        'host' => 'smtp.gmail.com',
        'username' => $emailFrom,
        'password' => $password,
        'port' => '465',
        'encryption' => 'ssl',
        // smtp.gmail.com    ---   StartTLS ---   587
        // smtp.gmail.com    ---    SSL       ---      465
    ]);
    

    before this line

    Yii::$app->mail->compose('@common/mail/cron_acordo', ['model_definicoes' => $model_definicoes])