Bash script to collect all mails of a local linux user and forwars all of them to a mail address as a single mail.
Linux - ServerThis forum is for the discussion of Linux Software used in a server related context.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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?
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.
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.
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
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?
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.
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.
!/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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.