Search code examples
emailcoldfusioncoldfusion-9cfmail

How do you send an email with an attachment using cfscript in CF9?


How do you do the following using cfscript?

<cfmail
   from="[email protected]"
   to="[email protected]"
   subject="Attachments">
   <cfmailparam name="Reply-To" value="[email protected]">
   Some message goes here...
   <cfmailparam file="c:\files\readme.txt">
   <cfmailparam file="c:\files\logo.gif">
</cfmail>

Using the following causes a "function keyword is missing in FUNCTION declaration" error:

mail subject="Test Email" from="[email protected] to="[email protected]" server="localhost" {
    mailpart file="#ExpandPath('readme.txt')#";
}

Solution

  • Here's a how-to explaining what needs to be done: http://simonbingham.posterous.com/sending-email-using-cfscript-in-coldfusion-9

    // Create an instance of the mail object
    mail=new mail();
    
    // Set it's properties
    mail.setSubject( "Sample Email" );
    mail.setTo( "[email protected]" );
    mail.setFrom( "[email protected]" );
    mail.setCC( "[email protected]" );
    mail.setBCC( "[email protected]" );
    
    // Add an attachment
    mail.addParam( file="C:\foo.txt" );
    
    // Add email body content in text and HTML formats
    mail.addPart( type="text", charset="utf-8", wraptext="72", body="This is a test message." );
    mail.addPart( type="html", charset="utf-8", body="<p>This is a test message.</p>" );
    
    // Send the email
    mail.send();