Loftx
Loftx

Reputation: 1792

Changing filename of attachment in CFMail

I'm trying to use ColdFusion to send out emails containing attachments 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 its 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 separately from the file?

Upvotes: 5

Views: 4067

Answers (6)

Dungeon Hunter
Dungeon Hunter

Reputation: 20603

ColdFusion 2016 added this new attribute called filename to override the filename specified in the file attribute. A sample example is below

<cfmail from="[email protected]" subject="filename test" to="[email protected]" username="[email protected]" password="password" server="localhost" spoolenable="false">
    <cfmailparam file="c:\Book2.xlsx" filename="Offers.xlsx">
Check out the new offers sheet
</cfmail>

Now when the mail is sent to the user he/she will see this as Offers.xslx instead of Book2.xslx. More info at https://tracker.adobe.com/#/view/CF-4019518

Upvotes: 3

Tom Chiverton
Tom Chiverton

Reputation: 678

Since CF8 you can use file and content as per : http://www.bennadel.com/blog/1220-coldfusion-cfmailparam-s-new-content-attribute-is-awesome.htm

<cfmailparam
    file="someNiceName.doc"
    content="#fileRead( yourNastyFileName.doc )#"
/>

Upvotes: 1

Chris
Chris

Reputation: 91

You can try adding:

<cfmailparam 
file="#actual_file_name#" 
disposition="attachment; filename=""#changed_file_name#""">

The multiple quotes are deliberate... I they allow spaces in the file name.

Upvotes: 9

Sam Farmer
Sam Farmer

Reputation: 4118

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

Upvotes: 1

Vincent Buck
Vincent Buck

Reputation: 17132

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.

Upvotes: 4

Ryan Guill
Ryan Guill

Reputation: 13896

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.

Upvotes: 1

Related Questions