How to send email from one person to other using swiftMailer in Yii2

Hi, I have two tables users and tasks. Each user can create a task to other user.
Below is the table structure

User table
UserId
FullName
EmailId
username
password

Tasks table
TaskId
CreatedBy - foreign key
AssignedTo - foreign key
TaskName
TaskDescription
StartDate
EndDate
Status

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.

 'mailer' => [
            'class' => 'yii\swiftmailer\Mailer',
			'transport'=>[

			'class'=>'Swift_SmtpTransport',
			'host'=>'smtp.gmail.com',
			'username'=>'emailid', // gmail address specified
			'password'=>'', //app password
			'port'=>'587',
			'encryption'=>'tls',

			],
			'viewPath'=>'@app/mail',
            'useFileTransport' => false,
        ],

action create

 public function actionCreate()
    {
		if(\Yii::$app->user->can('createTask'))
		{
        $model = new Tasks();

        if ($model->load(Yii::$app->request->post())) {

			$emailid=\Yii::$app->db->createCommand("select EmailId from users where UserId=:id")->bindValues([':id'=>\Yii::$app->user->identity->getonlyid()])->queryAll();
			$useremail;
			foreach($emailid as $email1)
			{
				$useremail=$email1['EmailId'];
			}

			$toemail1=\Yii::$app->db->createCommand("select EmailId from users where UserId=:id")->bindValues([':id'=>$model->AssignedTo])->queryAll();
			$toemail;
			foreach($toemail1 as $toemail2)
			{
				$toemail=$toemail2['EmailId'];
			}


			$model->CreatedBy=\Yii::$app->user->identity->getonlyid();
			$model->Status="Not Started";
			$model->save();
			\Yii::$app->mailer->compose()
				->setFrom($useremail)
				->setTo($toemail)
				->setSubject('New Task Assigned - '.$model->TaskName)
				->setHtmlBody($model->TaskDescription)
				->send();

            return $this->redirect(['view', 'id' => $model->TaskId]);
        }
		else
			{

        return $this->render('create', [
            'model' => $model,
        ]);
			}
			}

		else
		{
		 throw new ForbiddenHttpException("Access denied");
		}
    }

Hi, welcome to Yii forums!
Your question is not clear as to what is not working
Please see How to ask a question that will land you a help - #2

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,
        ],

That means your setFrom() is being set to that email. var_dump $useremail to see what is actually being set

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.

If I use var_dump($useremail); then it correctly displays the email id of User A who created the task for User B

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

When I use setReplyTo() then still, the emails are being send from the one specified in the mailer instead of users email id

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.