Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Sending email attachments

by Anonymous Monk
on Jul 24, 2003 at 21:33 UTC ( [id://277700]=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 was wondering if I could get some help with sending attachments via email using sendmail. I can get my script to send regular text emails but I'm not sure how to append attachments. Here is my code so far:

my $mailprog = '/usr/lib/sendmail'; foreach my $dirContent ( @dirContents ) { open (MAIL, "|$mailprog $recipient") || die "Can't open $mailprog!\n"; print MAIL "Reply-to:jc\@home.com\n"; print MAIL "Subject: $dirContent\n"; print MAIL"The file $dirContent is attached.\n"; } close (MAIL);

I think I can use send to send out attachments, but I'm not quite sure how this works.

Any ideas? Thanks!

jc

Replies are listed 'Best First'.
Re: Sending email attachments
by MrCromeDome (Deacon) on Jul 24, 2003 at 21:40 UTC
    Have you considered Mail::Sender? Would give you a simpler and more portable way of doing what you want:
    (new Mail::Sender)->MailFile({ smtp => "your.mailserver.com", from => $from_email, to => $to_email, subject => $subject, msg => $message, file => \@filelist });
    @filelist is an array containing a list of file names to attach.

    Cheers!
    MrCromeDome

Re: Sending email attachments
by Willard B. Trophy (Hermit) on Jul 24, 2003 at 21:43 UTC

      I'm out of votes for the day, but I'd give a ++ for Mime::Lite, and -- for talking to Sendmail directly.

      This should do the trick:

      my $msg = MIME::Lite->new( From => 'jc@home.com', To => $recipient, Subject => $dirContent, Data => "The file $dirContent is attached.\n" ); $msg->attach( Path => $dirContent, Type =>'AUTO' ); $msg->send;
Re: Sending email attachments
by Limbic~Region (Chancellor) on Jul 24, 2003 at 22:41 UTC
    Anonymous Monk,
    Out of pure curiosity, does your script do anything other than send the emails that your snippet shows? The reason I ask is because I come from a shell scripting background and still use my command line (KSH) for such tasks:
    $ for file in `ls` > do > (echo The file $file is attached;uuencode $file $file) | mailx - +s "$file" email@address.com > done
    Now don't get me wrong - I am a Perl fiend. My office has running jokes about my obsession since I am the only wannabe JAPH in the office, but I do not find myself turning to Perl for every task that comes up. I was just wondering if this was a similar situation.

    Cheers - L~R

      Won't that send out a seperate email for each file? Don't get me wrong, your solutiuon is elegant, and simple, but for a large number of files, it could be frustrating for the recipient.

      Very funny Scotty... Now PLEASE beam down my PANTS!
        spartan,
        Yes - but that's what the original snippet does. If you wanted to send them all at once (better hope they are small files or that there are no size restrictions along the way), you would simply do:
        $ (for file in `ls` > do > subject="$subject $file" > uuencode $file $file > done) | mailx -s "$subject attached" email@address.com

        Cheers - L~R

Re: Sending email attachments
by geektron (Curate) on Jul 25, 2003 at 05:21 UTC
    i realize you were asking about ways to attach using sendmail directly, but a whole slew of modules exist to avoid having to do so.

    i'm fond of Mail::Mailer, and have used Mail::Sendmail. each has a convenient way of adding attachments.

Re: Sending email attachments
by rupesh (Hermit) on Jul 25, 2003 at 08:20 UTC
    Hi
    Try this:
    use MIME::QuotedPrint; use MIME::Base64; use Mail::Sendmail 0.75; # doesn't work with v. 0.74! %mail = ( from => 'you@domain.tld', to => 'whoever@someplace.tld', subject => 'Test attachment', ); $boundary = "====" . time() . "===="; $mail{'content-type'} = "multipart/mixed; boundary=\"$boundary\""; $message = encode_qp( "Some message" ); $file = $^X; # This is the perl executable open (F, $file) or die "Cannot read $file: $!"; binmode F; undef $/; $mail{body} = encode_base64(<F>); close F; $boundary = '--'.$boundary; $mail{body} = <<END_OF_BODY; $boundary Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable $message $boundary Content-Type: application/octet-stream; name="$^X" Content-Transfer-Encoding: base64 Content-Disposition: attachment; filename="$^X" $mail{body} $boundary-- END_OF_BODY sendmail(%mail) || print "Error: $Mail::Sendmail::error\n";
    Enjoy!


    we're born with our eyes closed and our mouths wide open, and we spend our entire life trying to rectify that mistake of nature. - anonymous.
      Hi, this doesn't seem to work for outlook !. No file gets attached. The body contains the content itself

Log In?
Username:
Password:

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

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

    No recent polls found