Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Sendmail .forward + perl

by IndyZ (Friar)
on Mar 24, 2000 at 04:19 UTC ( [id://6018]=perlquestion: print w/replies, xml ) Need Help??

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

I have my sendmail .forward file set up like this:
"|/home/brian/bin/mailfwd"
That works. Send mail runs the program when it get's mail for me. My problem is that when the program runs, it doesn't write everything it receives to a file, like it should. Here is the program:
#!/usr/bin/perl open (MAIL, ">/home/brian/mail.txt"); print MAIL "\n"; while (<STDIN>) {print MAIL $_}

The file never gets written. Whats the problem?

Replies are listed 'Best First'.
Re: Sendmail .forward + perl
by btrott (Parson) on Mar 24, 2000 at 04:41 UTC
    1) How do you know for sure that the program is getting executed?

    2) What user is the script running as? Does it have the correct permissions?

    3) With that in mind, you should be checking the return of open (always check the returns of system calls!):

    open (MAIL, ">/home/brian/mail.txt") or die "Can't open: $!"; print MAIL "\n"; while (<STDIN>) {print MAIL $_} close MAIL or die "Can't close: $!"
    Is the file ever created, even? Is anything ever written to it?
      I have to say it's probably number 2.
      [root@archaia /root]# su nobody [nobody@archaia /root]$ perl -e 'open (MAIL, ">./foo");print MAIL "scr +atchymonkey.\n" ;' [nobody@archaia /root]$ exit [root@archaia /root]# ls bin mod_perl_docs [root@archaia /root]#
      Note how there's no error at all, but the file isn't there.
      Let's try it with -w :
      [nobody@archaia /root]$ perl -we 'open (MAIL, ">./foo");print MAIL "sc +ratchymonkey.\n ";' print on closed filehandle main::MAIL at -e line 1.
      Hmm, what's going on there?
      [nobody@archaia /root]$ perl -we 'open (MAIL, ">./foo") || die "$!";pr +int MAIL "scrat chymonkey.\n";' Permission denied at -e line 1.
      What user does sendmail run as on your box?
      Check the open() as above...
Re: Sendmail .forward + perl
by Anonymous Monk on Mar 24, 2000 at 11:10 UTC
    don't forget to "close MAIL"
RE: Sendmail .forward + perl
by Anonymous Monk on Mar 24, 2000 at 13:01 UTC
    There are some things you could try. First, verify that the file really can be written to: open(">file") || die "couldn't open file: $!"; Alternatively, if the script just hangs at the end, you could try unbuffered output: $| = 1; And of course, you should always close the file when you're done with it, although this should be implicit. Hope this helps...
Re: Sendmail .forward + perl
by Anonymous Monk on Mar 24, 2000 at 20:37 UTC
    As written, your mail.txt file is only ever going to have the most recent message in it if you open the file handle for writing:
    open(MAIL, ">/home/brian/mail.txt");
    
    if you want a spool file (list of all messages), you'd need to open the file handle for appending:
    open(MAIL, ">>/home/brian/mail.txt");
    
    While you're at it, you should probably lock the file while you are writing to it, so the next piece of inbound mail doesn't try to screw with your mail.txt file while you are already writing to it.

    (this code is ripped from OraPP2ndEd, p 166-7)

    flock MAIL, 2;
    seek MAIL, 0, 2; # skip to the end for spooling
    while (<STDIN>) {print MAIL $_;}
    flock MAIL, 8; 
    close MAIL || die($!);
    
    Note that flock can't be tested with die, since it waits until the file is "freed up" for locking. If your system doesn't implement flock, you'll get a fatal runtime error, AFAIK
Re: Sendmail .forward + perl
by IndyZ (Friar) on Mar 25, 2000 at 02:57 UTC
    Your're right, i'm now pretty sure it isn't running. My sendmail is really rusty. Anybody out there who can tell me how to fix the problem of my .forward program not running as me?
    =Brian
      Instead of trying to make sendmail run as you (a practice frought with peril), I suggest you change the permissions of your file so that Sendmail can write to it. Consult the UNIX manpages on chgrp and chown to learn how to do this.

      In the meantime, put an "or die $!" after that open() statement, as a bare minimum...

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2024-04-25 16:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found