Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

give perl an email

by skazat (Hermit)
on Apr 17, 2000 at 23:42 UTC ( #7875=perlquestion: print w/ replies, xml ) Need Help??
skazat has asked for the wisdom of the Perl Monks concerning the following question:

hey everyone heres a question: how do i email a perl script and have the script do something super fantastic with the data? i set up an alias qmail address that pipes data to the script, somthing like |home/justin/www/cgi-bin/givememail.cgi and then tried to get it back with this my $email = <STDIN> apparently, theres more to it ;) i'm using qmail on my system (BSD 3.something or other) anyone gots some good majordomo/mhonarc experiance that can unravel this mystery (play scary music) thanks again, justin

Comment on give perl an email
Re: give perl an email
by btrott (Parson) on Apr 17, 2000 at 23:47 UTC
    If you're piping to the script, then you need to read the message from STDIN. So you'd do something like this, although if you're doing something spectacular, you'd want to modify this a bit :).
    open OUT, ">>mail.out" or die "Can't open: $!"; print OUT while <STDIN>; close OUT;
    At least this is how it works on my system.
Re: give perl an email
by pschoonveld (Pilgrim) on Apr 18, 2000 at 00:49 UTC
    Well, not being familiar with qmail, my guess is that it doesn't actually take the data and mail it to $email. I would start by seeing what happens when you print data out to qmail. like while (<STDIN>) { print "taco\n";} If it magically gives you 'taco' (highly unlikely), you probably just need to email the results to whereever you want. Generic Mail Form Processor will show you how to do that.
      thats not what i'm asking ;) i want the other way... i want to send an email to perl and have perl do something with it, instead of using perl to send an email ;) think about majordomo, how you send commands to it by email and it gets back to you, that sort of stuff. it seems like a cool way to interact with a script.. or maybe just an old way...
        Right. Did you look at my response? Try reading from STDIN.

        By the way, you might want to rephrase your question-- it's not very clear (in fact, it's sort of meaningless) to say that you want to "send an email to perl". That just doesn't mean anything.

        What you want to do, as far as I can tell, is to have all incoming email messages handled by a Perl script. So, for example, if I set up a Perl script to handle my mail, all incoming mail would be piped to that Perl script, and it could do whatever it wished with the contents of the messages.

        You might want to take a look at Mark-Jason Dominus's My Life with Spam series. Part One is up on his site, but I can't seem to get to plover.com, so it's also on LinuxPlanet; Part Two and Part Three are both up on perl.com. The articles deal with filtering spam from your mail, but you may be able to get something useful out of them. Here's a part that deals specifically with how the mail message gets into your program.

Re: give perl an email
by chromatic (Archbishop) on Apr 18, 2000 at 01:48 UTC
    Not too bad, if you've read perlman:perlipc. Here's a little demo that (only works on Unix?) prints out everything you send to it. Astute applicants will know where to go from here:
    #!/usr/bin/perl -w use strict; my $FIFO = "$ENV{HOME}/pipe"; while (1) { unless (-p $FIFO) { unlink $FIFO; system('mknod', $FIFO, 'p') && die "Can't make pipe $FIFO: $!"; } open (FIFO, "$FIFO") || die "Can't open $FIFO: $!"; while (<FIFO>) { chomp; print "Command sent: $_\n"; } close FIFO; }
    This creates a new named pipe in your home directory named, appropriately enough, pipe. You can echo commands to it or attach it at the end of any pipe. It's not terribly exciting, but it's the basics of what I think you want to do.
      On a side note:
      Yeah, it only works on *nix... Notice the system()? Its pretty safe to say that whenever you do that, you're going to find serious portability issues.

      Then again, I've never seen qmail for anything not *nix... :o)
Re: give perl an email
by jbert (Priest) on Apr 18, 2000 at 15:54 UTC
    You want to send email to a process. You want that process to be implemented by a perl script.

    This amounts to needing to tell your local mail delivery system that instead of sending mail for a particular address to a user's mailbox it should instead be delivered to a process.

    "Being delivered to a process" means different things. You could, for example, have the mail delivered normally to a mailbox and go through the mailbox with a perl module. But thats fugly. "We don't want to give you that".

    If you've got a POP or IMAP server you could deliver the mail to a mailbox and then slurp it into a perl script using the Net::POP3 or similar module. Less ugly, but still too ugly to live. "But we don't want to give you that".

    If you were using sendmail as your local mail delivery system, it honours something called '.forward' files. (This is UNIX, right?) I know you aren't using sendmail, but stay with me here.

    You create a unix user id corresponding to the script you want to run and create a file called '.forward' in that user's home directory. The original purpose of such a file was so you could put another email address in here and have all your mail forwarded to that address.

    Instead of forwarding to another email address you can instead put in the name of an executable command, for example a perl script.

    The email gets handed to the process on standard input. Typically in the form of "headers + blank line + body". There are perl modules to help you grok these things.

    The '.forward' concept applies to sendmail, but because of sendmail's previous ubiquity, is also supported by many other mail systems. I don't know if qmail is one of these but I would hazard a guess that it will either support this as is (i.e. it will 'just work') or grepping the qmail docs for '.forward' will help you.

    Of course, there are probably better ways of doing this depending on a) how much access you have to the local system (above idea does not require root) and b) how much you want to read the docs.

    For example, sendmail also supports configurable local delivery agents, so cool tools like procmail can be run more efficiently and securely by the mailer.

    Basically its a qmail issue if you are using qmail. It will have some mechanism for delivery to a process and the above posted comments about reading standard input will then apply.

    Have fun

Re: give perl an email
by httptech (Chaplain) on Apr 18, 2000 at 17:25 UTC
    If you want to go OO you can use Mail::Internet

    However, if you're just wanting to put mail into a Mhonarc archive, here's some code I use to do that:

    my $mhonarc = "/usr/local/bin/mhonarc"; # Path to Mhonarc executable my $archive = "/home/httpd/html/archive"; # Path to archive my $rcfile = "/home/httpd/mhonarc.rc"; # Path to Mhonarc rcfile my ($m,$y) = (localtime(time))[4,5]; $m++; $y+=1900; my $month = sprintf("%04d%02d", $y, $m); # make a directory name like +YYYYMM unless (-e "$archive/$month") { mkdir("$archive/$month", 0775) || die "Couldn't make new archive dir: $!\n"; } open(OUT, "|$mhonarc -add -rcfile $rcfile -outdir $archive/$month -qui +et") || die "Couldn't open mhonarc: $!\n"; while(<STDIN>) { print OUT; } close (OUT);
    Make sure the mail daemon has permission to run this script, and that it has permission to create subdirectories in your archive directory.
thanks everyone!
by skazat (Hermit) on Apr 18, 2000 at 22:11 UTC
    thanks everyone for your help! i found the answer in the article my life with spam as someone graciously pointed out. so i had perl bounce an email back to me, since if i know the info is passed, i can do anything with the data ;)

    i have qmail on a freebsd server, so my .qmail file looked like this:

    |/usr/home/justin/wwwdev/cgi-bin/mojo/take_mail.cgi

    and my little program looks like this: (lots of code... ok almost all the code from the soam article)

     
    
    #!/usr/bin/perl 
    #take_mail.cgi
    use strict; 
    my ($header, $body); 
    
    my $recieved = "/usr/home/justin/wwwdev/cgi-bin/mojo/recieved.html";
    my$mailprog = "/usr/sbin/sendmail";
    { local $/ = ""; 
     $header = <STDIN>; 
     undef $/; 
    $body = <STDIN>; 
    } 
    
    open(MAIL,"|$mailprog -t");
    print MAIL "From: <justin\@skazat.com>\n";
    print MAIL "To: <justin\@skazat.com>\n";
    print MAIL "Subject: perl wants to  says hi to you all\n\n";
    print MAIL "header: $header\n";
    print MAIL "body: $body\n\n";
    close (MAIL);
    

    and thats all there is too it, thanks everyone!

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (12)
As of 2014-09-18 20:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (124 votes), past polls