Yii 2 Mailer and Auto-Responder

Now if you are following along in the book, you know we have a helper class named RecordHelpers, and this is where I put the following method:

public static function findStatusMessage($action_name, $controller_name)
{
    $connection = \Yii::$app->db;
        $sql = "SELECT id FROM status_message WHERE action_name=:action_name AND controller_name=:controller_name";
        $command = $connection->createCommand($sql);
        $command->bindValue(":action_name", $action_name);
        $command->bindValue(":controller_name", $controller_name);
        $result = $command->queryOne();
        if($result == null){
                             
            return false;
        }
            return $result['id'];
                      
}

As you can see, it does a simple lookup via sql, when you hand in the action_name and controller name. We bind the parameters to the query and you can see this is very straightforward syntax.

If the check finds no record, it returns false, which we will use in an if statement to tell the method not to continue. In other words, find no record, then do nothing.

If the check is successful, and a record exists, it returns the id of the message, which I will hand in to other methods as $message_id. Very simple stuff.

Here are the other two methods that retrieve the message subject and message body, respectively:

public static function getMessageSubject($message_id)
{
    $connection = \Yii::$app->db;
        $sql = "SELECT subject FROM status_message WHERE id=:message_id";
        $command = $connection->createCommand($sql);
        $command->bindValue(":message_id", $message_id);
        $result = $command->queryOne();
        if($result == null){
                             
           return false;
        }
           return $result['subject'];
                      
}
            
public static function getMessageBody($message_id)
{
    $connection = \Yii::$app->db;
        $sql = "SELECT body FROM status_message WHERE id=:message_id";
        $command = $connection->createCommand($sql);
        $command->bindValue(":message_id", $message_id);
        $result = $command->queryOne();
        if ($result == null){
                             
           return false;
        }
           return $result['body'];
                      
}

In both cases, we give the message_id that we want and we get back the message subject or body that we need for the email. Later we will attach these results to our sendTheMail method, which will be part of our MailCall class that we are going to create. Don’t worry it sounds much more complicated than it actually is, it’s very simple stuff.

Just a note. I prefer to use sql for these kinds of tasks, when I’m doing these kinds of queries. You could alternatively elect to use Yii 2’s ActiveRecord methods to achieve the same results, you can consult the guide for that, if you wish.

Also note that we are using public static methods. This makes it easy to place the method inside another method without having to use the longer instantiation syntax. You will see what I mean in a minute or two.

Anyway, so now we have the record helpers that will help us return the data from the table. If you are following along in the book, add these methods to common\models\RecordHelpers.php. If you are not following along the book, you are free to place the methods wherever you wish, just make sure you have access to them and that they are namespaced correctly.

You can see we are working our way backwards from the data. Obviously, we will need a mail method to send the mail, once we have the bits of the message we need.

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