How can I send an automated email reply with attachment when a user submits a web form?

Wil Forbis March 22, 2013
Ads by Google

I’m wondering if the following scenario is possible:

A user visits a website and submits a web form. The form is parsed by an app or web service (perhaps something like and it pulls an email address from the submitted web form data and sends an email with an attachment to that email address. Basically this is a web form by which people can say “I would like you to send an email attachment to the email address I have submitted.”

I’m mostly wondering if this could be accomplished via existing web tools and apps (again, maybe glued together with but I’d also be interested in it could be done as a Perl script or some CGI bin chicanery on a server. What’s not an option is something running on a PC (e.g. batch job).

Thanks in advance!


  1. Paul Pruitt
    March 23, 2013 at 3:17 pm

    Maybe try and see if you can with the WYSIWYG create a form that does what you want.

  2. Justin
    March 22, 2013 at 9:27 pm

    I tried to post the code but the comments section is screwing it up. please visit the below website for code

  3. Justin
    March 22, 2013 at 9:26 pm

    As the previous person said there are multiple ways. My suggestion is to use PHP. Please see the below code :

    function mail_attachment($filename, $path, $mailto, $from_mail, $from_name, $replyto, $subject, $message) {
    $file = $path.$filename;
    $file_size = filesize($file);
    $handle = fopen($file, "r");
    $content = fread($handle, $file_size);
    $content = chunk_split(base64_encode($content));
    $uid = md5(uniqid(time()));
    $name = basename($file);
    $header = "From: ".$from_name." rn";
    $header .= "Reply-To: ".$replyto."rn";
    $header .= "MIME-Version: 1.0rn";
    $header .= "Content-Type: multipart/mixed; boundary="".$uid.""rnrn";
    $header .= "This is a multi-part message in MIME format.rn";
    $header .= "--".$uid."rn";
    $header .= "Content-type:text/plain; charset=iso-8859-1rn";
    $header .= "Content-Transfer-Encoding: 7bitrnrn";
    $header .= $message."rnrn";
    $header .= "--".$uid."rn";
    $header .= "Content-Type: application/octet-stream; name="".$filename.""rn"; // use different content types here
    $header .= "Content-Transfer-Encoding: base64rn";
    $header .= "Content-Disposition: attachment; filename="".$filename.""rnrn";
    $header .= $content."rnrn";
    $header .= "--".$uid."--";
    if (mail($mailto, $subject, "", $header)) {
    echo "mail send ... OK"; // or use booleans here
    } else {
    echo "mail send ... ERROR!";

    $my_file = "";
    $my_path = $_SERVER['DOCUMENT_ROOT']."/your_path_here/";
    $my_name = "Olaf Lederer";
    $my_mail = "";
    $my_replyto = "";
    $my_subject = "This is a mail with attachment.";
    $my_message = "Hallo,rndo you like this script? I hope it will help.rnrngr. Olaf";
    mail_attachment($my_file, $my_path, "", $my_mail, $my_name, $my_replyto, $my_subject, $my_message);

    PS: the credit for the code goes to the dude who wrote on this website :

  4. Rob Hindle0
    March 22, 2013 at 2:27 pm

    There are several ways of doing that.
    A program on the server is one way, the script that accepts the form input probably just sends you an email. It could extract senders address and send them something - an email possibly with attachment. Two things to think about there. First is that people could enter someone else's email address on your form. Spammers do this with forms that send a copy of the form input to the email address they put on the form as theirs, that way the spam appears to have come from you... Worst case is when the form allows them to enter multiple addresses, but even without that they can have a robot program feeding the form.
    That doesn't rule out this approach but the form should be super-secure with loads of validation to minimise the risk of it becoming a spam relay. Even if they can't send their spammy message using your system they may run intensive tests to try to find a way to exploit the form or may just send "nuisance" messages - i.e. send your automated response to third parties to annoy them or to clog up their mailbox.
    The second aspect is that the coding to send attachments with email can be a bit tricky. If you need to use MIME then the problem is that some recipient email clients are very fussy about the precise format.'s PHP formmail does offer an autorespond capability (not sure about that allowing an attachment), the script is free and very versatile but setup can get quite challenging - they are hoping to get you to buy their commercial service.

    You mentioned PERL - same considerations apply. Perl is a bit less demanding on the server but PHP is an easier language to deal with.

    If usage levels are high then automation may be essential - but bear in mind the spam relay risk.

    You could use a Gmail canned response (an add-in you'll find under settings>labs). Do it manually unless usage is high in which case you can create a filter, triggered by the incoming subject line to auto-send the canned response (and make sure the form has strong validation). If you are handling large numbers of emails may fall foul of Gmail's daily usage quota limits (upgrade to commercial gmail to increase the limits).

  5. Clinton Dcruz
    March 22, 2013 at 12:11 pm

    If you need the link to the website it's :

  6. Clinton Dcruz
    March 22, 2013 at 12:05 pm

    Well, there are many applications but according to me the best one is MailChimp. MailChimp is at a point where it is really being evolved at the optimum level. I have used it in my blog and I'm quite happy how it works.. You have the freedom, freedom of chosing the mailing list to deliver and how the work, you can choose different built-in templates for your mails. Its free for about 2,000 subscribers and 12,000 emails per month! It's really great if your website is new.

Ads by Google