Plugins can create and send emails. Using Blesta's built-in Emails model makes it super easy to send and log email communications. There are two distinct types of emails, template and custom.

Custom Emails

To create a custom email, simply invoke Emails::sendCustom() with the appropriate parameters. The subject and body you provide may contain Template Parsing syntax as well. Use the $tags parameter to pass in variables to be substituted within the email subject and body. Using this method will automatically log the email to the database. To attached the email to a specific client use the $options parameter to specify the 'to_client_id' and 'from_staff_id'. This will allow you to view and resend the message under the client's profile.

/plugins/my_plugin/controllers/admin_main.php
<?php
class AdminMain extends MyPluginController {

    public function index() {
        $this->uses(array("Emails"));

        $from = "no-reply@mydomain.com";
        $from_name = "My Company";
        $to = "tommy.callahan@yourdomain.com";
        $subject = "Subject of the email";
        $body = array(
            'text'=> "Hi {first_name},

This is your email."
        );
        $tags = array('first_name'=>"Tommy");

        // Send the custom email
        $this->Emails->sendCustom($from, $from_name, $to, $subject, $body, $tags);
    }
}
?>

Template Emails

Plugins are free to take advantage of the email template system in Blesta. Doing so will allow staff members to update the content of the emails you intend to send from within the Company > Emails section of the Company Settings. To create an email template use the EmailGroups::add() method. The $action parameter must be unique across all email templates, so prepending your plugin name is a good idea. For example, if your plugin is named FooBar then 'FooBar.notify_client' would make an excellent choice. As with custom emails, templates may also contain Template Parsing syntax.

/plugins/my_plugin/controllers/admin_main.php
<?php
class AdminMain extends MyPluginController {

    public function index() {
        $this->uses(array("Emails", "EmailGroups"));

        $group = array(
			'action' => "MyPlugin.custom_action",
			'type' => "staff",
			'plugin_dir' => "my_plugin",
			'tags' => "first_name,last_name"
		);

        // Add the custom group
		$group_id = $this->EmailGroups->add($group);


        $email = array(
			'email_group_id' => $group_id, 
			'company_id' => $this->company_id, 
			'lang' => "en_us", 
			'from' => "no-reply@mydomain.com", 
			'from_name' => "My Company",
            'subject' => "Subject of the email", 
			'text' => "Hi {first_name}, 
This is the text version of your email", 
			'html' => "<p>Hi {first_name},</p>
<p>This is the HTML version of your email</p>"
        );

        // Add an email to the group
		$this->Emails->add($email);

	}
}
?>

Once created, sending an email using your email template is very similar to sending a custom email, except you invoke Emails::send() and pass in the appropriate email $action.

/plugins/my_plugin/controllers/admin_main.php
<?php
class AdminMain extends MyPluginController {

    public function index() {

		...
		
		$this->uses("Emails");

		$tags = array('first_name' => "Tommy", 'last_name' => "Callahan");
		$options = array('to_client_id' => 1020);
		$this->Emails->send("MyPlugin.custom_action", $this->company_id, "en_us", "tommy.callahan@yourdomain.com", $tags, null, null, null, $options);
	}
}
?>
  • No labels