?
Solved

Attaching documents in CGI sendmail

Posted on 1997-12-06
1
Medium Priority
?
190 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 1

Accepted Solution

by:
maholt earned 400 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

770 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