Neptune 1.0 Beta 3 Documentation: Email Notices

Neptune Information

Download from RIA Forge

Email Notices

One very common task for applications is to send out an email message when an event occurs. Ideally the content of this email message should be dynamic, but editable.

Fortunately, NoticeMgr is built for just this task. First, however, we need to do a bit of set up to get email working.

We'll start by adding configuration variables for email in _config/config.cfm:

setSetting("MailServer","mail.example.com");
setSetting("MailDefaultFrom","robot@example.com");
setSetting("MailDefaultTo","admin@example.com");
setSetting("MailerMode","Live");

The settings don't need to be named as above, but they do need to match the configurations that we will put in for Mailer.cfc in _config/components.cfm:

<component name="Mailer" path="com.sebtools.Mailer">
	<argument name="DataMgr" component="DataMgr" ifmissing="skiparg" />
	<argument name="MailServer" arg="MailServer" ifmissing="skipcomp" />
	<argument name="From" arg="MailDefaultFrom" ifmissing="skipcomp" />
	<argument name="To" arg="MailDefaultTo" ifmissing="skipcomp" />
	<argument name="mode" arg="MailerMode" />
</component>

The "ifmissing" attribute indicates what to do if the data is missing. The default is "error". A value of "skiparg" means to load the component without that argument. A value of "skipcomp" means to skip loading that component.

The "From" and "To" arguments for Mailer set default values for "From" and "To" which can be overridden. A mode of "Live" will send email whereas a mode of "Sim" will not. If DataMgr is passed in then Mailer will add a record to a log table for every email that it sends (even in "Sim" mode).

Now we also need to add our NoticeMgr component as well as a NoticeMgr argument to ContactOMatic:

<component name="NoticeMgr" path="com.sebtools.NoticeMgr">
	<argument name="DataMgr" component="DataMgr" ifmissing="skipcomp" />
	<argument name="Mailer" component="Mailer" ifmissing="skipcomp" />
</component>
<component name="ContactOMatic" path="contacts.model.ContactOMatic">
	<argument name="Manager" component="Manager" />
	<argument name="NoticeMgr" component="NoticeMgr" />
</component>

If a ProgramManager component has NoticeMgr passed in during initialization then it will call the "loadNotices" method. So, to add a notice to our Contact-O-Matic, we could use the following code:

<cf_NoticeMgr
	action="addText"
	Component="ContactOMatic"
	Name="Contact Saved"
	Subject="A Contact was Saved"
	DataKeys="ContactName"
>
A contact ("[ContactName]") was saved.
</cf_NoticeMgr>

An action of "addText" creates a text and an action of "addHTML" would create an HTML notice. In either case, the content between the tags will be set only if the notice doesn't already exist in the system so that it can be edited externally (see the "Notices" program for an example).

Now we can send out a notice every time that a contact is saved:

<cffunction name="saveContacts" access="public" returntype="string" output="no">
	
	<cfset var result = saveRecord(argumentCollection=arguments)>
	<cfset var sNotice = StructNew()>
	
	<cfif StructKeyExists(arguments,"ContactName")>
		<cfset sNotice["ContactName"] = arguments.ContactName>
		<cfset variables.NoticeMgr.sendNotice("Contact Saved",sNotice)>
	</cfif>
	
	<cfreturn result>
</cffunction>

Now an email will be sent every time that a contact is saved (assuming, of course, that the Contact Name is provided).

Next: Scheduled Tasks