Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Sending e-mail with attachments.

by Anonymous Monk
on Jun 08, 2004 at 18:10 UTC ( [id://362472]=perlquestion: print w/replies, xml ) Need Help??

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

I was reading Perl Script, MS OutLook and saw a link posted to How do I send e-mail from my Perl Program?. I used the example code and it worked (see below). Can I send e-mail with attachments (Excel, Word, etc.) using this stanard version of Perl (This is perl, v5.6.1 built for MSWin32-x86-multi-thread)? That is, can I do it without CPAN downloads?

Thanks.

use strict; use warnings; use Net::SMTP; my $smtp = Net::SMTP->new('MAILSERVER') or die $!; my ($from, $to) = ('blah@domain.com', 'user@otherdomain.com'); my ($subject, $message) = ('Test subject', 'Test message'); $smtp->mail( $from ); $smtp->to( $to ); $smtp->data(); $smtp->datasend("To: $to\n"); $smtp->datasend("From: $from\n"); $smtp->datasend("Subject: $subject\n"); $smtp->datasend("\n"); # done with header $smtp->datasend($message); $smtp->dataend(); $smtp->quit(); # all done. message sent.

Replies are listed 'Best First'.
Re: Sending e-mail with attachments.
by periapt (Hermit) on Jun 08, 2004 at 18:55 UTC
    I don't think that Net::SMTP allows for attachments but the module Mime::Lite does. Mime::Lite is part of Activestate's core distribution. Below is a very stripped down example of using the module. I copped it out of a running program and took out the program specific variables and references so it may not work right out of the can but it should give you an idea.
    #!/usr/local/ActivePerl-5.6/bin/perl # Compiler directives and Includes use strict; use warnings; use diagnostics; use Mime::Lite; # Constants and pragmas use constant TRUE => scalar 1; use constant FALSE => scalar 0; #####################################################################; #main() { my @to = (); my @cc = (); my @bcc = (); my $subject = ''; my %mailhdr = (); my $msg = ''; my $body = ''; my $mailsvr = ''; my $filename = ''; my $path = ''; my $type = ''; my $tmp = ''; my $from = ''; my %mimetype = ('zip' =>'application/zip', 'pdf'=>'application/pdf', 'html'=>'text/html', 'txt'=>'text/plain', 'ps' =>'application/postcript', 'tgz'=>'application/gzip', 'tab' =>'text/tab-separated-values', 'htm'=>'text/html', 'rtf'=>'text/richtext', 'doc'=>'application/msword', 'gzip'=>'application/gzip', 'gz' =>'application/gzip',); $mailhdr{From} = "$from"; $mailhdr{To} = \@to; $mailhdr{Cc} = \@cc if scalar @cc; $mailhdr{Bcc} = \@bcc if scalar @bcc; $mailhdr{Subject} = "$subject"; $mailhdr{Type} = 'multipart/mixed'; # indicates attachments $msg = MIME::Lite->new( %mailhdr ); $msg->attach(Type =>'TEXT', Data =>"$body" ); $tmp = (exists($mimetype{$type})) ? $mimetype{$type} : 'application/octet-stream' ; $msg->attach(Type =>"$tmp", Path =>"$path", Filename =>"$filename", Disposition => 'attachment' ); MIME::Lite->send('smtp', "$mailsvr", Timeout=>60); eval{$msg->send}; # send email exit 0; } #end main() ######################################################################

    PJ
    We are drowning in information and starving for knowledge - Rutherford D. Rogers
    What good is knowledge if you have to pull teeth to get it - anonymous
Re: Sending e-mail with attachments.
by drewbie (Chaplain) on Jun 08, 2004 at 19:26 UTC
    I've included a shorter snippet from production code below. You can combine the object instantiation & text part of the email in new(). The attach param is an array ref of attachments. In my case, I have the data in memory from a dynamically generated PDF, but I believe you can also give a path to a file as well.

    One option to attach() is Filename, which I recommend you use to set a sane name for the attachment, ie. "Your_documents.pdf". You'll need some code to set $mime_type. My code only attaches PDF files, so I just hardcoded it.

    sub send_mime { my $class = shift; my %args = (from=>'', to=>'', subject=>'Default Subject', body=>'', attach=>[], # [{data=>\$content, name=>'foo'}] @_, ); # 1. create main body of email my $body = $args{body}; $body = ref($body) ? $$body : $body; my $msg = MIME::Lite->new(From=>$args{from}, To=>$args{to}, Subject=>$args{subject}, Type=>'TEXT', Data=>$body, ); # 2. attach file(s) foreach my $attach ( @{$args{attach}} ) { my $data = $attach->{data}; $msg->attach(Type=>$mime_type, Data=>$$data, Filename=>$attach->{name}, ); } # 3. deliver email $msg->send(); }
Re: Sending e-mail with attachments.
by eclark (Scribe) on Jun 08, 2004 at 18:30 UTC
    You can do it without a CPAN module, but you'll have to write a great deal of code. It is not easy.
Re: Sending e-mail with attachments.
by vek (Prior) on Jun 08, 2004 at 18:58 UTC

    ...can I do it without CPAN downloads?

    Perhaps it would help if you explained why you need to do it without CPAN downloads. MIME::Lite is your friend.

    c:\>ppm install Mime-Lite

    -- vek --
Re: Sending e-mail with attachments.
by runrig (Abbot) on Jun 08, 2004 at 19:17 UTC
    That is, can I do it without CPAN downloads?
    Yes, you can copy and paste the code from MIME::Lite or Mail::Sender into your code. Though IMHO it would make more sense to just install one of those modules.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (4)
As of 2024-04-16 19:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found