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...

About This Tutorial
Author: Pablo Varando
Skill Level: Beginner 
 
 
 
Platforms Tested: CF5
Total Views: 215,387
Submission Date: October 11, 2002
Last Update Date: June 05, 2009
All Tutorials By This Autor: 47
Discuss This Tutorial
  • I get error message << OLE Object: Picture (Device Independent Bitmap) >> when attached and send the mail... Any one can help?

  • Ok i got this program to work, kind of. It will send a message with no attachements, but after i browse for an attachment, and then click send.. i get the following error... Variable ATTACHMENT_LOCAL_FILE_1 is undefined. The error occurred in D:\inetpub\vhosts\gcextreme.com\httpdocs\CFMAIL\Send_Email.cfm: line 10 8 : 9 : 10 : 11 : 12 : LINE 10 is in BOLD.. point out that that is an error?? Any help would be appreciated.. email me at gc_extreme@hotmail.com www.gcextreme.com

  • Thanks Pablo - Once again you made something I thought was going to be hard - easy.

  • I made this form and am currently working on a fix where each time someone uses the form, the action page checks the server to see if there are any files at all in the specified folder besides the one just uploaded. If there are, delete them... so basically each next person using the form deletes the previous person's attachment if they left one/some.

  • I was able to customize and use this lesson within half an hour. It was very easy to follow and use, thanks!

  • Method Not Allowed The requested method POST is not allowed for the URL /formularz/Send_Email.cfm. any solutions?

  • Well, that was good. On my site I have a confirmation page before the email is sent. I need to make the filename unique in case there are two people at the same time uploading two different files with the same name. If, on the confirmation page, the user selects the back button, how do I delete the file just uploaded? The current button is using javascript:history.back(); for the onclick handler. What can I do to make it also delete the recently uploaded file. I don't want to have somebody click back a couple of times, leaving a file that doesn't need to be there.

  • How do

  • YOu cannot delete them after sending the email as the file will not be there when the email actually goes out. (CF can take a bit to send). so I would suggest a scheduled tasks that reutnrs files older then today and delete them then.

  • From the looks of the example, the form is uploading multiple files and then emailing them. These files are now going to be left on the server for no purpose. Can they be immedidately deleted after sending the email?

Advertisement


Website Designed and Developed by Pablo Varando.