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

Re: Sending e-mail with attachments.

by drewbie (Chaplain)
on Jun 08, 2004 at 19:26 UTC ( [id://362512]=note: print w/replies, xml ) Need Help??


in reply to Sending e-mail with attachments.

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(); }

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (6)
As of 2024-04-24 15:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found