Changing filename of attachment in CFMail
Asked Answered
M

6

5

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?

Marybethmaryellen answered 23/4, 2009 at 14:41 Comment(0)
D
3

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

Disinfest answered 1/8, 2017 at 11:40 Comment(0)
A
9

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.

Alfieri answered 14/1, 2012 at 23:18 Comment(2)
I searched for something similar to the original question and this is what I was looking for. Thanks.Chante
This seems to only work for one attachment. Additional attachments on the email have their real filename.Clawson
R
4

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.

Rebec answered 23/4, 2009 at 15:37 Comment(2)
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.Marybethmaryellen
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.Marybethmaryellen
D
3

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

Disinfest answered 1/8, 2017 at 11:40 Comment(0)
E
1

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.

Epiphenomenon answered 23/4, 2009 at 14:49 Comment(0)
A
1

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

Alcheringa answered 23/4, 2009 at 16:37 Comment(0)
D
1

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 )#"
/>
Donyadoodad answered 12/5, 2014 at 15:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.