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

Sending E-mail attachment wihout using external module

by DreamT (Pilgrim)
on Nov 18, 2009 at 07:48 UTC ( #807868=perlquestion: print w/ replies, xml ) Need Help??
DreamT has asked for the wisdom of the Perl Monks concerning the following question:

Hi!
My quest is to send a file via e-mail on a system where it's not possible to install a module for the purpose. So, I'm "stuck" with the following:
$mailprog = "/usr/sbin/exim -t -i -f"; open (MAIL, "|$mailprog"); print MAIL "From: Me <me\@myself.com>\n"; print MAIL "To: john\@doe.com\n"; print MAIL "Subject: Test"; print MAIL "This is a test\n"; close(MAIL);
(of course there are other solutions available since there's "more than one way to do it"...)

So, my question is, is it possible to expand the code snippet above to attach a file?

Comment on Sending E-mail attachment wihout using external module
Download Code
Replies are listed 'Best First'.
Re: Sending E-mail attachment wihout using external module
by moritz (Cardinal) on Nov 18, 2009 at 08:04 UTC
    As soon as one of the headers is not a string literal, but rather a variable coming from the outside of your program, it's much harder to correctly deal with it - you have to strip newlines and maybe quotes, escape non-ASCII-characters and so on.

    Which is why I recommend to look at Yes, even you can use CPAN and see if you really can't use a module. MIME::Lite appears to be a pure-Perl module, and thus you should be able to use it everywhere where you can put scripts by yourself.

    Perl 6 - links to (nearly) everything that is Perl 6.
Re: Sending E-mail attachment wihout using external module
by virtualsue (Vicar) on Nov 18, 2009 at 09:36 UTC
    moritz is correct. The odds are very high that if you can write code on this system, then you can install a module with the code. You can learn to do this a lot faster than you can rewrite MIME::Lite (e.g.), though you can always have a look at the source on the CPAN.
Re: Sending E-mail attachment wihout using external module
by ww (Bishop) on Nov 18, 2009 at 12:38 UTC

    You already have some specific answers. These are some additional observations:

    1. ...wihout (sic) using external module
      and
    2. ...where it's not possible to install a module for the purpose.

    As a general rule (though not in this case), the phrase in 1 is justified only when the writer is interested in reinventing the wheel for educational purposes or cannot compile the module in question for the OS in use.

    The clause in 2 is highly unlikely to be true (in terms of desired functionality) on any server for which the writer has the necessary permissions to install a script.

    The reading suggested by moritz provides detail and in the Monastery's Tutorials section, you may find Installing Modules on a Web Server relevant, along with others in Installing Modules.

      Hmmm,

      In general I think you have a point, but beware of generalising too much - there are circumstances e.g. air gapped security sensitive systems, in which the propositions don't hold in a formal sense, but constraints e.g. security certification, on the installation of external, particularly public, code have the direct effect of causing them [the propositions] to appear to hold.

      Been there, seen it, eaten the T-shirt ... and bloody frustrating, but educational (because you are forced to re-invent the wheel in the interests of expediency), it is.

      A user level that continues to overstate my experience :-))

        ++ for an exception.

        But note the qualifiers, "as a general" (which I understand to mean there are exceptions) and "highly unlikely" which (implicitly at least), allows for exceptions?

Re: Sending E-mail attachment wihout using external module
by zentara (Archbishop) on Nov 18, 2009 at 13:05 UTC
    See Answer: How do I send an email with Perl (including an attachment)? or do something as simple as this: (beware there are pitfalls in this method, but it works)
    #!/usr/bin/perl use strict; use warnings; use MIME::Base64; my $SENDFILE = 'z.jpg'; my $FROMUSER = 'zentara'; my $FROMEMAIL = 'zentara@zentara.zentara.net'; my $TOUSER = 'zentara'; my $TOEMAIL = 'zentara@zentara.zentara.net'; open(F_MAIL,"|/usr/sbin/sendmail -t"); my $boundary = "----------90125"; print F_MAIL <<END_OF_MAIL; Precedence: list From: $FROMUSER <$FROMEMAIL> To: $TOUSER <$TOEMAIL> MIME-Version: 1.0 Subject: File attachment test Content-Type: multipart/mixed; boundary=\"$boundary\" This is a multi-part message in MIME format. --$boundary Content-Type: text/plain; charset=\"iso-8859-1\" Here is the body of the message. A file attachment is also provided b +elow. --$boundary Content-Type: application/octet-stream; name=\"$SENDFILE\" Content-Transfer-Encoding: Base64 Content-Disposition: attachment; filename=\"$SENDFILE\" END_OF_MAIL #open(F_SEND,$SENDFILE) || &Error("Cannot open sendfile for MIME encod +ing"); open(F_SEND,$SENDFILE) || die ("Cannot open sendfile for MIME encoding +"); while (read(F_SEND,my $buf, 60 * 57 ) ) { print F_MAIL encode_base64($buf); } close(F_SEND); print F_MAIL <<END_OF_MAIL; --$boundary-- END_OF_MAIL close(F_MAIL);

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (8)
As of 2015-07-30 06:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (270 votes), past polls