Solved

Attaching documents in CGI sendmail

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

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

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

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

705 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