Solved

Attaching documents in CGI sendmail

Posted on 1997-12-06
1
185 Views
Last Modified: 2013-12-25
I am trying to make a cgi script that will attach a document to the results that it e-mails me. This document is a text file that resides in the same folder as the script.  It is on an apache server.
0
Comment
Question by:learning120697
1 Comment
 
LVL 1

Accepted Solution

by:
maholt earned 100 total points
ID: 1831530
This is kind of a long post because of the script but here goes.  You cannot exactly attach a file to a mail message but you can have the message that you receive have an attachment.  To get this to work you have to have your cgi script write out a file. This file is then mailed to you.  In this file that is created you can format the message in such a way that part of the message is seperated out into an attachment.

Incorporate this script into the one you have.  This sets up a three part mail message.  The header first.  Here you can put the From:, Subject:, Date:, and such.  The second part is between the first and second "$bound" lines.  What ever you put hear will be the body of the mail message.  The third section is comes between the second and third "$bound" lines.  This is the MIME information about what follows.  This is where the file is attached, so to speak.  This takes a file called "$attachFile" reads in every line and prints it out to the temporary mail file.

To use this, I would suggest putting it into a subroutine.  Then in your script add the lines:
&build_mail_message;
`/lib/sendmail me@host.com < $tmpFile`;

Improvements could be changing the name of the include file that the receiver gets(change the $attachFile name on line 16 and 31 of the script), and allowing multiple includes(add all the names on the X-Attachments line, and loop through the second to third $bound lines)

Hope this helps
Michael Holt
#####################################################
#!/bin/perl -I/bin/perllib

use Fcntl;

$tmpFile = "ttt.mail";
$attachFile = "test.mail";
$bound = "=======bound=====";


open(MAILFILE,"> $tmpFile");


print MAILFILE "Mime-Version: 1.0\n";
print MAILFILE "From: You Know Who\n";
print MAILFILE "Subject: test 2\n";
print MAILFILE "X-Attachments: " . $attachFile . ";\n";
print MAILFILE "Content-Type: multipart/mixed; boundary=\"" . $bound . "\"\n";
print MAILFILE "\n";
print MAILFILE "--" . $bound . "\n";
print MAILFILE "Content-Type: text/plain; charset=\"us-ascii\"\n";
print MAILFILE "\n";

print MAILFILE "--" . $bound. "\n";
print MAILFILE <<END_OF_MESSAGE_BODY;
Put the text for the message body here
END_OF_MESSAGE_BODY

print MAILFILE "--" . $bound. "\n";
print MAILFILE "\n";
print MAILFILE "Content-Type: text/plain; charset=\"us-ascii\"\n";
print MAILFILE "Content-Disposition: attachment; filename=\"" . $attachFile . "\"\n";
 


open(TTT,"< $attachFile");

while (<TTT>)
{
      print MAILFILE;
}
close(TTT);
print MAILFILE "\n";
print MAILFILE "\n";
print MAILFILE "--" . $bound. "\n";

close(MAILFILE);

0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article covers the basics of the Sass, which is a CSS extension language. You will learn about variables, mixins, and nesting.
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

789 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question