Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

Re: Sending a email with file attachment with LWP ONLY

by tobyink (Abbot)
on Jul 05, 2012 at 13:19 UTC ( #980059=note: print w/replies, xml ) Need Help??

in reply to Sending a email with file attachment with LWP ONLY

I'm not entirely sure what you think that boundary parameter is doing in your Content-Type header. The boundary parameter is only defined for multipart content types, not text/plain.

What exactly do you mean by "without using MIME or MAIL"? Are you referring to some specific Perl modules? Or do you mean that you want to avoid your mail conforming to the MIME specification? If the latter, you should be aware that MIME is the only commonly supported mechanism for attaching files to e-mail, so if you use any non-MIME mechanism for attaching files, it's unlikely to be recognised by recipients.

If you just want to avoid using Perl MIME modules, it's not especially difficult. You'll need a base64 or quoted-printable encoding function, but once you've got that it's quite simple.

use LWP::UserAgent; use HTTP::Request::Common 'POST'; # I'm using encode_base64 from MIME::Base64, but you can use something # else if you prefer. use MIME::Base64 'encode_base64'; # I'm using the DateTime module to generate the "Date" header for my # e-mail, but you can use something else if you prefer. use DateTime; my @parts = ( { 'Content-Type' => 'text/plain', 'Content-Disposition' => 'inline', 'CONTENT' => 'Hello world', }, { 'Content-Type' => 'text/html', 'Content-Disposition' => 'attachment; filename=example.html', 'CONTENT' => '<!doctype html><title>HW</title><p>H +ello world', }, { 'Content-Type' => 'text/plain', 'Content-Disposition' => 'attachment; filename="another exampl +e.txt"', 'CONTENT' => 'Hello world', }, ); my $boundary = join '-', map { sprintf '%08x', rand(2**32) } 0..3; my $body = "This is a multipart message in MIME format.\n\n"; foreach (@parts) { my %part = %$_; my $part_body = delete $part{CONTENT}; $body .= "--$boundary\n"; $body .= "$_: $part{$_}\n" for keys %part; $body .= "Content-Transfer-Encoding: base64\n"; $body .= "\n"; $body .= encode_base64($part_body)."\n"; } $body .= "--$boundary--\n"; my $req = POST( '', From => '', Date => DateTime->now->strftime('%a, %d %b %Y %H:%M:%S %z' +), Subject => 'Test Message', Content_Type => qq(multipart/mixed; boundary="$boundary"), Content => $body, ); my $response = LWP::UserAgent->new->request($req);

The above code is quite na´ve and there will no doubt be plenty of edge-cases it doesn't cover, but if you're using it in a small, tightly controlled place, it may be adequate.

perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

Replies are listed 'Best First'.
Re^2: Sending a email with file attachment with LWP ONLY
by MarkusH (Initiate) on Jul 05, 2012 at 14:42 UTC
    Thanks a LOT, your answer got me in the right direction! I tried something in this direction first, but i failed when i tried to combine multiple contents, my understanding of bondarys was false! Not as small as a solution with MIME or NET, but it will satisfy the problems requirement! @all: thanks to your responses! It interesting to see, in how many ways you can solve a problem in perl!

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://980059]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (3)
As of 2018-05-20 14:34 GMT
Find Nodes?
    Voting Booth?