User Guide Cancel

mail

 

Note:

In ColdFusion (2018 release), script functions implemented as CFCs are deprecated in ColdFusion.

Description

Used to sends an e-mail message, that optionally contains query output, using an SMTP server.

Syntax

Mode

Syntax

Creating the service

new mail() or createObject("component", "mail")

Initializing the attributes

Any one of the following:

  • mailService=new mail(_attribute-value_pair_)
  • mailService.setAttributes(_attribute-value_pair_)
  • mailService.set_AttributeName_(attribute_value)
  • mailService.send(attribute-value_pair)

Executing the service action

mailService.send(_attribute-value_pair_)

Properties

from

to

subject

bcc

cc

charset

debug

failto

group

groupcasesensitive

mailerid

maxrows

mimeattach

password

port

priority

query

replyto

server

spoolenable

startrow

timeout

type

username

useSSL

useTLS

wraptext

remove

body

 

 

 

All attributes supported by the tag cfmail can be used as attribute-value pairs. For example,

<cfmail from="#form.mailFrom#">

can be used as

mailerService.setFrom(form.mailFrom);

See also

cfmailFunction summary

History

ColdFusion 9: Added this function.

Methods

  • addParam

    Description

    Used to add cfmailparam tags. For example, to attach a file or add a header to an e-mail message.

    Syntax

    mailService.addParam(attribute-value pair)

    Returns

    Nothing

    Arguments

    All attributes supported by the cfmailparam tag can be used as attribute-value pairs.

     

  • addPart

    Description

    Used to add cfmailpart tags. For example, one part of a multipart e-mail message.

    Syntax

    mailService.addPart(attribute-value pair)

    Returns

    Nothing

    Arguments

    All attributes supported by the cfmailpart tag can be used as attribute-value pairs.

     

  • send

    Description

    Used to invoke the mail service to send an e-mail message.

    Returns

    Nothing

    Syntax

    mailService.send(attribute-value pair)

    Arguments

    All attributes supported by the cfmail tag.

     

  • setAttributes

    Description

    Sets attributes for the mail function.

    Returns

    Nothing

    Syntax

    mailService.setAttributes (attribute-value pair)

    Arguments

    All attributes supported by the cfmail tag.

     

  • getAttributes

    Description

    Gets attributes that were set for the mail function.

    Returns

    Returns a struct with all or some of the attribute values.

    Syntax

    mailService.get_Attributes_ (attributelist)

    Arguments

    A comma-separated list of attributes. If no list is specified, all defined attributes are returned.

     

  • clearAttributes

    Description

    Removes all attributes added for the mail function.

    Returns

    Nothing

    Syntax

    mailService.clearAttributes(attribute_list)

    Arguments

    A comma-separated list of attributes.

     

  • clearParams

    Description

    Removes cfmailparam tags that were added using the addParam method.

    Returns

    Nothing

    Syntax

    mailService.clearParams()

    Arguments

    None

     

  • clearParts

    Description

    Removes cfmailpart tags that were added using the addPart method.

    Returns

    Nothing

    Syntax

    mailService.clearProcResults()

    Arguments

    None

     

  • clear

    Description

    Removes all attributes, cfmailparam tags, and cfmailpart tags that were added using the methods addParam and addPart.

    Returns

    Nothing

    Syntax

    mailService.clear()

    Arguments

    None

     

Usage

This function corresponds to the tag cfmail. For usage details, see the Usage section for cfmail.

Example

<h3>Sending mail in cfscript</h3>
<cfscript>
/* create mailer service */
mailerService = new mail();
if(IsDefined("form.mailto"))
{
if(form.mailto is not "" AND form.mailfrom is not "" AND form.Subject is not ""
and form.attachment is not "")
{
savecontent variable="mailBody"{
WriteOutput("This message was sent by an automatic mailer built with cfmail:
= = = = = = = = = = = = = = = = = = = = = = = = = = =" & "<br><br>" & form.body);
}
/* set mail attributes using implicit setters provided */
mailerService.setTo(form.mailto);
mailerService.setFrom(form.mailFrom);
mailerService.setSubject(form.subject);
mailerService.setType("html");
/* add mailparams */
mailerService.addParam(file=expandpath(form.attachment),type="text/plain",remove
=false);
/* send mail using send(). Attribute values specified in an end action like "send" will not persist after the action is performed */
mailerService.send(body=mailBody);
writeoutput("<h3>Thank you</h3>" & "<p>Thank you, " & mailfrom & "<br>" & "Your message, " & subject & ", has been sent to " & mailto & "</p>");
}
}
</cfscript>
<p>
<form action = "mail1.cfm" method="POST">
<table>
<tr>
<td>TO</td>
<td><input type = "Text" name = "MailTo"></td>
</tr>
<tr>
<td>FROM</td>
<td><input type = "Text" name = "MailFrom"></td>
</tr>
<tr>
<td>SUBJECT</td>
<td><input type = "Text" name = "Subject"></td>
</tr>
<tr>
<td>ATTACHMENT</td>
<td><input type = "file" name = "attachment"></td>
</tr>
</table>
<hr>
MESSAGE BODY:
<br>
<textarea name ="body" cols="40" rows="5" wrap="virtual"></textarea>
<!--- Establish required fields. --->
<input type = "hidden" name = "MailTo_required" value = "You must enter a recipient">
<input type = "hidden" name = "MailFrom_required" value = "You must enter a sender">
<input type = "hidden" name = "Subject_required" value = "You must enter a subject">
<input type = "hidden" name = "Body_required" value = "You must enter some text">
<input type = "hidden" name = "attachment_required" value = "You must select a file">
<p><input type = "Submit" name = ""></p>
</p>
</form>

 

Get help faster and easier

New user?