LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 08-19-2004, 11:23 PM   #1
luwigie
Member
 
Registered: Aug 2004
Location: Michigan, USA
Distribution: Fedora Core 2
Posts: 42

Rep: Reputation: 15
Mail (logs, etc), can it be sent via SMTP?


I rarely check the command line 'mail' in linux, but I know that I probably should especially since there a now new vulnerabilities to SSH and my machine is connected to a static IP (so I can use it remotely).

My question is; is there a way that I can make the mail that ends up in that command line program get sent to one of my e-mail accounts so that I can read it in a graphical environment (and I don't forget it)?

Thanks!

 
Old 08-20-2004, 12:43 AM   #2
legolin
Member
 
Registered: Dec 2003
Location: munich
Distribution: Fedora Core 4
Posts: 141

Rep: Reputation: 15
thats a very good question, i was looking for the same thing. i read a littlebit in the man of mail, but i didn't found anything as well....

thanks,

leg
 
Old 08-21-2004, 05:30 AM   #3
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,039

Rep: Reputation: 97
Hello,

I am making a few assumptions here so please forgive me if I'm on the wrong track.

Assumptions:[list=1][*]You are wanting emails forwarded from a remote machine[*]local mails are stored in /var/mail under the account name[*]You can send mailx messages externally[/list=1]
Before going too far, test that you can send external emails using mailx
  • Log in to the remote machine
  • Type the following
  • echo "This is a test"|mailx -s "Remote Test" my_account@my_isp.com
I have written a little script (58 lines) called forward.
Code:
#!/usr/bin/bash
###
### Script: forward
###
### Purpose: to forward local emails to a remote address
###

MAIL_DIR=/var/mail
TMP_DIR=/tmp
mail_to=my_account@my_isp.com

cd $MAIL_DIR
ls |while read ACCOUNT
do
   if [ -s ${MAIL_DIR}/${ACCOUNT} ]
   then
      mv ${MAIL_DIR}/${ACCOUNT} ${TMP_DIR}/mail.${ACCOUNT}
      touch ${MAIL_DIR}/${ACCOUNT}

      l_msg=0
      Subject="ERROR"

      cat ${TMP_DIR}/mail.${ACCOUNT}|while read EACHLINE
      do
         BEGIN=`echo ${EACHLINE}|grep '^From '|wc -l`
         if [ $BEGIN -eq 1 ]
         then
            if [ -s ${TMP_DIR}/mail.${ACCOUNT}.${l_msg} ]
            then
               cat ${TMP_DIR}/mail.${ACCOUNT}.${l_msg}|mailx -s"$Subject" ${mail_to}
            fi

            rm ${TMP_DIR}/mail.${ACCOUNT}.${l_msg}
            l_start=1
            l_msg=`expr $l_msg + 1`
            cat /dev/null > ${TMP_DIR}/mail.${ACCOUNT}.${l_msg}
         fi

         BEGIN2=`echo ${EACHLINE}|grep '^Subject:'|wc -l`
         if [ $BEGIN2 -eq 1 ]
         then
            Subject=`echo ${EACHLINE}|cut -d':' -f2`
         fi

         BEGIN3=`echo ${EACHLINE}|grep '^From:'|wc -l`
         if [ $BEGIN3 -eq 1 ]
         then
            l_start=2
         else
            if [ $l_start -eq 2 ]
            then
               echo $EACHLINE >> ${TMP_DIR}/mail.${ACCOUNT}.${l_msg}
            fi
         fi
      done
      rm ${TMP_DIR}/mail.${ACCOUNT}
   fi
done
If you create this file in your home dorectory, and create a cron job to run it every hour or so
crontab -e

#Line for remote mail forwarder
00 * * * * ~/forward > /dev/null 2>&1

Don't forget to make the script executable:

chmod +x ~/forward
 
Old 08-21-2004, 10:28 AM   #4
luwigie
Member
 
Registered: Aug 2004
Location: Michigan, USA
Distribution: Fedora Core 2
Posts: 42

Original Poster
Rep: Reputation: 15
Wow. Thank you for writing that script.

I will be sure to try that as soon as I get home. For some reason my remote SSH access isn't working... I'll post back if it works!
 
Old 08-23-2004, 09:19 PM   #5
luwigie
Member
 
Registered: Aug 2004
Location: Michigan, USA
Distribution: Fedora Core 2
Posts: 42

Original Poster
Rep: Reputation: 15
For the line: MAIL DIR =/var/mail Should I add the user to the end such as /var/mail/root?

Are there any lines other than mail_to=my_account@my_isp.com that I have to edit?

How exactly do I change how often it runs as a cron job?

Last edited by luwigie; 08-23-2004 at 09:22 PM.
 
Old 08-23-2004, 10:14 PM   #6
luwigie
Member
 
Registered: Aug 2004
Location: Michigan, USA
Distribution: Fedora Core 2
Posts: 42

Original Poster
Rep: Reputation: 15
I got everything to work by running the script manually, but I still don't know how to change the frequency of the cron job. Can some one please enlighten me?
 
Old 08-24-2004, 11:28 AM   #7
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,039

Rep: Reputation: 97
As you probably found when you ran the script manually, it runs for each file in the /var/mail directory.

A cron entry consists of:[list=1][*]Minute of the Hour (0-59)[*]Hour of the Day (0-23)[*]Day of the Month (1-31)[*]Month of the Year (1-12)[*]Day of the Week (0-7) Both 0 and 7 are Sunday[*]Command to be run (and any parameters)[/list=1]

For each parameter relating to time, you can use:
Therefore to run a command at 12:00 on the 1st of each month
  • Ranges EG: 3-8
  • A list EG: 1,2,5,6
  • ALL EG: *

Therefore to run a command at 12:00 on the first of each month:
00 12 1 * * command

to run a command every hour, but only on Friday:
00 * * * 5 command

For further information, try:

man 5 crontab
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Cron mail sending using outside SMTP mail server Utah Linux - Software 6 08-24-2005 07:44 PM
how to read mail logs? djfranknitti Linux - Newbie 2 09-21-2004 08:41 AM
Mail logs Woutermelon Linux - Networking 2 11-21-2003 02:02 PM
SMTP and KMail and SMTP and Netscape Mail jazz... Linux - Newbie 5 10-11-2003 03:49 AM
mail logs when i havent sent mail poseidoniv Linux - Newbie 1 04-19-2003 02:39 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 11:40 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration