MIME Structure Multipart/Mixed with attachment and Multipart/Alternative

B

blaine

Hello,

I'm trying to send a message that has an attachment as well as either
text or html message depending on the email client. Below is the code
I use to create the structure. However when the email arrives it looks
good except that boundary information is displayed prior to the email
client showing the correct alternative selection.

How can I manipulate this structure so that boundary information is
not displayed when the email client chooses either the text or html
entity for display?


++++CODE++++
my $top = MIME::Entity->build('Type' => "multipart/mixed",
'From' => '(e-mail address removed)',
'To' => '(e-mail address removed)',
'Subject' => 'test Message' );

### Attach stuff to it:
$top->attach(Path => "t.txt",
Type => "text/plain",
Disposition => "attachment",
Encoding => "7bit");

##Build Alternative Entity for email client to select the correct
message
my $ent = MIME::Entity->build('Type' => "multipart/alternative");

$ent->attach(Data => 'test text/plain Message',
Type => 'text/plain',
Encoding => "7bit");

$ent->attach(Data => '<html><body>test <h3>text/html</h3> Message</
body></html>',
Type => 'text/html',
Encoding => "7bit");

## add the alternitive entity to the root entity (multipart/mixed)
$top->add_part( $ent );

## Show the skeleton.. After send the message
$top->dump_skeleton(\*STDERR);
#open MAIL, "| /usr/sbin/sendmail -t -f \"(e-mail address removed)\"" or die
"open: $ !";
#$top->print(\*MAIL);
#close MAIL;
 
B

blaine

Well after reading and re-reading the docs I finally found a solution
to my problem. I just wanted to post it so hopefully it will make
someone else's life easier.

When creating the Entity for the multipart/alternative do not have it
specified as a top level entity. Instead use TOP => 0;

Below is the solution...

use MIME::Entity;

my $top = MIME::Entity->build('Type' => "multipart/mixed",
'From' => '(e-mail address removed)',
'To' => '(e-mail address removed)',
'Subject' => 'Test Message' );


### Attach a plain text file
$top->attach(Path => "t.txt",
Type => "text/plain",
Disposition => "attachment",
Encoding => "7bit");


### Create an new entity to contain both HTML and Text versions of the
### message
my $ent = MIME::Entity->build('Type' => "multipart/alternative",
'Top' => 0,
);

## Attach the text/plain entity to the multipart/alternative entity
$ent->attach(Data => 'test text/plain Message',
Type => 'text/plain',
Encoding => "7bit");

## Attach the text/html entity to the multipart/alternative entity
$ent->attach(Data => '<html><body>test <h3>text/html</h3> Message</
body></html>',
Type => 'text/html',
Encoding => "7bit");


### Attach the Multipart/Alternative Entity to the Multipart/Mixed
Entity
$top->add_part( $ent );

$top->dump_skeleton(\*STDERR);

#open MAIL, "| /usr/sbin/sendmail -t -f \"(e-mail address removed)\"" or die
"open: $ !";
#$top->print(\*MAIL);
#close MAIL;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top