LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Server (https://www.linuxquestions.org/questions/linux-server-73/)
-   -   Bash script to collect all mails of a local linux user and forwars all of them to a mail address as a single mail. (https://www.linuxquestions.org/questions/linux-server-73/bash-script-to-collect-all-mails-of-a-local-linux-user-and-forwars-all-of-them-to-a-mail-address-as-a-single-mail-4175627111/)

Pheonix777 04-06-2018 02:35 AM

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.

TB0ne 04-06-2018 08:07 AM

Quote:

Originally Posted by Pheonix777 (Post 5839911)
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.

Ok...what help do you need??? Read the "Question Guidelines" link in my posting signature. We're happy to help, but we are NOT going to write your scripts for you. If you'd like assistance, post what you have written/done/tried so far, and tell us what errors you're getting, and provide details about your system.

Otherwise, you can find bash scripting tutorials easily...follow them.

smallpond 04-06-2018 09:20 AM

Why not just add a .forward file and forward the email as it is received?

scasey 04-06-2018 01:27 PM

Quote:

Originally Posted by Pheonix777 (Post 5839911)
What I am planning to do is:
1. Need to copy all the mail to a file.
2. Forward it to an email id.

I don't think you need to copy anything.../var/spool/mail/user is already a file containing all the mail for that user (mbox mail format), so just forward that file to them. What have you tried? What do you need help with? What are you trying to accomplish? ...that is, why?

MadeInGermany 04-07-2018 02:30 AM

There is a tool "formail" that does it.
See https://serverfault.com/questions/77...ail-in-mailbox

Pheonix777 04-08-2018 08:19 PM

Quote:

Originally Posted by smallpond (Post 5840011)
Why not just add a .forward file and forward the email as it is received?

I've few cron jobs running in background. I want the report of those jobs to be sent to my personal mail as a single mail at the end of the day. Now all the cron job reports are coming to mail ID one by one and fills up my inbox.

Pheonix777 04-08-2018 11:45 PM

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

TB0ne 04-09-2018 07:03 AM

Quote:

Originally Posted by Pheonix777 (Post 5840937)
Will this work?
Code:

#!/bin/bash
while :; do sleep 8h
cp /var/spool/mail/<USER> > Cron.txt
mutt -s "Cron report" -a <filename>.txt user@example.com
done


You tell us; you could have run it and found out, quicker than it took you to post it. And please post your code in CODE tags.

Pheonix777 04-09-2018 09:25 PM

Quote:

Originally Posted by TB0ne (Post 5841006)
You tell us; you could have run it and found out, quicker than it took you to post it. And please post your code in CODE tags.

Dude, we ask questions here. That's what this site is for. Everyone may not be smart as you. If you don't know what the solution is, please don't say anything at all.

scasey 04-10-2018 12:41 AM

Quote:

Originally Posted by Pheonix777 (Post 5841258)
Dude, we ask questions here. That's what this site is for. Everyone may not be smart as you. If you don't know what the solution is, please don't say anything at all.

I don't agree.
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
. See man cp
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:

Originally Posted by Pheonix777 (Post 5840895)
I've few cron jobs running in background. I want the report of those jobs to be sent to my personal mail as a single mail at the end of the day. Now all the cron job reports are coming to mail ID one by one and fills up my inbox.

I'd also point out that copying and remailing the mail file is not going to stop the impact on your inbox, since, presumably, the mail file you're moving is the one that's getting "filled up"

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?

TB0ne 04-10-2018 07:02 AM

Quote:

Originally Posted by Pheonix777 (Post 5841258)
Dude, we ask questions here. That's what this site is for. Everyone may not be smart as you. If you don't know what the solution is, please don't say anything at all.

I know exactly the solution, and can write a script in a few minutes to do exactly what you're after. But you're asking us to download the script, put it in a file on our systems, run it, then TELL YOU if it works or not, and from there, probably debug it for you. Things don't work like that. If you had the script already, you could have just executed it, and would have KNOWN if it worked or not. Easy to understand.

Your question was "Will this work?" The answer was "run it and find out". Simple.

Pheonix777 04-10-2018 08:58 PM

Quote:

Originally Posted by TB0ne (Post 5841357)
I know exactly the solution, and can write a script in a few minutes to do exactly what you're after. But you're asking us to download the script, put it in a file on our systems, run it, then TELL YOU if it works or not, and from there, probably debug it for you. Things don't work like that. If you had the script already, you could have just executed it, and would have KNOWN if it worked or not. Easy to understand.

Your question was "Will this work?" The answer was "run it and find out". Simple.

#!/bin/bash
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.

scasey 04-11-2018 03:31 AM

Quote:

Originally Posted by Pheonix777 (Post 5841688)
#
Code:

!/bin/bash
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.

Yes. Use cron to run the script instead of having it sleeping for 8 hours.
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.