Creating Your Own Function for Sending Email

Published on
8,389 Points
Last Modified:
Hi, I will be creating today a basic tutorial on how we can create a Mail Custom Function and use it where ever we want. The main advantage about creating a custom function is that we can accommodate a range of arguments to pass to the Function and use them to help generate the Message!

So here below is the basic code of how to create a custom mail function, and what we can achieve in doing so!

While it is very basic code, up until now it has been working Ok! Functionality can be enhanced or extended just by adding few more features depending upon the Coldfusion version you are using!
Hope you find this article of interest and feel free to use the code for your own purposes. If you do enhance or extend it, please post back here in the comments and let me know what you have done.

The Code is below!

By way of brief introduction to the function, a few explanations are first required. So this is how we will call the function...

If we are specifying the body Struct, we can skip all the other Attributes as we will sending our own custom build design to the fmail tag else you can use the default design of the Mail..

<cfset EmailNow = sendMail(mailto='abc@wal.com',mailcc='abc@wall.com',subject="#Subject#",Option="1")>


<cfset EmailNow = sendMail(mailto='abc@wal.com',subject="#Subject#",body="1",contents="#your_custom_table_content#")>

The main purpose of creating this kind of small UDF is suppose you want to have a custom Template layout for all your emails for a specific purpose, for example, like a newsletter. You might even end up with a couple of special purpose functions as a front before calling this function.

That way you can use it and just pass the relevant fields to cffunction function sendmail() and it will work, you do not have to write cfmail everytime and change its internal layout.

Also, this is a reusage UDF in which users can create a cfinclude for the sendMail() function and pass the relevant parameters to it and run it, the Code will work.
<cffunction access="public" name="sendMail" rturntype="string" output="true">
	<cfargument name="structform" default="" type="struct" required="true">
	<cfset var strResult = "">
	<cfmail to="#arguments.structform.mailto#" cc="#arguments.structform.mailcc#" from="abc@abc.com" subject="#arguments.structform.subject#" 
	type="html" priority="1">
	<cfif isDefined('arguments.structform.body') AND arguments.structform.body EQ 1>
	<table width="80%" align="center" cellspacing="1" cellpadding="2">
	<table align="center" width="90%">
		<cfif isDefined('arguments.structform.Option') AND arguments.structform.Option EQ 1>Testing</cfif>: <br>
		Date: #DateFormat(now(),'mm/dd/yyyy')#
		Your Details Go here
	<cfset strResult = "Email Send Successfully">
	<cfset str = "Error! #cfcatch.detail# #cfcatch.message#">
	<cflog file="error.log" text="#cfcatch#">
	<cfreturn strResult>

Open in new window

So we are all done, we have created a custom Mailing where we can just use the attributes and send emails, can add as many attributes and call them in the mail!

Featured Post

Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

Join & Write a Comment

From store locators to asset tracking and route optimization, learn how leading companies are using Google Maps APIs throughout the customer journey to increase checkout conversions, boost user engagement, and optimize order fulfillment. Powered …
Wrapper-1-Query. Use an Excel function to calculate a column for an Access query. Part 1. Shows a query in Access that has a calculated column with the results of an Excel worksheet function. See how to call a wrapper function from a query, and …

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month