Search code examples
perlgmailemail-attachments

Send email to gmail with inline images and MIME::LITE


I need to send an email to gmail with some inline images.

I use perl and MIME:LITE, but Gmail shows my image as Attachment, not inline.

What's the problem?

This is my code,

use MIME::Lite;

my $texto = '<html><body>hola <img src="cid:image" /> adios </body></html>';

#
my $msg = MIME::Lite->new(
    Date            => "Xxx, 22 Oct 2012 17:45:00 CET",
    From            => "[email protected]",
    To              => '[email protected]',
    Subject         => "En un lugar de la mancha",
    'Message-ID'    => '123456789012345656789@8888888888888ldkf',
    Type            =>'multipart/related'
);

$msg->attach(
    Type => 'text/html',
    Data => $texto,
    Encoding => 'quoted-printable'
);

$msg->attach(
    Encoding => 'base64',
    Type => 'image/png',
    Path     => "image.png",
    Id => "image"
);

$msg->scrub(['x-mailer', 'Content-Disposition']);

print $msg->as_string;

The result is this email (I strip image secction)

Content-Transfer-Encoding: binary
Content-Type: multipart/related; boundary="_----------=_135100636840600"
MIME-Version: 1.0
Date: Xxx, 22 Oct 2012 17:45:00 CET
From: pruebachunga.com
To: [email protected]
Subject: En un lugar de la mancha
Message-Id: 123456789012345656789@8888888888888ldkf

This is a multi-part message in MIME format.

--_----------=_135100636840600
Content-Transfer-Encoding: quoted-printable
Content-Type: text/html

<html><body>hola <img src=3D"cid:image" /> adios </body></html>=

--_----------=_135100636840600
Content-Id: <image>
Content-Transfer-Encoding: base64
Content-Type: image/png; name="image.png"

iVBORw0KGgoAAAANSUhEUgAAAGQAAABkCAMAAAEwO1XwAAAAB3RJTUUH1gYE
...
kmqg1wGgkx35p/KStnLuw2BGhXwIZqT+D8sxTLVK0VpuAAAAAElFTkSuQmCC

--_----------=_135100636840600--

EDIT:

After reading on the web, I think that image is an attachment and has a problem known as "Image Blocking": https://www.campaignmonitor.com/resources/guides/image-blocking-in-email/

I maintained my question because people may find this question interesting. Now, I think that this code is correct, and with the other message, other ID, other sender... this code can run well.


Solution

  • After reading on the web, I think that image is an attachment and has a problem known as "Image Blocking": https://www.campaignmonitor.com/resources/guides/image-blocking-in-email/

    I maintained my question because people may find this question interesting. Now, I think that this code is correct, and with the other message, other ID, other sender... this code can run well.