Creating Your Own Function for Sending Email

Coast LineCEO
CERTIFIED EXPERT
Published:
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")>

or

<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 = "">
                      	<cftry>
                      	<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>
                      	#trim(paragraphFormat(arguments.structform.contents))#	
                      	<cfelse>	
                      	<table width="80%" align="center" cellspacing="1" cellpadding="2">
                      	<tr><td>
                      	<p>
                      	<table align="center" width="90%">
                      		<tr><td>
                      		<h2>
                      		<cfif isDefined('arguments.structform.Option') AND arguments.structform.Option EQ 1>Testing</cfif>: <br>
                      		Date: #DateFormat(now(),'mm/dd/yyyy')#
                      		</h2>
                      		</td></tr>
                      		<tr>
                      		<td>
                      		Your Details Go here
                      		<br>
                      		</td>
                      		</tr>
                      	</table>
                      	</p></td></tr>
                      	</table>
                      	</cfif>
                      	</cfmail>
                      	<cfset strResult = "Email Send Successfully">
                      	<cfcatch>
                      	<cfset str = "Error! #cfcatch.detail# #cfcatch.message#">
                      	<cflog file="error.log" text="#cfcatch#">
                      	</cfcatch>
                      	</cftry>
                      	<cfreturn strResult>
                      </cffunction>

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!
0
2,880 Views
Coast LineCEO
CERTIFIED EXPERT

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.