Solved

Attaching documents in CGI sendmail

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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…

861 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

23 Experts available now in Live!

Get 1:1 Help Now