Creating Your Own Function for Sending Email

Coast LineCEO
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.c om',mailcc ='abc@wall .com',subj ect="#Subj ect#",Opti on="1")>


<cfset EmailNow = sendMail(mailto='abc@wal.c om',subjec t="#Subjec t#",body=" 1",content s="#your_c ustom_tabl e_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="" 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!
Coast LineCEO

Comments (0)

Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.