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":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.