Re: Getting Perl scripts to work as mail filters
On Thu, Dec 19, 2002 at 01:13:17AM +0100, Mxsmanic wrote: What about appending directly to the mailbox file under /var/mail/$USER with the script? It looks like the format of the file is very simple. Why wouldn't that work? I've tried writing to it, but I can't, at least not when the script runs from the aliases file (works fine when I run it myself, though). mail.local(8) is probably what you want to use to do that. However your main problem will be getting sendmail to setuid() to the required user before calling the mymail script. Cheers, Matthew -- Dr Matthew J Seaman MA, D.Phil. 26 The Paddocks Savill Way Marlow Tel: +44 1628 476614 Bucks., SL7 1TH UK To Unsubscribe: send mail to [EMAIL PROTECTED] with unsubscribe freebsd-questions in the body of the message
Re: Getting Perl scripts to work as mail filters
I finally got it to work by piping to sendmail. Seems to be adequate for what I want to do. Now all I have to do is design my patented, foolproof methodology for distinguishing spam from real mail so that I can encode the rules in the script. There must be a way! Also, this may allow me to do something about getting rid of HTML in mail, which I can't stand. - Original Message - From: Matthew Seaman [EMAIL PROTECTED] To: FreeBSD Questions [EMAIL PROTECTED] Sent: Thursday, December 19, 2002 09:14 Subject: Re: Getting Perl scripts to work as mail filters On Thu, Dec 19, 2002 at 01:13:17AM +0100, Mxsmanic wrote: What about appending directly to the mailbox file under /var/mail/$USER with the script? It looks like the format of the file is very simple. Why wouldn't that work? I've tried writing to it, but I can't, at least not when the script runs from the aliases file (works fine when I run it myself, though). mail.local(8) is probably what you want to use to do that. However your main problem will be getting sendmail to setuid() to the required user before calling the mymail script. Cheers, Matthew -- Dr Matthew J Seaman MA, D.Phil. 26 The Paddocks Savill Way Marlow Tel: +44 1628 476614 Bucks., SL7 1TH UK To Unsubscribe: send mail to [EMAIL PROTECTED] with unsubscribe freebsd-questions in the body of the message To Unsubscribe: send mail to [EMAIL PROTECTED] with unsubscribe freebsd-questions in the body of the message
Re: Getting Perl scripts to work as mail filters
On Thu, 19 Dec 2002, Mxsmanic wrote: I finally got it to work by piping to sendmail. Seems to be adequate for what I want to do. Now all I have to do is design my patented, foolproof methodology for distinguishing spam from real mail so that I can encode the rules in the script. There must be a way! Also, this may allow me to do something about getting rid of HTML in mail, which I can't stand. There's nothing wrong with reinventing procmail like this, but I'm curious why you consider it bloated--it's under 64K on my system. Anyway, here's a procmail recipe that catches a lot of spam. Not all, but it's a start, and the regular expression should be usable in your Perl program with few changes: # Mail not addressed to me. :0 * $!^((Resent-|Apparently-)?(To|Cc|Bcc)):.*${LOGNAME} $MAILDIR/Spam-folder -Warren Block * Rapid City, South Dakota USA To Unsubscribe: send mail to [EMAIL PROTECTED] with unsubscribe freebsd-questions in the body of the message
Re: Getting Perl scripts to work as mail filters
On Fri, 20 Dec 2002, Mxsmanic wrote: *** There's nothing wrong with reinventing procmail like this, but I'm curious why you consider it bloated--it's under 64K on my system. *** Bloated in the sense of complexity. My script is one file; I install it by changing one line in /etc/mail/aliases. Procmail cannot compete with that. Um... install the port, create a .forward file, and set up .procmailrc? There are more complex ways to use it, but I haven't tried them. I looked at some packaged versions of procmail setups, and found them too complex to mess with--scoring and weighting and stuff. My setup just filters things, and does a pretty good job of it. The nice thing about procmail is that it has all the hard stuff already handled--dealing with headers or the body of the message, locking, and so on. -Warren Block * Rapid City, South Dakota USA To Unsubscribe: send mail to [EMAIL PROTECTED] with unsubscribe freebsd-questions in the body of the message
Re: Getting Perl scripts to work as mail filters
On Wed, Dec 18, 2002 at 02:46:14PM +0100, Mxsmanic wrote: What do I have to do to make a simple Perl script filter incoming mail for a mailbox? I wrote a script that just reads standard input and writes it to standard output, then put it in my home directory, then changes /etc/mail/aliases to point to it, like this: mymail:|/usr/home/mymail/perlfilter But when I actually send mail to this address, it just disappears--no error message, nothing. When I execute the same program by hand, it works fine. If I remove the alias, the mail is delivered just fine, too. What am I missing?? The Perl program has 755 for permissions and has mymail:users as its userid and group. The first line points to the location of Perl in the usual way. I am at a loss for ideas as to why this isn't working. The sendmail manual seems to indicate that it just pipes the e-mail message into this script, then delivers whatever comes out of standard output, but it doesn't seem to work. I believe that sendmail will consider the e-mail to have been delivered if it can pipe it into your script and the script returns a successful exit code. It won't treat the script as a filter: that is, it won't read the script's output and treat that as a message to deliver. Anything your script writes to stdout will be silently discarded. If you want that re-mailing effect then you're going to have to make the script fire up another instance of sendmail, and pipe the output message from the perl script into sendmail yourself. Beware of mail loops though --- make sure you modify the 'To:' header of the message as it passes through the filter sufficiently that it doesn't hit the alias expansion again. Alternatively, take a look at procmail(8) which can be used to pass a message through a filter program and then re-send it to a new address. Cheers, Matthew -- Dr Matthew J Seaman MA, D.Phil. 26 The Paddocks Savill Way Marlow Tel: +44 1628 476614 Bucks., SL7 1TH UK To Unsubscribe: send mail to [EMAIL PROTECTED] with unsubscribe freebsd-questions in the body of the message
RE: Getting Perl scripts to work as mail filters
-Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]]On Behalf Of Mxsmanic What do I have to do to make a simple Perl script filter incoming mail for a mailbox? I wrote a script that just reads standard input and writes it to standard output, then put it in my home directory, then changes /etc/mail/aliases to point to it, like this: mymail:|/usr/home/mymail/perlfilter But when I actually send mail to this address, it just disappears--no error Are you sure there is no error - did you check the sendmail log? You will need to ensure the script is listed in your sm.bin directory (man smrsh). Briefly: in /etc/mail/aliases: mymail: | perlfilter in sm.bin (location varies - possibly /usr/libexec/sm.bin) ln -s /path/to/my/perfilter perlfilter Cheers, Barry To Unsubscribe: send mail to [EMAIL PROTECTED] with unsubscribe freebsd-questions in the body of the message
Re: Getting Perl scripts to work as mail filters
'Are you sure there is no error - did you check the sendmail log?' Yup. maillog mentions the message being sent, but indicates no error. 'You will need to ensure the script is listed in your sm.bin directory (man smrsh).' I thought smrsh was not enabled by default (?). Also, if it couldn't find it at all, wouldn't I get an error or something? I even tried changing the script to write directly to /var/mail/mymail to append the message to the actual mailbox, but that does nothing--no effect. Works fine from the command line, though. To Unsubscribe: send mail to [EMAIL PROTECTED] with unsubscribe freebsd-questions in the body of the message
RE: Getting Perl scripts to work as mail filters
-Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]]On Behalf Of Mxsmanic 'Are you sure there is no error - did you check the sendmail log?' Yup. maillog mentions the message being sent, but indicates no error. Can you post the log entry - does it say mailer=prog ? - Barry To Unsubscribe: send mail to [EMAIL PROTECTED] with unsubscribe freebsd-questions in the body of the message
Re: Getting Perl scripts to work as mail filters
Here are the two relevant log entries after I tried to mail something from the console: Dec 18 18:31:10 myserver sendmail[97621]: gBIHVAo97621: from=root, size=39, class=0, nrcpts=1, msgid=[EMAIL PROTECTED], relay=root@localhost Dec 18 18:31:10 myserver sendmail[97623]: gBIHVAo97621: to=|/usr/home/mymail/mailman, ctladdr=mymail (1/0), delay=00:00:00, xdelay=00:00:00, mailer=prog, pri=30039, dsn=2.0.0, stat=Sent So it does say mailer=prog, which means what? - Original Message - From: Barry Byrne [EMAIL PROTECTED] To: Mxsmanic [EMAIL PROTECTED]; FreeBSD Questions [EMAIL PROTECTED] Sent: Wednesday, December 18, 2002 15:28 Subject: RE: Getting Perl scripts to work as mail filters -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]]On Behalf Of Mxsmanic 'Are you sure there is no error - did you check the sendmail log?' Yup. maillog mentions the message being sent, but indicates no error. Can you post the log entry - does it say mailer=prog ? - Barry To Unsubscribe: send mail to [EMAIL PROTECTED] with unsubscribe freebsd-questions in the body of the message To Unsubscribe: send mail to [EMAIL PROTECTED] with unsubscribe freebsd-questions in the body of the message
RE: Getting Perl scripts to work as mail filters
-Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]]On Behalf Of Mxsmanic Here are the two relevant log entries after I tried to mail something from the console: Dec 18 18:31:10 myserver sendmail[97621]: gBIHVAo97621: from=root, size=39, class=0, nrcpts=1, msgid=[EMAIL PROTECTED], relay=root@localhost Dec 18 18:31:10 myserver sendmail[97623]: gBIHVAo97621: to=|/usr/home/mymail/mailman, ctladdr=mymail (1/0), delay=00:00:00, xdelay=00:00:00, mailer=prog, pri=30039, dsn=2.0.0, stat=Sent So it does say mailer=prog, which means what? Well it looks like your script is being run - what should your script do? sendmail manual seems to indicate that it just pipes the e-mail message into this script, then delivers whatever comes out of standard output, but it doesn't seem to work. the script should run with the mail message (headers + body) supplied on STDIN. sendmail doesn't do anything with the STDOUT from your script. I'm nut sure what you mean by 'delivers whatever comes out' - where would it deliver it? If you need output to go somewhere such as a file or emailed somewhere, you will need to explicitly do this. if you want to quickly to assue yourself that the script is running, try making it create a file in /tmp or somewhere else that it has permissions - sendmail is should not be running as root (probably 'mailnull'). - Barry To Unsubscribe: send mail to [EMAIL PROTECTED] with unsubscribe freebsd-questions in the body of the message
Re: Getting Perl scripts to work as mail filters
*** Well it looks like your script is being run - what should your script do? *** Heck, it's so simple I can list it here: --- #!/usr/bin/perl while ($inputline = STDIN) { $inputline =~ s/18 Dec 2002/29 Dec 2013/; print STDOUT $inputline; } --- The s/// stuff is just to give me some proof that the script saw the message. I'm not getting anything at all now, though. I assumed that writing to STDOUT would somehow get the message delivered, but maybe not. But if that's not the way to get it delivered, what is? The sendmail manual is not at all clear on this. I looked in sendmail.cf for the name of some program to which perhaps I should pipe STDOUT in the aliases file, but couldn't find anything. When the aliases file does not specify piping of messages to some special program, who normally delivers them? *** sendmail doesn't do anything with the STDOUT from your script. I'm nut sure what you mean by 'delivers whatever comes out' - where would it deliver it? *** To my mailbox. I want the script to see incoming messages just before they get deposited in my mailbox on the server. From what I can understand of how this works, some program actually receives the messages from sendmail (?) and deposits them in my /usr/home/$USER mailbox, which appears to be just a simple text file (I can't find any reference giving the format of that, either). *** sendmail is should not be running as root (probably 'mailnull'). *** It's running as root on my system. I thought it _had_ to run as root (?); doesn't it have to access mailboxes that have only 600 access or something? To Unsubscribe: send mail to [EMAIL PROTECTED] with unsubscribe freebsd-questions in the body of the message
RE: Getting Perl scripts to work as mail filters
-Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]]On Behalf Of Mxsmanic *** Well it looks like your script is being run - what should your script do? *** Heck, it's so simple I can list it here: #!/usr/bin/perl while ($inputline = STDIN) { $inputline =~ s/18 Dec 2002/29 Dec 2013/; print STDOUT $inputline; } The s/// stuff is just to give me some proof that the script saw the message. I'm not getting anything at all now, though. I assumed that writing to STDOUT would somehow get the message delivered, but maybe not. But if that's not the way to get it delivered, what is? The How do you expect sendmail to know which user to deliver to? By altering the aliases file, you have said to sendmail: Don't deliver to localuser 'mymail', Instead deliver to the script 'perlfilter' If you don't need your script to modify the data that get's sent, then you could just add another recipient in the aliases file: mymail: | perlfilter, myusername If you want to modify the message before sending it elsewhere, you could do something like: #!/usr/bin/perl $SENDMAIL=/usr/sbin/sendmail -bm; $MYADDRESS=me\@mydomain.com; open(SENDMAIL, | $SENDMAIL $MYADDRESS) or die Can't open $SENDMAIL; while ($inputline = STDIN) { $inputline =~ s/18 Dec 2002/29 Dec 2013/; print SENDMAIL $inputline; } close(SENDMAIL); sendmail manual is not at all clear on this. I looked in sendmail.cf for the name of some program to which perhaps I should pipe STDOUT in the aliases file, but couldn't find anything. When the aliases file does not specify piping of messages to some special program, who normally delivers them? *** sendmail doesn't do anything with the STDOUT from your script. I'm nut sure what you mean by 'delivers whatever comes out' - where would it deliver it? *** To my mailbox. I want the script to see incoming messages just before they get deposited in my mailbox on the server. From what I can understand of how this works, some program actually receives the messages from sendmail (?) and deposits them in my /usr/home/$USER mailbox, which appears to be just a simple text file (I can't find any reference giving the format of that, either). It's running as root on my system. I thought it _had_ to run as root (?); doesn't it have to access mailboxes that have only 600 access or something? Older versions ran as root - current recommended setup is not to run as root. But that's another issue. - Barry To Unsubscribe: send mail to [EMAIL PROTECTED] with unsubscribe freebsd-questions in the body of the message
Re: Getting Perl scripts to work as mail filters
Mxsmanic [EMAIL PROTECTED] writes: | What do I have to do to make a simple Perl script filter incoming mail for a | mailbox? I wrote a script that just reads standard input and writes it to | standard output, then put it in my home directory, then changes | /etc/mail/aliases to point to it, like this: | | mymail:|/usr/home/mymail/perlfilter I know that seems like the way it should work . . . but it's not. What that does is just pipe the message to your script---and at that point, sendmail is done. The output from your script went into the bit bucket. What you're going to have to do is open up a sendmail process. You can do something like this in your Perl script, though: open( MAIL, | /usr/sbin/sendmail ); print MAIL $headers; print MAIL $body; close MAIL; Your headers will have to have all the relevant information, and be separated from the body with a blank line. Good luck with that. To Unsubscribe: send mail to [EMAIL PROTECTED] with unsubscribe freebsd-questions in the body of the message
Re: Getting Perl scripts to work as mail filters
- Original Message - From: Mxsmanic [EMAIL PROTECTED] To: Barry Byrne [EMAIL PROTECTED]; FreeBSD Questions [EMAIL PROTECTED] Sent: Wednesday, December 18, 2002 7:19 PM Subject: Re: Getting Perl scripts to work as mail filters Heck, it's so simple I can list it here: --- #!/usr/bin/perl while ($inputline = STDIN) { $inputline =~ s/18 Dec 2002/29 Dec 2013/; print STDOUT $inputline; } --- A bit too simple, perhaps. :) STDOUT is irrelevant to sendmail here; because, as far as sendmail is concerned, your message has already been delivered, namely to the alias mymail. And your log says so: to=|/usr/home/mymail/mailman, ctladdr=mymail (1/0) Indicating that the controlling address (ctladdr) is mymail. If your recipient (mymail) decides to output something, good for him, but sendmail is long since done with the delivery. If you want to use filters, more or less like you describe, have a look at: Sendmail::Milter get deposited in my mailbox on the server. From what I can understand of how this works, some program actually receives the messages from sendmail (?) and deposits them in my /usr/home/$USER mailbox For that to happen, in a set-up where the alias is to deliver mail for variable users, you might, for instance, take a look at the plussed users facility in sendmail. But why re-invent the wheel? A program like procmail will more than likely do what you want. But even procmail, run from a .forward scheme, suffers from having difficulty extracting the intended recipient. Only if you define a Perl mailer can you solve that problem entirely, as you can parse sendmail variables like $f and $u to your delivery agent. - Mark To Unsubscribe: send mail to [EMAIL PROTECTED] with unsubscribe freebsd-questions in the body of the message
Re: Getting Perl scripts to work as mail filters
What about appending directly to the mailbox file under /var/mail/$USER with the script? It looks like the format of the file is very simple. Why wouldn't that work? I've tried writing to it, but I can't, at least not when the script runs from the aliases file (works fine when I run it myself, though). Procmail is exactly the sort of bloated tool that I'm trying to avoid. I don't know that calling sendmail again would be _that_ hard. After all, I'm mostly just copying the input to the output. I guess all I'd have to do would be to change the recipient address to avoid a loop (?). Right? Can't I just leave all the headers intact otherwise? - Original Message - From: Mark [EMAIL PROTECTED] To: Mxsmanic [EMAIL PROTECTED]; Barry Byrne [EMAIL PROTECTED]; FreeBSD Questions [EMAIL PROTECTED] Sent: Wednesday, December 18, 2002 23:50 Subject: Re: Getting Perl scripts to work as mail filters - Original Message - From: Mxsmanic [EMAIL PROTECTED] To: Barry Byrne [EMAIL PROTECTED]; FreeBSD Questions [EMAIL PROTECTED] Sent: Wednesday, December 18, 2002 7:19 PM Subject: Re: Getting Perl scripts to work as mail filters Heck, it's so simple I can list it here: --- #!/usr/bin/perl while ($inputline = STDIN) { $inputline =~ s/18 Dec 2002/29 Dec 2013/; print STDOUT $inputline; } --- A bit too simple, perhaps. :) STDOUT is irrelevant to sendmail here; because, as far as sendmail is concerned, your message has already been delivered, namely to the alias mymail. And your log says so: to=|/usr/home/mymail/mailman, ctladdr=mymail (1/0) Indicating that the controlling address (ctladdr) is mymail. If your recipient (mymail) decides to output something, good for him, but sendmail is long since done with the delivery. If you want to use filters, more or less like you describe, have a look at: Sendmail::Milter get deposited in my mailbox on the server. From what I can understand of how this works, some program actually receives the messages from sendmail (?) and deposits them in my /usr/home/$USER mailbox For that to happen, in a set-up where the alias is to deliver mail for variable users, you might, for instance, take a look at the plussed users facility in sendmail. But why re-invent the wheel? A program like procmail will more than likely do what you want. But even procmail, run from a .forward scheme, suffers from having difficulty extracting the intended recipient. Only if you define a Perl mailer can you solve that problem entirely, as you can parse sendmail variables like $f and $u to your delivery agent. - Mark To Unsubscribe: send mail to [EMAIL PROTECTED] with unsubscribe freebsd-questions in the body of the message To Unsubscribe: send mail to [EMAIL PROTECTED] with unsubscribe freebsd-questions in the body of the message
Re: Getting Perl scripts to work as mail filters
On Thu, 19 Dec 2002, Mxsmanic wrote: What about appending directly to the mailbox file under /var/mail/$USER with the script? It looks like the format of the file is very simple. Why wouldn't that work? I've tried writing to it, but I can't, at least not when the script runs from the aliases file (works fine when I run it myself, though). Procmail is exactly the sort of bloated tool that I'm trying to avoid. I don't know that calling sendmail again would be _that_ hard. After all, I'm mostly just copying the input to the output. I guess all I'd have to do would be to change the recipient address to avoid a loop (?). Right? Can't I just leave all the headers intact otherwise? Yes, using aliases would probably work to avoid a loop. You may even want to clean out the Cc: and To: lines of anyone and everyone you didn't want getting another copy (I'm not 100% sure this is the case, but it seems to me that if there's a long list of people Cc:'d, calling sendmail again without stripping them out will send them another email).. Someone else mentioned it, I think it's worth mentioning again. I'm not 100% sure what you want to do, but I'm 99% sure you'd be better off using Sendmail's Milter interface than trying to mess around with aliases and .forward files. Check out http://www.samag.com/documents/s=7178/sam0206l/. It's got an example of a small perl script Milter, and the sendmail source has some others (in C, last I checked). - Jeff Jirsa To Unsubscribe: send mail to [EMAIL PROTECTED] with unsubscribe freebsd-questions in the body of the message
Re: Getting Perl scripts to work as mail filters
- Original Message - From: Mxsmanic [EMAIL PROTECTED] To: Mark [EMAIL PROTECTED]; Barry Byrne [EMAIL PROTECTED]; FreeBSD Questions [EMAIL PROTECTED] Sent: Thursday, December 19, 2002 1:13 AM Subject: Re: Getting Perl scripts to work as mail filters What about appending directly to the mailbox file under /var/mail/$USER with the script? Possible, but not ideal. Be sure, in that case, you familiarize yourself with the assorted locking mechanism in question -- especially that of qpopper, for instance (if that is what you use to retrieve mail, of course), or mail may become lost. It looks like the format of the file is very simple. Yes, standard mbox format. Why wouldn't that work? It would, if done properly. :) I don't know that calling sendmail again would be _that_ hard. Certainly possible (as has been suggested already, be careful you do not wind up in a loop, though). Like so, for instance: $mailprog = '/usr/sbin/sendmail'; open (MAIL, | $mailprog -f$sender $recipient) || die $!; print MAIL $header$body; close (MAIL); The -f override is so the envelope sender will remain the original sender. Then you need to work a few things out with trusted users and all, to avoid authwarnings within the message. But a second invocation of sendmail from your Perl-script is sure possible. After all, I'm mostly just copying the input to the output. I guess all I'd have to do would be to change the recipient address to avoid a loop (?). Right? Can't I just leave all the headers intact otherwise? Headers are all part of the DATA stream, so they have no bearing on where sendmail delivers mail (unless you run sendmail with the -t option, which is generally ill-adviced anyway). The recipient given to sendmail as parameter (see above) is what will be used for delivery. No need to change the headers for that (as, like I said, they are part of the DATA stream). - Mark To Unsubscribe: send mail to [EMAIL PROTECTED] with unsubscribe freebsd-questions in the body of the message
Re: Getting Perl scripts to work as mail filters
Mxsmanic [EMAIL PROTECTED] writes: | What about appending directly to the mailbox file under /var/mail/$USER with | the script? I'd recommend against that. Using the sendmail interface is just as easy programmatically as appending it to /var/mail/whatever, plus you don't run into any messy issues with lockfiles and such. Just use sendmail; it's already there. | Procmail is exactly the sort of bloated tool that I'm trying to avoid. Well, for this, you don't need procmail, especially if you have a dedicated alias for this. | I don't know that calling sendmail again would be _that_ hard. After all, | I'm mostly just copying the input to the output. I guess all I'd have to do | would be to change the recipient address to avoid a loop (?). Right? Can't | I just leave all the headers intact otherwise? Absolutely; in fact, that's the best solution to the problem. To Unsubscribe: send mail to [EMAIL PROTECTED] with unsubscribe freebsd-questions in the body of the message