Search code examples
perlemailmime-mail

How to inline images with Perl Email::Mime?


I am trying to send HTML email with inline images. I will have to use native unix things and Email::Mime since those are the only things I found installed in the box i am stuck with. I am creating a Email::Mime message and sending it to sendmail. I am using cid for in-lining the image but for some reason I keep getting the image as an attachment.

Can someone help me, code snippet is below.

sub send_mail(){

use MIME::QuotedPrint;
use HTML::Entities;
use IO::All;
use Email::MIME;

$boundary = "====" . time() . "====";

$text = "HTML mail demo\n\n"
      . "This is the message text\n"
      . "Voilà du texte qui sera encodé\n";

$plain = encode_qp $text;

$html = encode_entities($text);
$html =~ s/\n\n/\n\n<p>/g;
$html =~ s/\n/<br>\n/g;
$html = "<p><strong>" . $html . "</strong></p>";
$html .= '<p><img src="cid:123.png" class = "mail" alt="img-mail" /></p>';

# multipart message
    my @parts = (
        Email::MIME->create(
            attributes => {
                content_type => "text/html",
                encoding     => "quoted-printable",
                charset      => "US-ASCII",
            },
            body_str => "<html> $html </html>",
        ),
        Email::MIME->create(
            attributes => {
                content_type => "image/png",
                name => "pie.png",
                disposition  => "Inline",
                charset      => "US-ASCII",
                encoding     => "base64",
                filename => "pie.png",
                "Content-ID" => "<123.png>",
                path => "/local_vol1_nobackup/user/ramondal/gfxip_gfx10p2_main_tree03/src/verif/ge/tb",
            },
            body => io("pie.png")->binary->all,
        ),
    );

     my $email = Email::MIME->create(
         header_str => [
             To => '[email protected]',
             Subject => "Test Email",
         ],
         parts      => [@parts],
     );


    # die $email->as_string;

    open(MAIL, "|/usr/sbin/sendmail -t") or die $!;

    print MAIL $email->as_string;

    close (MAIL);

    }

Solution

  • There are two problems with your code.

    First, it should be a Content-Id: <123.png> MIME header but your code instead produces a content-id=<123.png> parameter for the Content-Type header. To fix this don't add the Content-Id to the attributes but instead as header_str:

    ...
    Email::MIME->create(
        header_str => [
            "Content-ID" => "123.png",
        ],
        attributes => {
            content_type => "image/png",
    ...
    

    Second, the code creates a multipart/mixed content type for the mail. But the image and the HTML are related, so it should be a multipart/related content-type:

    ...
    my $email = Email::MIME->create(
        header_str => [
            To => '[email protected]',
            Subject => "Test Email",
        ],
        attributes => {
            content_type => 'multipart/related'
        },
        parts      => [@parts],
    );
    ...