Bash script to collect all mails of a local linux user and forwars all of them to a mail address as a single mail.
Hello,
I need help making a script to read all the mails of a local user (/var/spool/mail/<user>) and forward them as a single mail to a mail address say a Gmail address. What I am planning to do is: 1. Need to copy all the mail to a file. 2. Forward it to an email id. |
Quote:
Otherwise, you can find bash scripting tutorials easily...follow them. |
Why not just add a .forward file and forward the email as it is received?
|
Quote:
|
There is a tool "formail" that does it.
See https://serverfault.com/questions/77...ail-in-mailbox |
Quote:
|
Will this work?
#!/bin/bash while :; do sleep 8h cp /var/spool/mail/<USER> > Cron.txt mutt -s "Cron report" -a <filename>.txt user@example.com done |
Quote:
|
Quote:
|
Quote:
You asked if a posted script would work. How should we know? We certainly can't test it for you. Did you run it? What happened when you did? I note these things: cp doesn't need a redirect. Syntax is: Code:
cp source target I don't know mutt syntax. I use mail in scripts. But, if you're cp to Cron.txt, then why try to mail <filename>.txt (if that's what that's supposed to do) I don't know if the while clause will work as written...but to you want a job running all the time waiting to execute every 8 hours? That looks like a job for cron. Quote:
The "right" answer is, probably, to modify whatever is sending the emails to your inbox to add to a file instead, and then use cron to mail that file daily. Why are you the recipient of the cron job emails? How many are there? |
Quote:
Your question was "Will this work?" The answer was "run it and find out". Simple. |
Quote:
tail -10 /var/spool/mail/$USER > filename.txt while :; do sleep 8h cat filename.txt | mail -s "Cron report" user@example.com done The script works. But I need to know if there's a better way to do it. |
Quote:
Do you really want this to happen three times a day? How do you know that a tail of the last 10 lines of the mbox is going to be exactly what you want? ...and I'll repeat: How does this solve your problem of your mailbox "filling up"? Aren't you still going to see the emails anyway? PS Note the use of code tags...please use them when posting code. |
All times are GMT -5. The time now is 05:21 PM. |