LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 04-06-2018, 02:35 AM   #1
Pheonix777
LQ Newbie
 
Registered: Nov 2017
Posts: 7

Rep: Reputation: Disabled
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.

Last edited by Pheonix777; 04-06-2018 at 02:50 AM.
 
Old 04-06-2018, 08:07 AM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,634

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by Pheonix777 View Post
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.
 
Old 04-06-2018, 09:20 AM   #3
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: Fedora
Posts: 4,140

Rep: Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263
Why not just add a .forward file and forward the email as it is received?
 
Old 04-06-2018, 01:27 PM   #4
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,727

Rep: Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211
Quote:
Originally Posted by Pheonix777 View Post
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?

Last edited by scasey; 04-06-2018 at 01:32 PM.
 
Old 04-07-2018, 02:30 AM   #5
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,790

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
There is a tool "formail" that does it.
See https://serverfault.com/questions/77...ail-in-mailbox
 
Old 04-08-2018, 08:19 PM   #6
Pheonix777
LQ Newbie
 
Registered: Nov 2017
Posts: 7

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by smallpond View Post
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.
 
Old 04-08-2018, 11:45 PM   #7
Pheonix777
LQ Newbie
 
Registered: Nov 2017
Posts: 7

Original Poster
Rep: Reputation: Disabled
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
 
Old 04-09-2018, 07:03 AM   #8
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,634

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by Pheonix777 View Post
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.
 
Old 04-09-2018, 09:25 PM   #9
Pheonix777
LQ Newbie
 
Registered: Nov 2017
Posts: 7

Original Poster
Rep: Reputation: Disabled
Angry

Quote:
Originally Posted by TB0ne View Post
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.
 
Old 04-10-2018, 12:41 AM   #10
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,727

Rep: Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211
Quote:
Originally Posted by Pheonix777 View Post
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 View Post
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?

Last edited by scasey; 04-10-2018 at 12:49 AM.
 
2 members found this post helpful.
Old 04-10-2018, 07:02 AM   #11
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,634

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by Pheonix777 View Post
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.
 
Old 04-10-2018, 08:58 PM   #12
Pheonix777
LQ Newbie
 
Registered: Nov 2017
Posts: 7

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by TB0ne View Post
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.
 
Old 04-11-2018, 03:31 AM   #13
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,727

Rep: Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211
Quote:
Originally Posted by Pheonix777 View Post
#
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.

Last edited by scasey; 04-11-2018 at 03:32 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Rejecting non local mail for specific e-mail address vrsic Linux - Server 4 10-24-2006 02:46 AM
Redirecting non-user mail to a local address? Trent Hatred Red Hat 2 11-19-2003 10:59 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 04:37 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration