I have been wanting to update my ColdFusion skills writing code in full cfscript. So, one of the first things that I did was write a method to send me email from a contact form for my UX site.
To do this I created the following CFC. Most of which is written on page 22 of the 25Meg PDF file Developing Applications ADOBE� COLDFUSION 10.
component displayname="contactUs" hint="Allows users to send email." output="false" {
remote any function setEmailMessage(
required string fromName,
required string fromEmail,
required string message,
string subject = "Contact Form",
string toEmail = "to@fake.com",
string failToEmail = "fail.to@fake.com",
numeric priority = 2,
array attachements,
required string smtp,
required string smtpUsername,
required string smtpPassword
)
returnformat='json' {
/***** BEGIN DEFAULT RETURN VALUE *****/
// Pre-populate the JSON structure that is to be returned on failure or success.
// This can be adjusted to meet your needs.
local.returnValue = {
"statusCode" : 1000,
"requestID" : createUUID(),
"permissions" : "VALED",
"data" = {
"message" : "Success"
},
"eventName" : "/com/modules/contactUs.cfc",
"requestParams" : arguments
};
/***** END DEFAULT RETURN VALUE *****/
/***** BEGIN EMAIL PROCESS *****/
try {
// Create an instance of the mail object
mail=new mail();
// Set it's properties
mail.setSubject( arguments.subject );
mail.setTo( arguments.toEmail );
mail.setFrom( arguments.fromEmail );
if ( arguments.ccEmail !== "~") {
mail.setCC( arguments.ccEmail );
}
if ( arguments.bccEmail !== "~") {
mail.setBCC( arguments.bccEmail );
}
if ( arguments.failToEmail !== "~") {
mail.setFailTo( arguments.failToEmail );
}
// Set the priority (1, 2 or 3 - High to Low)
mail.addParam(
name="X-Priority",
value= arguments.priority
);
// Add email body content in text format
mail.addPart(
type="text",
charset="utf-8",
wraptext="72",
body=REReplace( arguments.message, "<[^>]*>", "", "All" )
);
// Add email body content in HTML formats
mail.addPart(
type="html",
charset="utf-8",
body=arguments.message
);
// Set SMTP account access
mail.setServer( arguments.smtp );
mail.setUsername( arguments.smtpUsername );
mail.setPassword( arguments.smtpPassword );
// Send the email message
mail.send();
// Update JSON status message
local.returnValue.data.message = "Email Sent";
}
catch (any e) {
local.returnValue.statusCode = 5000;
local.returnValue.data.message = e.message;
}
/***** END EMAIL PROCESS *****/
/***** BEGIN JSON CLEAN UP OF ARGUMENTS TO RELAY BACK THE USER *****/
StructDelete(local.returnValue.requestParams, "smtp");
StructDelete(local.returnValue.requestParams, "smtpUsername");
StructDelete(local.returnValue.requestParams, "smtpPassword");
StructDelete(local.returnValue.requestParams, "failToEmail");
if ( local.returnValue.requestParams.ccEmail == "~" ) {
StructDelete(local.returnValue.requestParams, "ccEmail");
}
if ( local.returnValue.requestParams.bccEmail == "~" ) {
StructDelete(local.returnValue.requestParams, "bccEmail");
}
/***** END JSON CLEAN UP OF ARGUMENTS TO RELAY BACK THE USER *****/
return local.returnValue;
}