PDA

View Full Version : How to email attachments?


cortic
03-20-2003, 07:47 AM
The idea is that a surfer selects options from a page and hits submit then gets those options emailed to them.. simple, but I really need to display it using CSS and maybe some JavaScript in the email, I have heard that {print MAIL "Content-type: text/html\n";} lets html tags display properly inline, but with all the different email clients out there I'm thinking it would be easier and safer to make the info into an attachment.

I can arrange it all with perl into one variable (their chosen options) along with all relevant HTML CSS and JS tags and stuff, but how do I then add that to my email:

open (MAIL, "| /usr/sbin/sendmail -t");
print MAIL "Content-type: text/html\n";
print MAIL "Reply-To:\n";
print MAIL "Errors-To:\n";
print MAIL "Sender:\n";
print MAIL "Subject: ";
print MAIL "From: \n";
print MAIL "To:\n";
print MAIL "whatever - \n";
print MAIL "$thename\n $themail\n $themnth\n $thecmts\n\n";
close (MAIL);

Is there an easy way to dump an attachment into the above code? When the attachment itself is an html file already stored in a variable?


Thanks

cortic
03-21-2003, 11:14 AM
I've learned a bit from reading through the mailto script at mattsscripts.co.uk, in particular:

"\n--".$boundary."\n".
"Content-Type: octet/stream; name=".$filename."\n".
"Content-Transfer-Encoding: BASE64\n".
"Content-Description:\n".
"Content-Disposition: attachment; filename=".$filename."\n\n".base64_encode($fd)."\n";

the boundary bit is just confusion, I am assuming its part of the message or something..?
I need to make the html file attach and a small image file too - so Content-Type would be like above for the image and text/html for the html file? Yes? I assume the name after the semicolon would be the filename I would link to from the html file..? Content-Transfer-Encoding would be BASE64 for the image and not needed for the HTML..?
Content-Disposition (which I've never heard of before lol) would be attachment for both and the filename would be its location on server for the image and just the variable for the HTML..? I don't understand what base64_encode($fd) is doing there.. though I have seen this in emails, I've searched the script but don't know what kind of data to pass to that ($fd).

I don't want to use someone else's script for this, I'd rather learn this myself, so if anyone knows a good tutorial that covers this subject please let me know, otherwise if you can answer any of the above confusions I'd really appreciate it.

thanks

YUPAPA
03-21-2003, 10:08 PM
Use mine:


#!/usr/bin/perl
use Net::SMTP;
use MIME::Entity;
use strict;


my $email_server = q(localhost);
my $emailformat = q(text/plain);
my $to = q(you@yourdomain.com);
my $from = q(you@yourdomain.com);
my $subject = q(Testing Email Attachments);
my @files = ('/path/to/attachment.file');
my @body = q(HelloHello);


my $email = build MIME::Entity
( Type => $emailformat,
From => $from,
To => $to,
Subject => $subject,
Data => \@body,
Disposition => 'inline',
Encoding => 'quoted-printable'
);
foreach my $this_file (@files) {
if (-e $this_file) {
$email -> attach ( Path => $this_file,
Type => 'application/octet-stream',
Disposition => 'attachment',
Encoding => 'base64'
);
}
}
my $smtp = Net::SMTP -> new ($email_server,
Hello => 'localhost',
Timeout => 120,
Debug => 0
);
$smtp -> mail ($from);
$smtp -> to ($to);
$smtp -> data ();
$smtp -> datasend ($email->stringify);
$smtp -> dataend ();
$smtp -> quit;