fetchmail/procmailrc filter by date - email

I have fetchmail grab my email from a pop account and send it to procmail. I have 'keep' set in my fetchmailrc file because i also use the email for outlook and i cant have fetchmail taking all my emails.
My problem is i want to download just todays emails or at lease since last time fetchmail ran.
right now i am trying to filter by date in the procmail file but it is not working.
I am using fedora14
contents of .procmailrc
SHELL=/bin/bash
DATEZ="date +'%a, %d %m %Y'"
:0
*^From.*\<(blah#blah\.com|blah2#blah2\.ca)\>
*^Content-Type:*
*^ name.*\.(xls|doc)
*$ ^Date:.*$DATEZ
{
:0fw
| uudeview -p $HOME/Inbound/Received -
:0
| $HOME/Inbound/Start.bash 2> /dev/null
}

Well it pays to read. In the fetchmail manual it explains in detail to use the -U flag. This then only downloads the emails from when you last polled the server.
so the command is:
fetchmail -k -U

Related

command to email file as an attachment

I'm using the following command to send an email with a specific file attached from the web server to me
mail -A /home/root/BACKUPS/backup.sql -s "$(date +%F) sql Backup" me#domain.com < /dev/null
Resource: https://tecadmin.net/ways-to-send-email-from-linux-command-line/
Problem is that although it is sending me an email, it's not attaching the item (backup.sql).
Does anyone see the problem with this code? Or is there something else that needs to be done to make the code work?

mail: Invalid header when sending mail attachment with Ubuntu

This exact question was posted by user 'anvd' on 29th July but then removed - I found it on Google Cached version...!
I'm guessing the fix was something obvious, but would be great if it wasn't removed.
I am trying this command to send an email with an attachment.
echo 'These are contents of my daily backup' | mail -s 'Daily backup' -a /tmp/filename.gz mymail#hotmail.com
The error: mail: Invalid header: /tmp/filename.gz
The email gets sent, but the attachment is not attached.
This used to work, and still dos on other systems, but not sure what has changed on one of my machines...
I just had this problem. It turns out that -a is the flag for appending headers, whereas -A is the flag for attaching files.

Perforce - getting stream name in a trigger script

I want to create a trigger for preventing check-out on files if they belong to a specific stream.
I am using the pre-user-edit trigger.
The trigger executes a Perl script.
When I execute in the Perl script a p4 command, then I get this error:
Perforce password (P4PASSWD) invalid or unset.
What I did is getting the stream name out of the client name (which is passed to the Perl script):
$ stream = `p4 client -o $ client | grep ^Stream: | awk '{print \$ 2}'`;
chomp $ stream;
This does not work.
Also, trying to assign $p4 with new P4, fails.
Anyone has a clue how to solve this?
At least, give me a way to extract the stream name out the client name?
Thanks,
You must be logged in to Perforce to run p4 client. In interactive shell you do this by p4 login which prompts you for a password. Once that's validated, Perforce keeps you logged in for a week or so (depends on your P4 server setting). During that week your command should succeed, but once your session expires it will start failing again.
If session expiry is a problem for you, you will need to get hold of a non-expiring ticket. That must be enabled by your server admin (read "To create tickets that do not expire..." att p4 login). See also P4TICKETS.
As an alternative,
$ stream = p4 -F "%Stream%" -ztag client -o;
This would just give you the stream name(no trimming is required)
As an other alternative, use p4 switch to show the name of the current stream.

Is procmail chrooted or limited in using linux commands?

im using procmail to forward emails to different folders in my Maildir.
I use these two lines to get the FROM and TO from the mail, which works pretty fine.
FROM=`formail -x"From:"`
TO=`formail -x"To:"`
These two commands return the whole line without the From: and To: prefix.
So i get something like:
Firstname Lastname <firstname.lastname#mail-domain.com>
Now i want to extract the email between < and >.
For this i pipe the variable FROM and TO grepping it like this.
FROM_PARSED=`echo $FROM | grep -o '[[:alnum:]+\.\_\-]*#[[:alnum:]+\.\_\-]*'`
TO_PARSED=`echo $TO | grep -o '[[:alnum:]+\.\_\-]*#[[:alnum:]+\.\_\-]*'`
But when i print FROM_PARSED into the procmail log by using LOG=FROM_PARSED, i get an empty string in FROM_PARSED and TO_PARSED.
But if i run these commands on my console, all works fine. I tried many other grepping methods, using grep, egrep, sed and even cut (cutting < and >). All working on console, but i use it in procmail it just returns nothing.
Is it possible that procmail is not allowed to use grep and sed commands? Something like a chroot?
I dont get any error logs in my procmail log. I just want to extract the valid email address from the FROM and TO line. Extracting with formail works, but parsing it with grep or sed fails, even if expression is correct.
Could somebody help? Maybe i need to setup procmail somehow.
Strange.
I added this to the users .procmailrc file
SHELL=/bin/bash
The users shell was set to /bin/false, which is correct because its a mail user, no ssh access at all.
You should properly quote "$FROM" and "$TO".
You will also need to prefix grep with LC_ALL=POSIX to ensure [:alnum:] will actually match the 26 well-known characters + 10 digits of the English alphabet.
You already solved this, but to answer your actual question, it is possible to run procmail in a chroot, but this is certainly not done by Procmail itself. Sendmail used to come with something called the Sendmail Restricted Shell (originally called rsh but renamed to remsh) which allowed system administrators to chroot the delivery process. But to summarize, this is a feature of the MTA, not of Procmail.

Cron send email with STDERR but NOT STDOUT?

I have some python scripts that run on a daily basis in cron. How can I have cron send me an email ONLY WHEN THERE IS STDERR OUTPUT from my script? I want to be able to mail multiple recipients, and set the subject line individually for each cron entry.
I tried this:
./prog > /dev/null | mail . . .
but it didn't work -- I still receive blank emails when there is no STDERR. Do I need to do this in the script itself?
Sorry if this seems basic, I have googled a lot but can't seem to find this answered succintly.
For cron you don't need to pipe through mail. The cron daemon will automatically mail any output of your command to you. Your crontab entry should look like:
# every minute
* * * * * ./prog >/dev/null
If there is no STDERR output, you won't get any mail.
You are asking incorrect question. When you are using mail(1) to send the email, it is no longer relevant that its in cron. What you actually need is to pipe stderr to stdin of mail. Normal pipe is from stdout to stdin, so simplest way to solve this is redirect:
{ /prog > /dev/null ; } 2>&1 | mail ...
Or in the less-clear way because of confusing order of redirectings:
/prog 2>&1 > /dev/null | mail ...
mail v1.6 has an option to not send messages with an empty body:
-E Do not send messages with an empty body.
This is useful for piping errors from cron(8) scripts.
This might be what you are looking for.
The -s file test will tell you if a file exists and has size greater than zero.
./prog >/dev/null 2>some/file ; if [ -s some/file ] ; then mail < some/file ; fi
There is a nice tool called cronic that does this. It is part of the moreutils package.
If your SCRIPT has commands that may produce STDERR that you want to be notified on, then you need to use a mail or mailx call within the script itself (if then else or ). The cron job STDOUT and STDERR redirects are ONLY for cron job EXECUTION STDOUT and STDERR. hkmaly had it right on the n