O
O
Oleg Aksenov2015-10-13 12:07:42
Yii
Oleg Aksenov, 2015-10-13 12:07:42

How to create an email alert?

Actually I use yii2-swiftmailer, according to the documentation I saw an example:

$message = Yii::$app->mailer->compose();
if (Yii::$app->user->isGuest) {
    $message->setFrom('[email protected]')
} else {
    $message->setFrom(Yii::$app->user->identity->email)
}
$message->setTo(Yii::$app->params['adminEmail'])
    ->setSubject('Message subject')
    ->setTextBody('Plain text content')
    ->send();

Here is the code where I am going to use it.
public function actionClose($id)
    {
        $model = $this->findModel($id);
        if(Yii::$app->user->identity->id==$model->author_id){
        $model = new StatusTicket();
    $model -> status_id = 6;
    $model -> ticket_id = $id;
        $message = Yii::$app->mailer->compose();
    
        if ($model -> save())
            Yii::$app->session->setFlash('success','<p>Заявка закрыта</p>');

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

I still can’t figure out how to dock it with the code, it turns out that after $model_> save I need to execute so that the notification is correct, there will be a message output and sending along with it.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey Sanych, 2015-10-13
@Takun

Format the code. Don't forget to put semicolons.

if ($model -> save()) {
    $message = Yii::$app->mailer->compose();
    if (Yii::$app->user->isGuest) {
        $message->setFrom('[email protected]');
    } else {
        $message->setFrom(Yii::$app->user->identity->email);
    }

    $mail = $message->setTo(Yii::$app->params['adminEmail'])
        ->setSubject('Message subject')
        ->setTextBody('Plain text content');

    if ($mail->send()) {
        Yii::$app->session->setFlash('success','<p>Заявка закрыта</p>');
    } else {
        Yii::$app->session->setFlash('error','<p>Ашибка</p>');
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question