I have a requirement that the email should be send to the the user who has been assigned a task by another user. For example, User A creates task for User B. Now an email should be send from User A email address to User B email address. I have configured the mailer property in application configuration as shown below. I have used a gmail address but every time the email is send from this email address which is set in the transport property. How to change that the user who creates the task, from his email id email should be send to other user.
One thing I noticed is you are using Yii::$app->user->identity->getonlyid(), I assume you want to get logged user id. If so then just use Yii::$app->user->id
The emails are being send using the email address specified in the mailer property of the application configuration. I want that email should be send from that user email address who creates the task for another user.
For eg: User A creates task for User B. User A has email id a@gmail.com and User B has email id b@gmail.com. So email should be send from User A email id to User B email id
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
'transport'=>[
'class'=>'Swift_SmtpTransport',
'host'=>'smtp.gmail.com',
'username'=>'abc@gmail.com', // from this email address email are being sent to the users all the time
'password'=>'', //app password
'port'=>'587',
'encryption'=>'tls',
],
'viewPath'=>'@app/mail',
'useFileTransport' => false,
],
Check setFrom() if it can help. You can use setReplyTo() to set email of user, so when receiver reply the email it will be automatically sent to task giving user.
Can you please check the entire values in your debug toolbar? Just to ensure your mail client doesn’t mess with the replyTo value and displays this instead.
However I strongly suggest not to use such a dynamic way to send emails. It’s not intended that way and could certainly cause harm.
A coworker of mine did that as well… An attacker found that out and abused it. They created a bot that submitted many different forms in order to send many emails in the name of several different mail addresses.
The end result was, that the original mail server was blocked everywhere because they were blacklisted.
It’s not adviced to send a bunch of mails all the time with different from values
Ok. Thanks. Should it be feasible if I specify a static email id in the mailer and send the mails from that email address only to all the users who have been assigned the task.