开发者

How can I attach a file to an email from a url?

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 at开发者_如何转开发tach. Am I doing something wrong? How can I attach a PDF document to an email from a URL?


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.


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>


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜