in reply to Re^2: Using Net::SMTP to send email attachments
in thread Using Net::SMTP to send email attachments

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'

Replies are listed 'Best First'.
Re^4: Using Net::SMTP to send email attachments
by astrobal (Acolyte) on Apr 30, 2017 at 18:45 UTC

    I tried this script but I got an error message, "EOH" seems to be undefined here....?

    And, is the email sent from the web server instead of being transferred to the email server? My understanding is that emails that do not originate from specific email servers are regarded as spam and so binned.

    Nice if we could get this to work though...

    Thanks.

      "I tried this script but I got an error message, "EOH" seems to be undefined here....?"

      The second EOH needs to be on a line by itself, with no leading or trailing whitespace. See "perlop: Quote-Like Operators: here-document" for details.

      [For future reference, please post error messages, in their entirety, between <code>...</code> tags, so that we can see a verbatim copy: a synoptic, prosaic rendering of the message is typically not that helpful.]

      — Ken

      I tried this script but I got an error message, "EOH" seems to be undefined here....?

      What error message do you get?

      And, is the email sent from the web server instead of being transferred to the email server?

      The message is sent from the machine where the script runs. Whether it is sent directly or transferred to the mail exchanger of this machine's domain depends on your mail configuration.

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

        The error message is this:

        "Can't call method "datasend" on an undefined value at example2.cgi line 26, <> line 1."

        and line 26, line 1 was "$smtp->datasend(<<"EOH");"

        Thanks

        Geoffrey