2

I'm sending an email using the cfmail tag, and trying to attach a PDF file from a URL:

<cfmail to="me@mydomain.com" from="you@yourdomain.com" subject="Subject" type="html">
   <cfmailparam file="http://myfilelocation/pdfFile.pdf">
   Body of the email
</cfmail>

However, this is not working. Without the cfmailparam tag, the email sends successfully. If I go to http://myfilelocation/pdffile.pdf, I see the PDF document that I'm trying to attach. Am I doing something wrong? How can I attach a PDF document to an email from a URL?

Binod Kalathil
  • 1,939
  • 1
  • 30
  • 43
froadie
  • 79,995
  • 75
  • 166
  • 235
  • 2
    If you sending an HTML email, why not just link to the PDF directly? – Paul Perigny Jul 11 '11 at 15:28
  • @Paul Perigny - brilliant idea :) Don't know why we didn't think of that. Just ran the idea past my boss and it was approved :) Thanks! (If you post it as an answer I can accept it) – froadie Jul 12 '11 at 07:55

4 Answers4

3

If your are send HTML email, why not just link to the PDF directly? This makes for smaller emails and gives you a chance to update the PDF even after it is sent out.

Paul Perigny
  • 973
  • 5
  • 12
2

The cfmailparam file should point to a location on your server. This file is then attached to the email:

<cfmail to="me@mydomain.com" from="you@yourdomain.com" subject="Subject" type="html">
<cfmailparam file="d:\websites\mysite\resources\pdf1.pdf">
   Body of the email
</cfmail>
Antony
  • 3,781
  • 1
  • 25
  • 32
1

You can use CFHTTP to retrieve your file to your server and getTempFile() || getTempDirectory() to temporary stock this file and finally use CFMAILPARAM to attach this file.

Edit:

<cfset tempFile = getTempDirectory(getTempFile()) />
<cfhttp url="http://myfilelocation/pdfFile.pdf" method="get" file="#tempFile#"></cfhttp>

<cfmail to="me@mydomain.com" from="you@yourdomain.com" subject="Subject" type="html">
   <cfmailparam file="#tempFile#">
   Body of the email
</cfmail>

Not tested

LarZuK
  • 746
  • 1
  • 8
  • 16
0

Your code will work fine for text files only. For other files we need conversion of data. See below:

<cfhttp method="get" url="http://myfilelocation/pdfFile.pdf">
<!---conversion--->
<cfif IsSimpleValue(cfhttp.Filecontent)>
    <cfset content = cfhttp.Filecontent>
<cfelse>
    <cfset content = cfhttp.Filecontent.toByteArray()>
</cfif>

And cfmailparam like:

<cfmailparam file="#fileNameOfYourChoice#" type="#cfhttp.Mimetype#" content="#content#" />

This works with special URLs(eg: http://myfilelocation/pdfFile.pdf?querystring=1 or http://myfilelocation/notPdfFileName/) and also allow us to name the file as we need

Binod Kalathil
  • 1,939
  • 1
  • 30
  • 43