Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Using Net::SMTP to send email attachments

by huck (Prior)
on Apr 30, 2017 at 07:18 UTC ( [id://1189199]=note: print w/replies, xml ) Need Help??


in reply to Using Net::SMTP to send email attachments

I think you may be missing some headers

MIME-Version: 1.0 Content-Type: multipart/mixed; boundary=frontier Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=\"UTF-8\"
Without multipart/mixed i think it just sees the rest as text.

https://en.wikipedia.org/wiki/MIME#Multipart_messages

Re: Sending An Attachment Using Net::SMTP

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

    Thanks Huck, after reading your second link to Alecjandro's 2014 posting, and slavishly putting it the multipart/mixed line and other lines that were not in my scrip, it worked. I got the text message to print out as text in the resulting email and then the binary file to be paperclipped as an attachment. I figured it was probably a Content-Type problem, but could not put my finger on it.

    Thanks very much again.

      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'

        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.

      As an update, I am finding that in different email clients, the text part of the email is also coming through as an attachment. So, from having no attachments I now have two attachments! It is probably a boundary problem, but I do not seem to be able to determine just where the problem is.

        The second attachment is empty, right? Remove the last boundary. It is followed by nothing, and thus produces an empty attachment. update: Instead, output the boundary with "--" (two hyphens) attached, as per afoken's advice below.

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

        It would be helpful to see what you did try

        You may have one too many $smtp->datasend("--$boundary\n"); in there. you dont want a boundary before the main text. The example seems to have one.

        Hi there.

        Believe me, I like an easy life. I would not be trying to hand-craft this email script if there was a neater, simpler way.

        It has been written that there are 927 ways to send emails using Perl. MIME::Lite is a very nice example and I have used it (and still use it) very successfully. I use MIME::Lite to send the content of cgi forms to myself, at an email address which is local to the web server handling the form. The problem with 926 of these methods is that they were written 20 years ago, when spam was not the problem it is now and, email servers were happy to accept emails generated by non-local web servers. That is not the case today. What Net::SMTP does is to transfer the email to the local email server, from which it is then sent. That way the email is not then blocked by AOL and other email servers as not coming from an email server and so probably spam.

        The other problem is that only MIME::Lite of the MIME variants is available on the suite of Perl modules available at my ISP. They like an easy life too, so they will not allow me to install modules of uncertain provenance - even if they do come from CPAN - that may be a potential threat to the security of their systems. I could a few years ago, but - like with emails - the world has moved on. So, I am stuck with what I have.

        Thanks anyway for the suggestions...

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1189199]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2024-04-24 02:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found