kleinbiker7 has asked for the wisdom of the Perl Monks concerning the following question:

Hey guys, I have a mailing problem. I am trying to get Mime Lite to send a simple text email with an attachment via SMTP. However, I am ending up with the encoded attachment in the BODY of the email, instead of it being attached as it should be

Here is the code that I am using. I used this site to obtain the code: http://perl.about.com/library/weekly/aa042302b.htm

### Create a new multipart message: $from = 'me@myworld.com'; $email = 'you@yourworld.com'; $msg = MIME::Lite->new( From =>"$from", To =>"$email", Subject =>"View User Report Results", Type =>'text/plain', Data =>"$data" ) || die "Error Creating MIME Body: $!\n\n"; # Attach the HTML document if the email is not empty if ($TheDocument2 !~ /E-mail.+$/) { # FIRST, CREATE A NEW DATA FILE my $gzip = '/bin/gzip'; my $unzipf = '/usr/temp/reports/YourResults.txt'; my $zipf = '/usr/temp/reports/YourResults.txt.gz'; open (UNZIPPED, ">$unzipf") || die "Cant prepare the unzipped +file $unzipf\n\n"; $script{guts} =~ s/\n/\r\n/g; print UNZIPPED $script{guts}; close (UNZIPPED); # NOW ZIP THE FILE UP! system ("gzip -f $unzipf"); # OPEN THE ZIPPED FILE, AND GET DATA FROM IT undef $/; # NOW ATTACH THE ZIPPED FILE TO THE EMAIL! $msg->attach( #Type =>'application/x-gzip', Type =>'application/zip', Disposition =>'attach', #Data =>"$zip_data", Path =>"$zipf", Filename =>'YourResults.txt.gz' ) || die "Error Creating MIME Attachment: $!\n\n";; # DELETE THE ZIP FILE system ("rm -f $zipf"); } #$msg->send(); # I WISH TO SEND THIS VIA SMTP! my $message_body = $msg->body_as_string(); # Set this variable to your smtp server name my $ServerName = 'mail.policy.net'; my $smtp = Net::SMTP->new($ServerName) || die "Couldn't connect to + server"; $smtp->mail ( $from ); $smtp->to ( $email ); $smtp->data(); $smtp->datasend($message_body); $smtp->dataend(); # Close the connection $smtp->quit();

This is what I get:

This is a multi-part message in MIME format. --_----------=_103832648034060 Content-Disposition: inline Content-Length: 452 Content-Transfer-Encoding: binary Content-Type: text/plain Here are the results of your View Users Report search. Save this file to your hard drive and extract it using WinZip (Windows) or StuffIt (Macintosh). Once extracted, you can view and manipulate the data file by using a spreadsheet application like Microsoft Excel or another program of your choice If you have other questions please contact your IDI account representa +tive or send an email to request@idi.net. Thank You --_----------=_103832648034060 Content-Disposition: attach; filename="YourResults.txt.gz" Content-Transfer-Encoding: base64 Content-Type: application/zip; name="YourResults.txt.gz" H4sICNCa4z0AA1lvdXJSZXN1bHRzLnR4dADFVl1v2zYUfb4D9h/4tg6IA5Ii 9fUUxU6XtokzxMGC7mGFatExV0kMJDqp//0u5VhSbSdIUiMzBEiUqXPPvTzn kieDItU5jFKryMWMHJtFOVXwXle1JeO0UHCWrp+SLKtUXcNQ2yVMLH4Bf+tb MjQZfpB+h6Epb9wEbco0JyNd20pP7WomOTWLWm2+nKjS3dq3ydTixzWcOE5k pHJ9p6qlA+8GEzVdVA8MKqvLG+K4w5W51dMafv0F59kiLY/udJotpvZwagqA E4SHs+YPYFQyj1yrZWEWdk4mtlLKHpDJQiMVTj04Vnau6izN4HwEnIZMDAT3 OUCR4b/no1MRJHibUBEAhdKUCsYGnz4ucsKjA8SgnFAeCxZ70Z/ngKR0OTNH OtOHpbIAH0YfsJiFLiFiEWFhj8X4+oAwii/e58ZUcJ3Wc0zRmjKD0RDJUOoD ZNMUV2x4iiO8TTDcc3nMdZ5Wy5bJxdQS7nUzGYtpiDNPm2kwmZtKAZOSnJvS pjcL1fJMbu0hYXBcGfMtX5Yw/gyMccoGnhQC3oVB8DsJRTBgge9BuWQUZ5xS yfE2oVxiJjC3Rd4R9tgDDcZjL4ilQBr/JHlO/qjSusYwtiarNT4gpPljrO7X rwimVpmvqrI/pMb8DpNSvJJzuGymwcTkpjBODL6kfTG0q99ffMkA0pwyt/iU Bc3i4w3TWSfBIVncELpOgmLJ/ZjzFyehSqU6oVy6IYwWZQkeY+RYlTZXSzJs /DObzUyVwV8JcC6lGATSjwDuUqQJyMeq73ZV3oZZ2GPmxSx8lBlpZfJl+qV2 CjiaG+sMubLSz2pjlxr6YmjYrtXLaCx4zGXywjr+q2azrowfcQSTntmu0Gc9 b6GhVtYaSIkO55QPuO8NeITaHQ2TM6wnXdXzs6rx0WmrLaijyGJG1xQ3mNh7 bb2j1DxU78oNof31E29QvQ6VBjEPksa2dq5r4q7yLs119u7Iqtq675/fP9r2 scpxI6+WAGt1EsWSohNfWvlGwRkqdqeKGfdIUi6dz7Abo5jJpZl+g+QSAs55 MPCiwFmtwo4GwDa4cdYVR3qxjB7l1hB5DomLfFnc6hSuE4hCSeUAK4QE7lNX oqcJyFiIJwlYVfVaLXxSea5xn3JvX79uu7XZiMhpk6948s7swo89+gyz/1YT Jyq+w+DF2uCoua9rU5fL1tT8MVN31RubO0JFVz2OV5j0tqQ3Dy/Z/xgel8VL Njbkp0M7lewnPHO7PBeb4Z9KfW+BvZhuBfbeJnCzh/wQWBxmbxFatFK7wRbU 9QM3grrQdg4iJEz2m8HJjgbAB6GgYf/w568Of/5WfNYeQzBvgX3qpccQt2l1 TK/mqtm3dhyWdmxjDYGgI8BwG6Or06eDZdu47JXAETa3DphvA/O9AHvbwN5e gMU2sNgLsNwGlnsB9reB/b0AB9vAwV6Aw23gcC/A0TZw9HrgoGcQusMhdD/Q u8z3E+57gP4PYOA1oUMQAAA= --_----------=_103832648034060--
So why is it not attaching the file properly? Thanks! Robert

Replies are listed 'Best First'.
Re: How can I mail email with attachments using MIME::Lite and SMTP?
by UnderMine (Friar) on Nov 26, 2002 at 16:52 UTC
    Asside: You might find Mail::Sender worth looking at as as it looks like this does exactly what you are trying to do.

    I have had good success using this to send HTML email with embedded images.

    Hope it helps
    UnderMine

      Sorry, but I do not have the permissions to install any new libraries on our system, so it's either smtp or bust. Thanks! Robert
Re: How can I mail email with attachments using MIME::Lite and SMTP?
by impossiblerobot (Deacon) on Nov 26, 2002 at 19:45 UTC

    Mime::Lite has the ability to send mails by SMTP without resorting to another library:

    $msg->send('smtp', 'mail.hostname.com');

    Update: Actually, Mime::Lite uses Net::SMTP "under the covers" to do its SMTP sending.


    Impossible Robot
Re: How can I mail email with attachments using MIME::Lite and SMTP?
by Thelonius (Priest) on Nov 27, 2002 at 00:04 UTC
    This might be a dumb question, but is there any chance you are reading this email with a mail reader that doesn't support MIME?
Re: How can I mail email with attachments using MIME::Lite and SMTP?
by Thelonius (Priest) on Nov 26, 2002 at 20:53 UTC
    You need to start with Type => 'Multipart/mixed', then add two attachments, one inline for the message body and one for the zip file.

      Mime::Lite will actually "magically" rewrite the header to "multipart/mixed" if you add additional attachments to a single-part message. Nevertheless, it might be worth trying, just for troubleshooting.


      Impossible Robot