Creating Your Own Function for Sending Email

AID: 5104
  • Status: Published

940 points

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>
                                    
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:

Select allOpen 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!
Asked On
2011-03-25 at 19:28:42ID5104
Tags

Coldfusion

,

CFML

,

Coldfusion Server

Topic

Cold Fusion Markup Language

Views
426

Comments

Add your Comment

Please Sign up or Log in to comment on this article.

Join Experts Exchange Today

Gain Access to all our Tech Resources

Get personalized answers

Ask unlimited questions

Access Proven Solutions

Search 3.2 million solutions

Read In-Depth How-To Guides

1000+ articles, demos, & tips

Watch Step by Step Tutorials

Learn direct from top tech pros

And Much More!

Your complete tech resource

See Plans and Pricing

30-day free trial. Register in 60 seconds.

Loading Advertisement...

Top ColdFusion Language Experts

  1. _agx_

    268,859

    Guru

    2,000 points yesterday

    Profile
    Rank: Genius
  2. gdemaria

    184,144

    Guru

    1,800 points yesterday

    Profile
    Rank: Genius
  3. SidFishes

    87,866

    Master

    2,000 points yesterday

    Profile
    Rank: Genius
  4. myselfrandhawa

    58,872

    Master

    0 points yesterday

    Profile
    Rank: Guru
  5. dgrafx

    36,068

    0 points yesterday

    Profile
    Rank: Sage
  6. pravinasar

    26,318

    0 points yesterday

    Profile
    Rank: Genius
  7. leakim971

    19,600

    0 points yesterday

    Profile
    Rank: Genius
  8. maestropsm

    15,340

    0 points yesterday

    Profile
    Rank: Guru
  9. ansudhindra

    13,300

    0 points yesterday

    Profile
    Rank: Wizard
  10. Zvonko

    12,264

    0 points yesterday

    Profile
    Rank: Genius
  11. micropc1

    10,200

    0 points yesterday

    Profile
    Rank: Master
  12. COBOLdinosaur

    9,800

    200 points yesterday

    Profile
    Rank: Genius
  13. TechHelpr08210

    9,700

    0 points yesterday

    Profile
  14. cyberdyne_dev

    9,500

    0 points yesterday

    Profile
  15. srikanthmadishetti

    8,601

    0 points yesterday

    Profile
    Rank: Guru
  16. brijeshchauhan

    8,468

    0 points yesterday

    Profile
    Rank: Guru
  17. Proculopsis

    8,200

    0 points yesterday

    Profile
    Rank: Sage
  18. kaufmed

    7,468

    0 points yesterday

    Profile
    Rank: Genius
  19. JohnHowlett

    6,000

    0 points yesterday

    Profile
  20. DaveBaldwin

    5,750

    0 points yesterday

    Profile
    Rank: Genius
  21. digicidal

    4,600

    0 points yesterday

    Profile
    Rank: Guru
  22. erikTsomik

    4,500

    0 points yesterday

    Profile
    Rank: Sage
  23. HainKurt

    4,500

    0 points yesterday

    Profile
    Rank: Genius
  24. mplungjan

    4,400

    0 points yesterday

    Profile
    Rank: Savant
  25. sedgwick

    4,000

    0 points yesterday

    Profile
    Rank: Genius

Hall Of Fame