user2429569
user2429569

Reputation: 101

sending multipart text/html alternative message with file attachments with perl,

I'm busy putting together a script which creates email with multipart alternative parts and attachments. Now all seems to be working fine except for normal attachments, they don't seem to be available for download in Gmail or Yahoo webmail, all my inline images are available but if I attach anything be it a simple .txt file or pdf they are not available for download and I have set the Disposition as attachment.. and I can see it's in the mail as such. Below is how I go about doing it. Is there something else I should be doing?

 my    $ent = MIME::Entity->build(From      => $from,
                             To             => $to,
                             Type           => "multipart/alternative",
                             'X-Mailer'     => undef);
if ($plain_body)
{
    $ent->attach(
    Type            => 'text/plain; charset=UTF-8',
    Data            => $plain_body,
    Encoding        => $textEnc,
  );
}

 $ent->attach(Type => 'multipart/related');
 $ent->attach(
    Type            => 'text/html; charset=UTF-8',
    Data            => $content,
    Encoding        => $htmlEnc,
  );


 if ($count != 0) {
 for my $i (0..$count-1){
 $ent->attach(Data        => $attachment[$i],
             Type         => $content_type[$i],
             Filename     => $attname[$i],
             Id           => $cid[$i],
             Disposition  => $disposition[$i],
             Encoding     => $encoding[$i]);
  }

////\ The Attachment which is not visible in either Yahoo or Gmail webmail..

------------=_1371465849-18979-0
Content-Type: text/plain; name="Careways Final.txt"
Content-Disposition: attachment; filename="Careways Final.txt"
Content-Transfer-Encoding: quoted-printable

Upvotes: 0

Views: 2934

Answers (1)

bjakubski
bjakubski

Reputation: 1747

My knowledge here is lacking and you didn't provide enough code to reproduce that email message, but I think your problem may be that you have all your parts/attachment attached to a single top level multipart/alternative part.

In case you'd have $plain_body and one attachment (so $count = 1) your multipart/alternative message would consit of 4 parts arranged like this:

multipart/alternative:
    text/plain
    text/html
    mutlipart/related (it is empty, you did not attach anything to it!)
    text/plain (or other type, depends what attachment you had

You probably need something in terms of:

multipart/mixed:
    multipart/alternative:
        text/plain
        text/html
    text/plain (first attachment)

So that whole message is of multipart/mixed type and it contains: 1. multipart/alternative with two alternative messages (html and text presumably) 2. any number of "attachments"

In your current version everything is under multipart/alternative, so mail reader considers all your attachments as different versions of the same content and may not provide "download to them"

You can create nested parts structure by calling ->attach methods on parts being result of other attach, for example:

my $top = MIME::Entity->build(
    ...
    Type           => "multipart/mixed",
);
my $alternative = $top->attach(
    ...
    Type    => "multipart/alternative",
);
# Now we add subparts IN the multipart/alternative part instead of using main message
$alternative->attach( ..., Type => "text/plain");
$alternative->attach( ..., Type => "text/html");

# Now we add attachments to whole message again:
for (@attachments) {
    $top->attach( ... );
}

Upvotes: 8

Related Questions