attach and send from the web?
June 8, 2006 8:08 AM   Subscribe

Web development question: sending emails from a site with file attachments, can it be done?

Our customer wants us to build a web page that offers several pdf files for download, they also want to initiate the creation of an email message. Both of those things are pretty easy to do, but combining these tasks is proving to be problematic.

Essentially the user should be able to select any combination of documents, then click a button that creates an email with the selected document files attached.

Can this be done? I've tried looking for solutions on the web, but I can't find this particular issue. We are using ASP, but as you can tell, we aren't exactly experts with this...
posted by lilboo to Computers & Internet (9 answers total)
 
It's as easy as sending an email from the server, you just need to convert each attachment to base64 encoded text then include that text in the message. If you're using perl on the server then you can use the MIME::Base64 module to do the encoding. PHP has a base64_encode() feature to do the same.

As far as the format of the email message you need boundary separators (unique strings of text that say something like boundary="__nextpart_234wd") in the message to separate the message body from the encoded attachment sections.

Email yourself a test message from your own email client with two very small images attachmented then after recieving that email look at the raw text of the message and you'll see how the mime boundary separators are labeled and how the base64 encoding is placed in the message body. Use that email as a template for your own email sending application.
posted by StarForce5 at 8:21 AM on June 8, 2006


Best answer: You need to send your mail with CDO.

Set myMail=CreateObject("CDO.Message")
myMail.Subject="Sending email with CDO"
myMail.From="mymail@mydomain.com"
myMail.To="someone@somedomain.com"
myMail.TextBody="This is a message."
myMail.AddAttachment "c:\mydocuments\test.txt"
myMail.Send
set myMail=nothing

from this page
posted by derbs at 8:23 AM on June 8, 2006 [1 favorite]


I forgot you mentioned you're using ASP, so there's this ASP base64 encoding/decoding code
posted by StarForce5 at 8:25 AM on June 8, 2006


...debs answer looks easier. I'm not a ASP coder.
posted by StarForce5 at 8:27 AM on June 8, 2006


I honestly don't think you need to worry about the base64 encoding shennanigans... i think CDO will sort all that out for you.

I may be wrong though!
posted by derbs at 8:28 AM on June 8, 2006


Since the question's been answered, a couple of warnings:

1) Lots of email accounts have attachment-size limits

2) You'll be supporting a lot of people who "didn't get their PDF" because it got dumped in their spam folder

3) You'll need to build something that can deal with bounces (eg mailbox full, bad email address entered)
posted by Leon at 8:35 AM on June 8, 2006


Response by poster: This is great. Thank you SO much. I owe all three of you a drink or two....
posted by lilboo at 8:45 AM on June 8, 2006


Thank you, AskMeFi, for answering a question roughly thirty minutes before I was going to ask it. This place rocks.
posted by Lokheed at 1:10 PM on June 8, 2006


If I recall correctly CDO only supported a local SMTP server for sending the messages. If this isn't the case for your environment, you'll need to look into 3rd ASP mail components like ASPemail.
posted by mmascolino at 2:04 PM on June 8, 2006


« Older Music suggestions for a new fan of heartbreaking...   |   I "try too hard" when it comes to social... Newer »
This thread is closed to new comments.