Re: problem sending email using MIME::Lite from gmail's stmp server



On Jun 19, 9:56 am, "Peter J. Holzer" <hjp-usen...@xxxxxx> wrote:
On 2010-06-18 22:22, Ted Byers <r.ted.by...@xxxxxxxxx> wrote:



While the html displays OK, the top haif of it is repeated at the end.
Equally badly, the link between the jpg file (containing the logo) and
the img tag in the html is broken.

The first few lines, showing the package I am using are:
[...]
use Email::MIME::Creator;
[...]
my $html_part = Email::MIME->create(
          attributes => {
             content_type => "text/html",
          },
          body => "$html_template",
);
my $image_part = Email::MIME->create(
          attributes => {
             content_type => "image/jpg",
             name         => "logo.jpg",
          },
          body => io( "template.files/image002.jpg" )->all,
);
my @parts = ($html_part,$image_part);
my $message = Email::MIME->create(
      header => [
          From => 'con...@xxxxxxxxxxxxxxxxxxxxxxxxxxx',
          To   => 'r.ted.by...@xxxxxxxxx',
     Subject => $subject,
      ],
      parts => \@parts,
);
[...]
It could hardly be simpler.

The img tag is:

<img width=634 height=95 src="logo.jpg" v:shapes="_x0000_i1025">

That doesn't work. You cannot use a relative URL like "logo.jpg" in an
email. You can either use an http: URL (but many mailers won't resolve
them by default for privacy reasons (google "web bugs" for details)) or
a cid: URL to refer to an image within the email (this is a better idea
and obviously what you are trying to do). To use cid: URLs, all the
related parts of the message (in this case the HTML part and the image)
need to be enclosed in a multipart/related message. You don't seem to do
that.

Here is an example using MIME::Lite to build an HTML mail with embedded
images. Adapting it to Email::MIME::Creator is left as an exercise to
the reader:

#!/usr/bin/perl
use warnings;
use strict;

use MIME::Lite;

my $msg = MIME::Lite->new(
    From => 'hjp-usen...@xxxxxx',
    To   => 'hjp-usen...@xxxxxx',
    Subject => 'HTML test message',
    Type    => 'multipart/related; type=text/html',
);

my $unique = time();
my $tb_logo_cid = "tb-logo.$unique\@hjp.at";
my $smiley_cid = "smiley.$unique\@hjp.at";

$msg->attach(
    Type => 'text/html; charset=UTF-8',
    Data => "<title>Message text</title>\n" .
            "<h1>Hallo</h1>\n" .
            "<p>Hier ist ein Text mit einem Bild:</p>\n" .
            "<p><img alt='TB Logo' src='cid:$tb_logo_cid'></p>\n" .
            "<p>Es funktioniert! <img alt=':-)' src='cid:$smiley_cid'></p>\n",
);

my $part = MIME::Lite->new(
    Type         => 'image/png',
    Path         => 'Mozilla_Thunderbird_logo.png',
);
$part->attr('Content-Id', "<$tb_logo_cid>");
$msg->attach($part);

$part = MIME::Lite->new(
    Type         => 'image/gif',
    Path         => 'smiley16.gif',
);
$part->attr('Content-Id', "<$smiley_cid>");
$msg->attach($part);

$msg->print(\*STDOUT);
__END__

        hp
Thanks All,

I have tried everything suggested in this thread. Always the result
was the same, even after using cid as directed. I do not understand
why.

However, I do have progress.

In my quest for additional information, I found
Email::MIME::CreateHTML. It does make things much simpler. With it,
I need only two statements to make the message:

my %objects = (
"logo.jpg" => "template.files/image002.jpg"
);

$message = Email::MIME->create_html(
header => [
From => $from_user,
To => $to_user,
Subject => "testing Connie's email",
],
body => $html_template,
embed => 0, #<--
inline_css => 0, #<--
objects => \%objects #<--
);

The improvement this produces is twofold. First, the html body is
invariably properly displayed. Second, ythe linked in image is
displayed in the right place. However, also invariably, only half of
the logo.jpg is displayed; this despite there being enough space being
available in the browser to display it all.

The body of the html file now begins with:

<p><img width="634" height="95" src='cid:logo.jpg'></p>

NB: The result is the same regardless of whether I use a file named
logo.jpg or template.files/image002.jpg.

If I use the following:

my $logo = io( "logo.jpg" )->binary->all;
open(FOUT,"> logo.output.jpg");
binmode(FOUT);
print FOUT $logo;
close(FOUT);

I can compare the images using both Windows explorer and Irfanview,
and the files (logo.jpg and logo.output.jpg) are identical. However,
when I download and examine the image that accompanies the email, I
see it is defective.

Any ideas on why the image sent with the email is defective and how
that can be fixed?

Thanks

Ted
.