I tried to send email using swiftmailer in Yii2. I'm still beginner for this framework. This is my basic code:
public function sendMail($email)
{
if ($this->validate()) {
$email = Yii::$app->params['adminEmail'];
$mailto = '[email protected]'; // need to change
$isi = "blablabla";
Yii::$app->mailer->compose("@app/mail/layouts/html", ["content" => $isi])
->setTo($mailto)
->setFrom($email)
->setSubject('coba')
->send();
return true;
}
return false;
}
In my case, I want to setup a 'setTo' based on my usr table. Field of my usr table:
id | username | manager | email | type |
1 | fauzi | arie | [email protected] | user |
2 | arie | rian | [email protected] | approver |
For example, when user with id = 1 login then he's create a new post and after click submit, the action also send email to user with id = 2 (manager of fauzi) for some review before post is publish to public. Thank you.
note: I assume your
usrtable has model calledUserif not, then change it to fit your codeyou can provide also array of email adresses like
What you want to know is which users you want to get, maybe add some conditions to activerecord etc...
or
then
Variant #1you can loop trough all recordsor
Variant #2you can get all emails first