Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Perl Sendmail and Attachment

Status
Not open for further replies.

starky5

MIS
Sep 27, 2001
9
US
I have a short program that I want to email a simple text file as an attachment. I have gotten the email to work but not the attachment portion. Here is my code so far. Can someone help me with the attachment part. Thanks, Greg

$smtpserver= "server.com";
$mail{From} = 'Security Manager <sys_admin@Cargill.com>';
$mail{To} = '<gstark@'.$smtpserver.'>';
$mail{Smtp} = $smtpserver;
$mail{Subject} = &quot;Accounts locked in the last 7 days&quot;;
$mail{Message} = &quot;Please see attached file.\n&quot;;
$mail{attachment} = &quot;$lock_File&quot;;

if (sendmail %mail) {
print &quot;Email Successful&quot;;
}
else {
print &quot;\n!Error sending mail:\n$Mail::Sendmail::error\n&quot;;

}


 
You forgot the

use Mail::Sendmail;

statement at the top to tell me that your were using the Mail::Sendmail module - I figured it out from the

print &quot;\n!Error sending mail:\n$Mail::Sendmail::error\n&quot;;

statement at the end. Try to give as much pertinent info as possible - you'll be helping us to help you!

I've never used the Mail::Sendmail module, but if you haven't seen it already, there's an example at


that I found on the at search.cpan.org.

HTH. Hardy Merrill
Mission Critical Linux, Inc.
 
I actually had the use statement at the top of the script, but I only included the mail code, sorry. The entire script is below. Also, I have looked at the links, but they don't seem to give a good example of attaching text files.

#!perl
use Mail::Sendmail;

#Variables
$dump_file = &quot;c:\\temp\\eldump.txt&quot;;
$lock_file = &quot;c:\\temp\\ellock.txt&quot;;
$exception = &quot; S-&quot;;
$format = &quot;%-12s %-12s %-6s %-20s %-12s\n&quot;;

# Main
&main;

sub main {
#Build dump file
`dumpel -f $dump_file -s \\\\csmpls02m -l Security -m security -e 644 -d 7`;

#Open el dump file
open (DUMP, &quot;<&quot;.$dump_file) || die &quot;Could not open $dump_file for reading!\n&quot;;
open (LOCK, &quot;>&quot;.$lock_file) || die &quot;Could not open $lock_file for writing!\n&quot;;

printf LOCK &quot;\n&quot;.&quot;The following accounts were locked within the last 7 days&quot;.&quot;\n&quot;;
printf LOCK &quot;$format&quot;, DATE, TIME, ID, USER, MACHINE;

my(@site_fields);
while (<DUMP>) {

$_ =~ s/\n//g;
my(@el_fields) = split(/\t/, $_);
if (@el_fields[9] =~ /^$exception/) { next; }
else {
my(@lock_fields) = split(/\s/, @el_fields[9]);
printf LOCK &quot;$format&quot;, @el_fields[0], @el_fields[1], @el_fields[4], @lock_fields[0], @lock_fields[1];
}

}
close (DUMP);
close (LOCK);

$smtpserver= &quot;sbox.cargill.com&quot;;
$mail{From} = 'Security Manager <DOMAIN@Cargill.com>';
$mail{To} = '<gstark@'.$smtpserver.'>';
$mail{Smtp} = $smtpserver;
$mail{Subject} = &quot;Accounts locked in the last 7 days&quot;;
$mail{Message} = `type $lock_file`;
$mail{Attachment} = &quot;$lock_file&quot;;

if (sendmail %mail) {
print &quot;Email Successful&quot;;
}
else {
print &quot;\n!Error sending mail:\n$Mail::Sendmail::error\n&quot;;

}
} # main
 
This part of the document describes exactly how to send a message with an attachment - read it carefully.

----------------------------------------------------------

How to send attachments?

In a way very similar to the HTML mail above. Be aware that you will put the whole file(s) in memory (twice, actually), so this method isn't suitable for very big files. For files of reasonable size though, you can adapt this example:

use MIME::QuotedPrint;
use MIME::Base64;
use Mail::Sendmail 0.75; # doesn't work with v. 0.74!

%mail = (
SMTP => 'smtp.site1.csi.com',
from => 'sendmail@alma.ch',
to => 'sendmail@alma.ch',
subject => 'Test attachment',
);


$boundary = &quot;====&quot; . time() . &quot;====&quot;;
$mail{'content-type'} = &quot;multipart/mixed; boundary=\&quot;$boundary\&quot;&quot;;

$message = encode_qp( &quot;Voilà le fichier demandé&quot; );

$file = $^X; # This is the perl executable


open (F, $file) or die &quot;Cannot read $file: $!&quot;;
binmode F; undef $/;
$mail{body} = encode_base64(<F>);
close F;

$boundary = '--'.$boundary;
$mail{body} = <<END_OF_BODY;
$boundary
Content-Type: text/plain; charset=&quot;iso-8859-1&quot;
Content-Transfer-Encoding: quoted-printable

$message
$boundary
Content-Type: application/octet-stream; name=&quot;$^X&quot;
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename=&quot;$^X&quot;

$mail{body}
$boundary--
END_OF_BODY

sendmail(%mail) || print &quot;Error: $Mail::Sendmail::error\n&quot;;

-----------------------------------------------------------

HTH. Hardy Merrill
Mission Critical Linux, Inc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top