Home grown mail scheduler with bash script and cron

2013-10-04 237 words 2 mins read

If you are using Linux (Fedora/Ubuntu or anything else) then you do get a lot of tools and one of them is cron. Very very useful. Just write some script that can do the task for you, put it in cron and forget it. So, here is a home grown way to schedule mails.

First, copy this script below:

#!/bin/bash -
#===============================================================================
#
#          FILE: sched_mail.sh
#
#         USAGE: ./sched_mail.sh
#
#   DESCRIPTION:
#
#       OPTIONS: ---
#  REQUIREMENTS: ---
#          BUGS: ---
#         NOTES: ---
#        AUTHOR: Amit Agarwal (aka), 
#      REVISION:  ---
#===============================================================================

mailfile="~/mail"
if [[ $(wc -l $mailfile|awk '{print $1}' ) -ge 5 ]]
then
    to=$(grep ^To: $mailfile|sed 's/To: //')
    echo "Good to send mail... to = $to"
    sendmail -t <$mailfile
    echo "once mail is send, delete the contents of file"
    echo "sed -i '4,$ d' $mailfile"
fi

Now, create a file called mail in your home directory, with the following contents:

To: Email
From: Email
Subject: Test mail from script.

Put the first script in cron to run every minute. And whenever you want to schedule a mail, create your mail in plaintext and then schedule that with at command to be appended to the ~/mail file. As soon as that file is appended, it will be sent out in 1 min 🙂

 

 

Enhanced by Zemanta

author

Authored By Amit Agarwal

Amit Agarwal, Linux and Photography are my hobbies.Creative Commons Attribution 4.0 International License.

We notice you're using an adblocker. If you like our webite please keep us running by whitelisting this site in your ad blocker. We’re serving quality, related ads only. Thank you!

I've whitelisted your website.

Not now
This website uses cookies to ensure you get the best experience on our website. Learn more Got it