This post refers to DotKernel 1, based on Zend Framework 1.
If you are looking for DotKernel 3 related posts, go here.

Alerts (or Dot_Alert’s) are e-mails usually sent to the site developers, these messages are sent with mail() therefore you shouldn’t use them to send regular mail. Alerts should only notify you as a developer: “Hey, something’s wrong here, you might want to know this!

In this article you will find out how to use the Alerts system in DotKernel, we will also go through an existing example so this can be understood easier.

The Dot_Alert class resembles with Dot_Email, the alerts, like a mail message, have at least the sender, a subject, a destination and a message and it can also be sent.

In DotKernel we use the Alerts for notifying the developer that an email was not sent successfully.

In this case we kept the message in the dots.xml file

<variable option="global">
	<alertMessages>
		<email>
			<subject> SMTP Error on {SITE_NAME} </subject>
			<message>
SMTP Error on {SITE_NAME}
We were unable to send SMTP email
---------------------------------
Caught exception: {E_CLASS} 
Message:  {E_MESSAGE}
To Email: {TO_EMAIL}
From Email: {FROM_EMAIL}
Date: {DATE_NOW}
---------------------------------
			</message>
		</email>
	</alertMessages>
</variable>

If you’re not familiar with the dots.xml, you should see this article.

First the message will be fetched from the xml file, we already have it in $this->option

$subject = $this->option->alertMessages->email->subject;
$message = $this->option->alertMessages->email->message;

Second, we get the destination recipient (in this case the developers e-mail addresses)

$devEmails = explode(',', $this->settings->devEmails);

As you can see the Alert messages contains {VARIABLES} called details in the Alert system. Now we will prepare the details:

$details = array(
    'e_class' => get_class($e),
    'site_name' => $this->seoOption->siteName,
    'site_url' => $registry->configuration->website->params->url, 
    'e_message' => $e->getMessage(),
    'to_email' => implode(',', $this->_to),
    'from_email' => $this->getFrom(),
    'date_now' => date('F dS, Y h:i:s A'),
);

Note that $e is a caught exception, this exception is thrown when the e-mail send process fails.
Now that we have it all, let’s create an alert:

$alert = new Dot_Alert();

This is just an empty alert, we will now set the sender, the subject and the message, the sender is set on sending

$alert->addHeader( "From: " . $this->settings->siteEmail);
$alert->addHeader( "Reply-To:" . $this->settings->siteEmail );
$alert->addHeader( "X-Mailer: PHP/" . phpversion() ) ;
$alert->setTo($devEmails); 
$alert->setSubject($subject);
$alert->setContent($message);

Our message doesn’t look that good, the setDetails method will replace our {VARIABLES} within subject and message with actual data

$alert->setDetails($details);

Everything is great, we can now send our alert:

$alert->send();

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>