Yii 2 Mailer and Auto-Responder

My first thought was to put this on a behavior on the controller, but that wasn’t quite right. The implementation of behaviors that I’ve seen in controllers allow you to pick specific actions, which is good, but defeats the purpose of applying to all actions, which is what I wanted.

If only there were a way in Yii 2 to apply a set of instructions to all actions. Actually there is. It’s called an afterAction. They also have a beforeAction method, but that’s not what we were looking for. So the afterAction method looks like this:

public function afterAction($action, $result)
   //your code here
     return parent::afterAction($action, $result);

It automatically takes in two arguments and as long as you call the parent, it will run after each action on the controller.

So I thought, hmm, maybe this will work. Thinking out the code before writing it, I thought that we could simply do a check on the action and controller name, see if there is a record for that specific action/controller pair, and if so, return it and run the afterAction, which would have a method that would send the email. Or if not, don’t run the parent and return false. Or something like that.

So, eager to try this, I set up my data structure for the email messages. And because I’m thinking a little more ahead, I named the table status_message. I figured I might be able to reuse the body of the emails for other messages as some point, and I wanted to give myself flexibility for extensibility. So that’s why I didn’t call it email_message. If you are more comfortable with that however, feel free to name it how you wish. Just make sure if you do pick a different name, to reference it correctly in the rest of the tutorial.

So the table structure looks like this:

id (int, Primary Key, Not Null, Auto Increment)
controller_name (varchar(105), Not Null)
action_name (varchar(105) Not Null)
status_message_name (varchar(105) Not Null)
subject (varchar(255) Not Null)
body (varchar(2025) Not Null)
status_message_description (varchar(255) Not Null)
created_at (DateTime)
updated_at (DateTime)

Ok, nothing too crazy there. I gave myself a description field, so I could describe the purpose of the message.

Now this data structure may evolve over time, but to get things up and running, I’ve kept it simple.

Also, note, I do not use migrations, that is a personal choice, but that is why I don’t provide the migration. I use MySql Workbench and PhpMyAdmin for these kinds of tasks. If you are following along and don’t use either of those two, feel free to use the method/tool of your choice.

So after creating the data structure, I could see that I needed a simple check to see if a record existed, and if so, return either false, indicating there is no message for that controller/action or return the id of the message, which I could then use in another method to retrieve the parts of the message that I wish to send via email.

Could I have done it all in one method? Yes, very easily, but by breaking it apart into multiple parts, the code is easier to digest, easier to write, and easier to reuse.

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