MIME Structure Multipart/Mixed with attachment and Multipart/Alternative

Discussion in 'Perl Misc' started by blaine@worldweb.com, Apr 3, 2007.

  1. Guest

    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' => '',
    'To' => '',
    '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 \"\"" or die
    "open: $ !";
    #$top->print(\*MAIL);
    #close MAIL;
     
    , Apr 3, 2007
    #1
    1. Advertising

  2. Guest

    Re: MIME Structure Multipart/Mixed with attachment and Multipart/Alternative

    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' => '',
    'To' => '',
    '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 \"\"" or die
    "open: $ !";
    #$top->print(\*MAIL);
    #close MAIL;
     
    , Apr 4, 2007
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. c# newbie
    Replies:
    3
    Views:
    5,894
  2. CJ
    Replies:
    1
    Views:
    1,573
    Andrew Thompson
    Oct 29, 2004
  3. Xeth  Waxman
    Replies:
    0
    Views:
    9,165
    Xeth Waxman
    Dec 9, 2004
  4. Arvin Portlock
    Replies:
    0
    Views:
    191
    Arvin Portlock
    May 3, 2006
  5. ecureuil
    Replies:
    0
    Views:
    327
    ecureuil
    May 28, 2006
Loading...

Share This Page