Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Mail Attachment.

by Anonymous Monk
on Dec 15, 2003 at 16:10 UTC ( [id://314837]=perlquestion: print w/replies, xml ) Need Help??

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

Hi Monks,

I am trying to send an Excel file via perl. With following code, I can send the file, but "my_message" is never get printed and the Excel file' cannot be opened. (It gives some bogus cannot access/read-only file error messages)

use MIME::Entity; use Mail::Send; @my_message = qw(Hello world Look at my attachment); $msg = MIME::Entity->build(From => 'me@myhost.com', Type => "multipart/mixed", To => 'user@company.com', Subject => "Hello, ! ", Data => \@my_message); $msg->attach(Path => 'account.xls', Encoding => '-SUGGEST', Disposition => "attachment", '-X-Mailer' => undef, ); open SENDMAIL, "|/usr/lib/sendmail -t" or die $!; $msg->print(\*SENDMAIL); close SENDMAIL;

Replies are listed 'Best First'.
Re: Mail Attachment.
by barrd (Canon) on Dec 15, 2003 at 17:56 UTC
    "my_message" is never get printed...
    Nor in its current state would you expect it to, why have you put it in an array? Change it to a string like so:
    ... $my_message = 'Hello world Look at my attachment'; $msg = MIME::Entity->build(From => 'me@myhost.com', Type => "multipart/mixed", To => 'user@company.com', Subject => "Hello, ! ", Data => $my_message);
    There, that should've worked.

    It gives some bogus cannot access/read-only file error messages...
    Adding the actual error message might have helped but as a quick guess I'd say check the documentation for the attach() paramater: Encoding. That's probably the cause?

Re: Mail Attachment.
by zentara (Archbishop) on Dec 15, 2003 at 17:40 UTC
    Here is an Excel sending script which was posted by someone awhile ago:
    #!/usr/bin/perl use MIME::Entity; use Net::SMTP; # create the multipart message with attachments $msg = MIME::Entity->build( Type => 'multipart/mixed', From => 'bogus@mail.com', # The "To" here is displayed in the message header as "To" # This is not the actual list of recipients To => '"Display Name" <user1@mail.com>, "Second Person" <user2@mail.com>', Subject => 'Automatic email of Excel report', ); $msg->attach( Type => 'application/msexcel', Path => $report, Filename => 'college_orders.xls', Encoding => 'base64' ); $msg->attach( Data => "Enclosed is the daily report of orders. (automated delivery)" ); # send the message $smtp = Net::SMTP->new("smtp.mail.com"); #authenticate if required $smtp->auth( "login", "passwd" ); # Identify yourself to the smtp server $smtp->mail('bogus@mail.com'); # The syntax for "To" is different in MIME::Entity and Net::SMTP # The "to" here is the list of actual recipients and is not displayed $smtp->to( 'user1@mail.com', 'user2@mail.com' ) || die "Bad address"; # Send the message and attachments $smtp->data( [ $msg->as_string ] ) || die "mail not accepted"; $smtp->quit; exit;

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://314837]
Approved by Paladin
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (3)
As of 2024-03-28 17:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found