Gossamer Forum
Home : General : Perl Programming :

Email attachment

Quote Reply
Email attachment
The following bit of code *almost* works for me:

Code:
$mail = "/usr/sbin/sendmail";
$email = "me@mydomain.com";
$me = "script@mydomain.com";
$subject = "poa file for me";
$file = "poa.txt";

my @boundaryv = (0..9, 'A'..'F');
srand(time ^ $$);
for (my $i = 0; $i++ < 24;) {
$boundary .= $boundaryv[rand(@boundaryv)];
}

open(MAIL,"|$mail -oi -t");
print MAIL "To: $email\n";
print MAIL "From: $me\n";
print MAIL "Subject: $subject\n";
print MAIL "Content-Type: multipart/mixed; boundary=\"------------$boundary\"\n";
print MAIL "\n";
print MAIL "This is a multi-part message in MIME format.\n";
print MAIL "--------------$boundary\n";
print MAIL "Content-Type: text/plain; charset=us-ascii\n";
print MAIL "Content-Transfer-Encoding: 7bit\n\n";
print MAIL "Hello\n";
print MAIL "\n";
print MAIL "--------------$boundary\n";
print MAIL "Content-Type: text/plain name=\"$file\"\n";
print MAIL "Content-Transfer-Encoding: base64 \n";
print MAIL "Content-Disposition: inline; filename=\"$file\"\n\n";


close INPUT;
print MAIL "\n--------------$boundary--\n";
print MAIL "\n";
close MAIL;


I get the email and attachment, but the attachment is blank. Can anyone see what may need to be tweaked?

It's a plain text file.


FYI - I used to use this one:
Code:
open(MAIL, "|$mailprog -oi -t") or die;

print MAIL <<"END";
From: $yourname
To: $youremail
Cc: $ccmail
Subject: POA Request

Requested File: $file

END
open(FILE, "uuencode $file $file|") or die;
while( <FILE>) { print MAIL; };

# Close the file and finish sending the mail

close(FILE);
close(MAIL);

Until our hosting company "upgraded" our servers and now it no longer works.
Quote Reply
Re: [Watts] Email attachment In reply to
im a little newbie with data mail but somewhere i saw that the line you write the email data should end with \r\n
worth a try.

any way if there is this module actived for you Net::SMTP::Multipart its pretty easy to attach things and send.
Quote Reply
Re: [Watts] Email attachment In reply to
You need to open the file and read the contents into $file. Take a look at the Perl open tut:

http://perldoc.perl.org/...ut.html#Simple-Opens

~Charlie

[Edit]
Hmm, I see this:

Code:
close INPUT;

Did you miss pasting the code to open the file maybe?
[/edit]

Last edited by:

Chaz: Jul 13, 2005, 1:53 PM
Quote Reply
Re: [Chaz] Email attachment In reply to
D'oh! Two things I didn't see...

Left out this:

use MIME::Base64 qw(encode_base64);

and missed part of a cut-n-paste as noted by Chaz. Blush

This post is where I got the code from:
http://gossamer-threads.com/perl/gforum/gforum.cgi?do=post_view_flat&post=143369
Just don't for get to add the use MIME::Base64 part.