Download the EasyCFM.COM Browser Toolbar!
Sending multiple attachments with CFMAIL!

Sending email with multiple attachments with <cfmail>!

I get a lot of requests for demonstrations on how to send multiple attachments when sending email with <CFMAIL>. This tutorial will show you how to do just that. Let's begin:

The first I will need to explain is that to send an attachment, you'll need to use the tag <cfmailparam> within your <cfmail> tags. 

Ok, let's show an example of you would do a multi-attachment email system...

The first thing we need to so is to create a form where the end user will upload their files, we'll call this page "Mail_Form.cfm":

<!--- Mail_Form.cfm --->
<html>
     <head>
         <title>
Please enter your message!</title>
     </head>

     <body>
         <form action="Send_Email.cfm" method="post" enctype="multipart/form-data">
          <table width="500" border="0" align="center">
              <tr>
                  <td width=
"500" colspan="2">Please enter your email message:</td>
              </tr>
              <tr>
                  <td width=
"250">To:</td>
                  <td width=
"250"><input type="text" name="to_addr" value=""></td>
              </tr>
              <tr>
                  <td width=
"250">Subject:</td>
                  <td width=
"250"><input type="text" name="subject" value=""></td>
              </tr>
              <tr>
                  <td width=
"250" valign="top">Message</td>
                  <td width=
"250"><textarea name="message" rows="5" cols="35"></textarea></td>
              </tr>
              <tr>
                  <td width=
"250">Attachment #1:</td>
                  <td width=
"250"><input type="file" name="attachment_1" value=""></td>
              </tr>
              <tr>
                  <td width=
"250">Attachment #2:</td>
                  <td width=
"250"><input type="file" name="attachment_2" value=""></td>
              </tr>
              <tr>
                  <td width=
"250">Attachment #3:</td>
                  <td width=
"250"><input type="file" name="attachment_3" value=""></td>
              </tr>
              <tr>
                  <td width=
"250">&nbsp;</td>
                  <td width=
"250"><input type="submit" name="Send_ Email" value="Send Email"></td>
              </tr>
          </table>

         </form>
     </body>
</html>

That will allow the user to upload up to 3 attachments and send out an email to a specified email address. The next thing we need to create is a file called "Send_Email.cfm". This is the file that will do all the work ;) Let's create it!

<!--- Send_Email.cfm --->
<!--- First make sure that the user uploaded attachments --->
<cfif FORM.attachment_1 neq "">
       <!--- first actually upload the file --->
       <cffile action="upload"
                 filefield=
"attachment_1"
                 destination=
"d:\uploads\"
                 nameconflict=
"Makeunique">
        <!--- now create a temporary holder for the attachment later on --->
        <cfset attachment_local_file_1 = "d:\uploads\#file.serverfile#">
</cfif>

Now repeat the same process for the second and third attachment:

<cfif FORM.attachment_2 neq "">
       <!--- first actually upload the file --->
       <cffile action="upload"
                 filefield=
"attachment_2"
                 destination=
"d:\uploads\"
                 nameconflict=
"Makeunique">
        <!--- now create a temporary holder for the attachment later on --->
        <cfset attachment_local_file_2 = "d:\uploads\#file.serverfile#">
</cfif>
<cfif FORM.attachment_3 neq
"">
       <!--- first actually upload the file --->
       <cffile action="upload"
                 filefield=
"attachment_3"
                 destination=
"d:\uploads\"
                 nameconflict=
"Makeunique">
        <!--- now create a temporary holder for the attachment later on --->
        <cfset attachment_local_file_3 = "d:\uploads\#file.serverfile#">
</cfif>

Ok, you have now upload the files to the server, now let's send out the email with the attachments:

<cfmail from="me@mysite.com" to="#form.to_addr#" subject="#subject#" server="mail.yoursite.com" port="25">
    #message#

   <cfsilent>
     <!--- Note that I used the <CFSILENT> tag, this will kill the white space in this area so your email is not cluttered with white space. --->
     <cfif FORM.attachment_1 neq "">
        <cfmailparam file=
"#attachment_local_file_1#">
     </cfif>
     <cfif FORM.attachment_2 neq
"">
        <cfmailparam file=
"#attachment_local_file_2#">
     </cfif>
     <cfif FORM.attachment_3 neq
"">
        <cfmailparam file=
"#attachment_local_file_3#">
     </cfif>
   </cfsilent>
</cfmail>

That's pretty much it, this will send out the email and apply as many attachments as you want the email message to send out!

Questions? Comments? Email Me...

All ColdFusion Tutorials By Author: Pablo Varando