I figured it was probably a Content-Type problem, but could not put my finger on it.

Right. A message with attachment is Content-Type: multipart/mixed where the parts are divided by the boundary. - Instead of repeating $smtp->datasend() statements over and over as in alejandro's example, one could make use of HERE-documents. The parts begin after the boundary line followed by the sub-headers which define the type of the part and an empty line. It is good practice to announce the type of mail to non-MIME-aware clients before any part starts.

... use MIME::Base64; use Time::HiRes qw(gettimeofday); ... my $filename = "some.pdf"; my $pdfbody = do { local $/; open my $fh,'<', $filename or die "pdfread: $!"; <>; }; my $boundary = encode_base64( join('',gettimeofday), ''); # $smtp->datasend(<<"EOH"); From: $from To: $to Subject: $subject Content-Type: multipart/mixed; boundary="==$boundary"; MIME-Version: 1.0 Return-Path: postmaster\@your-domain.tld This is a message in MIME format. Please use a MIME capable mail client to read this message. --==$boundary Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8bit Hello $recipient, this is the mail body as displayed by your MIME capable mail client. blah blah The PDF file $filename is sent with this message as attachment. regards, $sender --==$boundary Content-Type: application/pdf; charset=utf-8; name="$filename" Content-Disposition: attachment; filename="$filename" Content-Transfer-Encoding: 8bit $pdfbody --==$boundary-- EOH

This avoids repetitions and is easier to maintain.

update: added last boundary delimiter as per afoken's comment

perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'

In reply to Re^3: Using Net::SMTP to send email attachments by shmem
in thread Using Net::SMTP to send email attachments by astrobal

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.