Gossamer Forum
Home : General : Perl Programming :

Email attachments

Quote Reply
Email attachments
Does anyone know how to create an attachment when using sendmail from a perl cgi script? I really need to know what an MUA like netscape passes to sendmail when attaching a file so that I can do that myself in a script.

Quote Reply
Re: Email attachments In reply to
There are lots of scripts around that do this and I have written one myself that will email a file that is uploaded from a form or fetched using Net::FTP

I use

use MIME::Base64 qw(encode_base64);

in my script to encode the attatchent. This is supposed to be around 24 times faster than encoding with the perl encode sub.

You then need to send the content type in the header and define the boundary...

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

Then define Content-Transfer-Encoding and Content-Disposition.

Then encode the attatchment and send.



Paul
Installations:http://wiredon.net/gt/
Support: http://wiredon.net/forum/

Quote Reply
Re: Email attachments In reply to
Thanks for the help. I have written a little script like that but I am getting everything in the body of the email, not as an attachment. For example, if the script writes

BF2200678B955F03F173EACA

Content-Type: application/octet-stream; name="attach.txt"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="attach.txt"
VGhpcyBpcyBhIHRlc3QgZmlsZQ==
BF2200678B955F03F173EACA

to sendmail, then that is what I receive in the body of my email! Can anyone understand why it is not sent as an attachment? The base 64 bit above is just a base64 encoding of the string "This is a test file". I was expecting therefore to get an email with an attachment called attach.txt containing that string.

Quote Reply
Re: Email attachments In reply to
Here's how I do it...

Code:
my @boundaryv = (0..9, 'A'..'F');
srand(time ^ $$);
for (my $i = 0; $i++ < 24;) {
$boundary .= $boundaryv[rand(@boundaryv)];
}
Code:
open(MAIL,"|$mail");
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: application/octet-stream; name=\"$file\"\n";
print MAIL "Content-Transfer-Encoding: base64\n";
print MAIL "Content-Disposition: inline; filename=\"$file\"\n\n";
Code:
my $buf;
$/=0;
open INPUT, "$file";
binmode INPUT if ($^O eq 'NT' or $^O eq 'MSWin32');
while(read(INPUT, $buf, 60*57)) {
print MAIL &encode_base64($buf);
}
close INPUT;
print MAIL "\n--------------$boundary--\n";
print MAIL "\n";
close MAIL;

Installations:http://www.wiredon.net/gt/
Favicon:http://www.wiredon.net/favicon/

Quote Reply
Re: Email attachments In reply to
Outstanding :-)
Thanks again

Quote Reply
Re: Email attachments In reply to
Did you manage to get it to work for you?


Installations:http://www.wiredon.net/gt/
Favicon:http://www.wiredon.net/favicon/

Quote Reply
Re: Email attachments In reply to
Paul - another question according your snippet:

How would you solve it if you want to do the following:

A hidden field in a form defines the file name - the script should check if these field contains a value - if yes it should send the message with the attached file, if not it should only send the message!

Any idea?




Quote Reply
Re: Email attachments In reply to
Just use if statements. Something like:
Code:
my $hiddenfield = $FORM{hiddenfieldname};

if ($hiddenfield =~ s/[A-Za-z0-9]+/) {

email the file using code above, replacing $file with $hiddenfield

} else {

send an ordinary email

}
Is that what you meant?

Installations:http://www.wiredon.net/gt/
Favicon:http://www.wiredon.net/favicon/