Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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( 'mailto:mail@tobyinkster.co.uk', From => 'tai@g5n.co.uk', 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'

In reply to Re: Sending a email with file attachment with LWP ONLY by tobyink
in thread Sending a email with file attachment with LWP ONLY by MarkusH

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (6)
As of 2024-04-23 08:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found