webCOMAND

Email Template Content Type

The Email Template content type is used to define an email message that can be used to send emails that follow a certain structure, potentially using cTemplate variables in parts of the subject and message that will be filled in with values specific to the recipient.

  • Subject - The email subject.
  • Message - The email message HTML.
  • Attachments - Embedded list of Files to attach to the email.
  • From - Reference to an Email Address the message should be sent from.
  • To - Reference list to Email Addresses the message should be sent to.
  • CC - Reference list to Email Addresses the message should be copied to.
  • BCC - Reference list to Email Addresses the message should be blind copied to.

Example

The following example will send an HTML email with plain-text alternative and custom headers and attachments based on an Email Template with cTemplate Variables.

use \io_comand_script\ctemplate;

$repo = \comand::repo();
$log = new \io_comand_log\log();
$contact = $repo->get_first('FROM Contact WHERE Name="Fay"');
$opts = [
    'repo' => $repo,
    'log' => $log,
    'context' => $contact
];

// Send an HTML email.
$mail = new \io_comand_email\mail();
$mail->from = 'No Reply <no-reply@webcomand.com>';
$mail->to = $contact->Name . ' <' . $contact->Email . '>';

// Get and process the Email Template Subject and Message in the context
// of the contact we loaded, so its fields can be accessed as variables.
$template = $repo->get_object_by_oid(123, 'EmailTemplate');
$mail->subject = ctemplate::run_ctemplate($template->Subject, $opts);
$mail->message_html = ctemplate::run_ctemplate($template->Message, $opts);
foreach($template->Attachments as $attachment) {
    $mail->attach($attachment->Filename, $attachment->Data());
}

$mail->message = htmlspecialchars(strip_tags($mail->message_html));
$mail->html(TRUE);
$mail->header('X-Email-Client', 'webCOMAND');

$mail->send();