Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re^2: Send Email With Picture Embedded in Body of Email

by afoken (Chancellor)
on Sep 10, 2020 at 15:01 UTC ( [id://11121554]=note: print w/replies, xml ) Need Help??


in reply to Re:Send Email With Picture Embedded in Body of Email
in thread Send Email With Picture Embedded in Body of Email (SOLVED)

Here is my attempt to add a section to embed the picture in the body of the email. Unfortunately, it does not work. The email arrives with the raw base64 data displayed.

############################################################## # Embed JPG image in email body. # Note: This does NOT work. ############################################################## $smtp->datasend("MIME-Version: 1.0\n"); $smtp->datasend("Content-Type: text/html; boundary=$boundary\n"); $smtp->datasend("Content-Transfer-Encoding: base64\n"); $smtp->datasend("Content-Disposition: inline\n"); $smtp->datasend("\n"); $smtp->datasend("<body><img alt=\"My Picture\" img src=\""); open(my $fh2, '<', $pic) || die("Could not open Jpg file!"); binmode($fh2); local $/=undef; while (read($fh2, my $chunk, 72*57)) { my $buf = &encode_base64( $chunk ); $smtp->datasend($buf); } close($fh2); $smtp->datasend("\"></body>");

In the mail header, you claim to encode the entire mail as base64, but you send it in plain text. That won't work.

A little bit later, you use a base64 encoded file as URL of that file in an IMG tag. Of course, that also won't work. For an image URL containing the image instead of pointing to it, use a data URI. You would at least need to prefix the bas64 encoded image with "data:image/jpeg;base64,".

Note that several HTML viewers have size limits on data URIs, others don't accept them at all.

The RFC way of sending HTML mails with images is to append the image to the mail, give it a CID, and use the CID to as the URI of the image in the HTML part. See RFC2111.

MIME::Lite (yes, I know this will trigger tons of "do not use that module" warnings) has a quite easy way to do that:

$msg = MIME::Lite->new( To =>'you@yourhost.com', Subject =>'HTML with in-line images!', Type =>'multipart/related' ); $msg->attach( Type => 'text/html', Data => qq{ <body> Here's <i>my</i> image: <img src="cid:myimage.gif"> </body> }, ); $msg->attach( Type => 'image/gif', Id => 'myimage.gif', Path => '/path/to/somefile.gif', ); $msg->send();

(Copied from "Send an HTML document... with images included!" in the MIME::Lite pod)

Alexander

--
Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

Replies are listed 'Best First'.
Re^3: Send Email With Picture Embedded in Body of Email
by roho (Bishop) on Sep 10, 2020 at 15:21 UTC
    Thank you for your reply afoken. I would like very much to use a higher level module than "Net::SMTPS" (even the deprecated MIME::Lite), but "Net::SMTPS" is the only module I have found with the options that allow me to connect to the Outlook web email server.

    "It's not how hard you work, it's how much you get done."

      Have you tried building the message with MIME::Lite, and converting it to a string to send with Net::SMTPS?

      Borrowing from afoken's post:

      $msg = MIME::Lite->new( To =>'you@yourhost.com', Subject =>'HTML with in-line images!', Type =>'multipart/related' ); $msg->attach( Type => 'text/html', Data => qq{ <body> Here's <i>my</i> image: <img src="cid:myimage.gif"> </body> }, ); $msg->attach( Type => 'image/gif', Id => 'myimage.gif', Path => '/path/to/somefile.gif', ); $msg->send();

      But replacing $msg->send(); with $smtp->datasend($msg->as_string).

      In practice it might be more complicated since you will have to check for lines with a single period (untested):

      $smtp->datasend( join "\n", map { $_ eq "." ? ".." : $_ } split "\n", $msg->as_string );
        Thank you jeffenstein! That worked perfectly, and it worked without the need of the "join, map, split" code. My final version of the code segment for an embedded image in the email body (while still using "Net::SMTPS") is shown below:

        ############################################################## # Email - body (embedded image) ############################################################## $smtp->datasend("--$boundary\n"); my $msg = MIME::Lite->new( To =>'tester@hotmail.com', Subject =>'HTML with Embedded Image', Type =>'multipart/related' ); $msg->attach( Type => 'text/html', Data => qq{ <body> <img src="cid:mypicture"> </body> }, ); $msg->attach( Type => 'image/jpeg', Id => 'mypicture', Path => 'test.jpg', ); $smtp->datasend( $msg->as_string );

        "It's not how hard you work, it's how much you get done."

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (5)
As of 2024-03-29 09:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found