Yii 2 Mailer and Auto-Responder

Ok, so let’s provide the last method on MailCall to wrap this up:

public static function onMailableAction($action_name, $controller_name)
{
      if ($message_id = RecordHelpers::findStatusMessage($action_name, $controller_name)){
                     
      MailCall::sendTheMail($message_id);
          
      }
       
}

It’s so simple. It hands in the action name and controller name, then uses our helper method to try to get a record in the DB and set it to $message_id. If it can’t set $message_id, then it never calls MailCall. If it can set $message_id correctly, then it calls the sendTheMail method with the $messgae_id handed in.

So to call this correctly in our signup action on the site controller:

public function actionSignup()
{
   $model = new SignupForm();
    if ($model->load(Yii::$app->request->post())) {
       if ($user = $model->signup()) {
                
           if (Yii::$app->getUser()->login($user)) {
                    
                 MailCall::onMailableAction('signup', 'site');
                   
    return $this->goHome();
            }
        }
    }

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

You see that we popped in our onMailableAction method right after the user is logged in. This is because, as you recall from our sendTheMail method, we are using:

->setTo(\Yii::$app->user->identity->email)

And you only have access to \Yii::$app->user->identity->email if the user is logged in.

Also note, this is now one line of code in the controller, everything else is extracted out into other classes. How awesome is that? Nice clean controller code.

Another tip to keep in mind. We have 3 locations for our code. The MailCall model, the RecordHelpers model, and the controller. In my application, which is the example in the book and uses the advanced template, I put the models in the common folder, so they need to be namespaced:

namespace common\models;

When you want to use MailCall in the controller, at the top of the file, you need to include:


use common\models\MailCall;

You can use alternative namespaces if your application is set up differently, you just need to make sure everything is referenced properly.

As for the UI, you can simply use Gii to build it into the backend. The only changes you need to make are adding timestamp behavior to the model, and removing unnecessary fields from the form. You can change a textInput field to the following:

 <?= $form->field($model, 'body')->textArea(['maxlength' => 2025, 'rows' =>12]) ?>

So we changed it from textInput to textArea and also handed in the optional rows parameter, which lets you control the size of the text box.

If you are unfamiliar with Gii, consult the Yii 2 guide. I also cover it extensively in my book, Yii 2 For Beginners.

And that’s pretty much it. As with all code, it is not meant to be a final solution or grand solution, just one possibility among many. A lot of the choices you make in coding come down to personal preference and there is typically always room for improvement. Just do it your way and have fun coding!

Advertisements

One thought on “Yii 2 Mailer and Auto-Responder”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s