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

Hi,
I have a small script using sendmail, I want to send a file to myself but as an attachment. I am able to send this file contents in the e-mail itself but can't figure out how to send it as an attachment. Does anyone know how I can do this? Unfortunately, I do not have perl modules that can probably accomplish this task more easily and I do not have permission to download anything...so I need to stick with what I have now. Thanks a lot!
#!/usr/bin/perl my $sendmail = "/usr/bin/sendmail -t"; my $reply_to = "Reply-to: bozo\n"; my $subject = "Subject: Put the subject here\n"; my $to = "To: myself\@mydomain.com\n"; open(SENDMAIL, "|$sendmail") or die "Cannot open $sendmail: $!"; print SENDMAIL $to; print SENDMAIL $subject; print SENDMAIL "Content-type: text/plain\n\n"; print SENDMAIL `cat /the/file/i/want/to/send`; close(SENDMAIL);

Replies are listed 'Best First'.
Re: how to attach file in sendmail
by brian_d_foy (Abbot) on Feb 19, 2006 at 18:50 UTC

    See one of the many modules on CPAN, such as MIME::Lite. Googling "email attachments perl" yields lots of examples including one from The Perl Cookbook.

    If you have permission to change the script, you probably have permission to install modules in your own directory. If you decide you can't do that, the modules you should use show you how to code the problem. :)

    Good luck!

    --
    brian d foy <brian@stonehenge.com>
    Subscribe to The Perl Review
Re: how to attach file in sendmail
by ambrus (Abbot) on Feb 19, 2006 at 19:05 UTC
Re: how to attach file in sendmail
by stonecolddevin (Parson) on Feb 19, 2006 at 19:01 UTC
Re: how to attach file in sendmail
by hesco (Deacon) on Feb 20, 2006 at 09:04 UTC
    I use mpack at a bash command line for this purpose. To the best of my knowledge it will only permit the attachment of one file at a time. Although munpack will parse out multiple attachments from a single email.

    But the advice above seems sound. You essentailly want to _pack_ your binary data using MIME encoding which converts everything into network safe ascii, to be decoded on the other end.

    -- Hugh