Operating System - OpenVMS
1752662 Members
5381 Online
108788 Solutions
New Discussion юеВ

Attaching file(s) to sendmail in cgi

 
Jimmy_37
New Member

Attaching file(s) to sendmail in cgi

Hi, I'm in a CGI script and have got to the point where I want to send a mail using sendmail (which works btw), and I know which file(s) I want to send with the email (and have also uploaded them to Unix server).

The problem I have now is not knowing how to actually attach the file(s) to sendmail using the code I have below I can't get it to work, any ideas?



code:-----------------------------------------
open (MAIL, "|$mailProgram -f 'webserver' -F '$email_from' '$mailTo");

print "in mail loop so file is open";

print "\nsend to [$mailTo]";
print "\nfilename [$xmlfilename]";

print MAIL "To: $mailTo\n";
print MAIL "From: webserver\n";
print MAIL "Subject: Support Request Form Results\n\n";

# for each filename (later)
open (XMLFILE, "<$xmlfilename");

while ( )
{
print MAIL $_;
}

close (XMLFILE);

# now add attachments
print MAIL "uuencode $xmlfilename";

close (MAIL);
----------------------------------------------


I appreciate the test is only for one file at the moment, and for now, I'm sending to Microsoft Outlook, but only to test really, will be using pegasus mail on secure unix server so assume I'd have to duencode, appreciate the help

p.s. have already tried different variations of the uuencode but as yet, no success :(
Give me a firm spot on which to stand, and a long enough lever, and I shall Move the Earth, Oscar Wilde
One day, I will rule the world, Just one step at a time - Me, during hangover of 2003 ;)
3 REPLIES 3
Craig A Berry
Honored Contributor

Re: Attaching file(s) to sendmail in cgi

Since you appear to be using Perl, there are much better ways to do this than opening a pipe to sendmail. I think the MIME::Lite extension is the easiest -- see the example below. BTW, if you are doing this on a UNIX server, why did you post the question in an OpenVMS forum? In any case, MIME::Lite works on any platform Perl works on.

#perl

use MIME::Lite;

MIME::Lite->send('smtp', "smtp.mydomain.com", Timeout=>60);

$file = shift @ARGV;
$recipients = shift @ARGV;

$subject = 'This message tests attachment reception [octet-stream].';
$message = "The attachment should be named $file\n";

if (&mail_file( $file, $recipients, $subject, $message ))
{
print "Mailed $file to $recipients.\n";

}
else
{
print "Error mailing $file to $recipients\n";
$error_count++;
}

exit;

#-------------------------------------------------------------------------------
# This routine actually mails the file as an attachment using base64 encoding.

sub mail_file
{
my ($path, $recipients, $subject, $message) = @_;

# Create a new message object.

my @recipient_list = split( /,\s*/, $recipients );

my $msg = new MIME::Lite
From =>$ENV{USER},
To =>\@recipient_list,
Subject =>$subject,
Type =>'TEXT',
Encoding=>'quoted-printable',
Data =>$message;

my $filename = $path;

if ($path)
{
# Attach a file to the message.

attach $msg Type =>'application/octet-stream',
Path =>$path,
Filename =>$filename,
Encoding =>'base64',
Disposition =>'attachment';
}
return $msg->send;
}
Martin P.J. Zinser
Honored Contributor

Re: Attaching file(s) to sendmail in cgi

Hello,

I do almost perfectly agree with Craig ;-)

First you really would be better off in a Unix forum (OpenVMS does not use sendmail for SMTP services). E.g.
http://forums.itrc.hp.com/cm/CategoryHome/1,,150,00.html
might be appropriate (this is the hp-ux languages section).

Now, as to the solution to your problem, I quote the Perl motto "There is more than one way to do it", so I suggest the Mail::Sender Module rather than MIME::Lite since you do not have to bother about encoding the stuff yourself, the right form will be chossen automatically for you. I do use this module daily in a production environment to sent out both textual and binary file attachments.

Check http://search.cpan.org/author/JENDA/Mail-Sender-0.8.08/
for the module.

Greetings, Martin

Vouters
Advisor

Re: Attaching file(s) to sendmail in cgi

If your intention is only to send SMTP mail with file attachments from a Unix box, perhaps you would consider grabbing the following tool:
[Compaq C] Example-C,C++ SMTP Mail Message Sender With MIME File Attachments
available at the following URL:

http://h18000.www1.hp.com/support/asktima/communications/009F15E8-A8CE0B38-1C02A1.html

I tested the program on Tru64, HP-UX, Linux and Sun Solaris. Its use ought to be simple.
Hoping this can help you.