views:

568

answers:

3

Hi there,

I'm trying to use coldfusion to send out emails containing attachements stored on our server.

To manage these attachments we call them 1.jpg, 2.doc, ... n.ext ... where n is a key in a database where we hold other information about the file such as it's original filename.

I can use the code

<cfmailparam file="c:\path\1.doc">

to specify the file, but it is then attached to the email as 1.doc. Is there anyway I can override this and specify my own filename septately from the file?

Thanks,

Tom

+1  A: 

currently the only way to do this would be to use cffile and make a copy of the file in a temporary directory, rename it and then attach that. Then you would just want to delete the file once you are finished. I don't think there is a way to attach a file but call it something different when attaching to an email.

Ryan Guill
+3  A: 

Ryan's suggestion is probably the easiest solution. If you're on CF 8.01 you can make use of cfmailparam's new remove attribute. After you've renamed your attachments with cffile and passed them to cfmailparam, Coldfusion will delete them from disk for you once they have been processed by the mail spool:

<cfmailparam file="#File_path#" remove="true" />

(Before version 8.01, you had to make sure that your app didn't delete the temp files before Coldfusion's mail spool was finished with them.)

Alternatively you could call Coldfusion's underlying Java and construct your email message with attachments from memory only, with whatever names you fancy. Check out Dan Switzer's blog for an example on CF 7.02.

Vincent Buck
This is pretty much what I ended up doing but rather than writing the file to disk and deleting it I used the content attribute in CF8 to deal with most of this by just reading in the file to a binary variable and then letter cfmailparam write it out.
Loftx
I should note, I was really looking for a solution that didn't involve reading the file in (and possibly writing it out), as I expect this can be quite a drain on coldfusion for large files, so some way of renaming the attachment would still be preferred.
Loftx
+1  A: 

If you are running 8.0.1 (do cfdump var="#server#" to find out) then this might make your life a little easier:

  • The cfmail and cfmailparam tags now have a remove attribute that tells ColdFusion to remove any attachments after successful mail delivery.
  • The cfmailparam tag now has a content attribute that lets you send the contents of a ColdFusion variable as an attachment. To do so, specify the variable in # signs as the content attribute value, as in the following example: The file attribute specifies the file name to include in the mail header, not a file on the ColdFusion system

From:

http://www.adobe.com/support/documentation/en/coldfusion/801/cf801releasenotes.pdf

Sam Farmer