Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

help help monks! peace has left my house! my sanity is wearing thin!

I am using mime tools to assemle an email with a body and a text file attachment. However, the text file is just being inserted into the body of the email.

my $entity = MIME::Entity->build ( Type => 'multipart/mixed', From => 'email', To => 'address', Subject => "blah", ) or die "Error creating MIME entity: $!\n"; $entity->attach( Type => 'text/plain', #Encoding => 'base64', Data=>join("\n",@msg_body) ); $entity->attach( Filename => "report.txt", Type =>'text/plain', Data => join("\n",@attach) ) or die "Error adding the text message part: $!\n"; $entity->smtpsend;
thats what I've got..any help, please! I've been going through the mime-tools docs and trying lots of variations but no luck yet.

thanks!

20041011 Edit by ysth: chage br tags to p and code

Replies are listed 'Best First'.
Re: mime tools attachments
by atcroft (Abbot) on Oct 12, 2004 at 04:57 UTC

    I'm not that familiar with mime-tools, but I'll take a stab at your problem, and hopefully I can help.

    First of all, my testing code, and its output (mostly taken from what you posted).

    Code:

    Output:

    I then compared this with the source for an email in which I sent a coworker a perl script, in which the headers for that (which does show up as an attachment) are as follows:

    Content-Disposition: attachment; filename=blah.pl Content-Type: text/x-perl; name=blah.pl; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit

    I suspect strongly that the issue is that in the results of your code, the Content-Disposition is 'inline', rather than 'attachment', suggesting to the mail client to attempt to display it rather than show it as an attachment. So, in the code, I changed the attach line for 'report.txt' to the following, which seems to generate the appropriate entry:

    $entity->attach( Disposition => 'attachment', Filename => "report.txt", Type => 'text/plain', Data => join( "\n", @attach ) ) or die "Error adding the text message part: $!\n";

    Having done that, the headers for that section became:

    Content-Type: text/plain; name="report.txt" Content-Disposition: attachment; filename="report.txt" Content-Transfer-Encoding: binary

    I'm not sure that is quite the preferred way to do it, but it seems as if it might help. You would need to test to verify, however.

    I hope that helps.

Re: mime tools attachments
by neeraj (Scribe) on Oct 12, 2004 at 10:30 UTC
    Try out this code,
    $msg = MIME::Lite->new ( From => $from_address, To => $address, Cc => $cc_address, Subject => $subject, "Reply-To:" => $reply_address, Type =>'multipart/mixed' ) or die "Error creating multipart container: $!\n"; $msg->attach ( Type => 'TEXT', Data => $message_body ) or die "Error adding the text message part: $!\n"; $msg->attach ( Type => 'application/octet-stream', Path => $my_file, Filename => $your_file, Disposition => 'attachment' ) or die "Error adding $my_file: $!\n"; ### Send the Message MIME::Lite->send('smtp', $mail_host, Timeout=>60); $msg->send;