Yii 2 Mailer and Auto-Responder

So now we need to build the method that is going to put everything into action. To get an idea of what we want to do, let’s focus on the registration auto-response. Here is the snippet from the site controller on the signup action:

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

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

Nothing special here, this is what you get out of the box on the advanced template. So we need to add something like:

MailCall::onMailableAction('signup', 'site');

MailCall is the name of the class we created for the sendTheMail method. The onMailableAction method, we have not created yet. But we can see that it takes the action and controller names as arguments, which are also names of fields in our DB.

We will use the action name and controller name to find the specific message we want to mail out for this action.

It’s worth noting that I thought of another way to reference the arguments:

MailCall::onMailableAction(__METHOD__, getUniqueId());

__METHOD__ is a magic method to return the name of the current method and like I said before, getUniqueId() returns the name of the controller. So that would get us, ‘actionSignup’ and ‘site’, which is not quite right. So in order to use this approach, we would have to get rid of the word action.

So I did this, rather incorrectly, as the following:

 $method_name = str_replace("action","",$method_name);
 $action_name = $method_name;

Cool, it strips out action and replaces it with nothing. Yeah, but what happens if you have an action named actionTraction? It will strip action out of traction and return an error. Obviously I could just remove the first six characters from the string, which would be the correct way to do it. But I never bothered to write it that way, here’s why.

I didn’t want to have to remember what __METHOD__ and getUniqueId() were returning. Also, if someone else had to maintain the code, they would not be able to instantly understand what was happening. Plus, to explicitly name the controller and action instead actually required less code.

Sometimes it just comes down to a preference of what you want to stare at. You will spend far more time reading code than writing it. So the easier that code is to read and get at first glance, the better.

Now in terms of naming the method, I use onMailableAction because it is slightly ambiguous, which I think is appropriate because nothing is going to happen if there is no corresponding message in the DB.

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