Solved

Attaching documents in CGI sendmail

Posted on 1997-12-06
1
174 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Most of the sites are being standardized with W3C Web Standards. W3C provides lot of web standard services to the web. They have the web specification, process and documentation for all the web standards. You can apply HTML, CSS and Accessibility st…
This article covers the basics of the Sass, which is a CSS extension language. You will learn about variables, mixins, and nesting.
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

744 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now