LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   schedule email (https://www.linuxquestions.org/questions/programming-9/schedule-email-407686/)

rharris72 01-25-2006 01:53 AM

schedule email
 
is there a way to write an email via bash scripting then for it to schedule it to send say once a month

the bash script say asks some qustions that are then put into an email then the script sends the email on the date that it asks via the bash script

thanks

kshkid 01-25-2006 04:52 AM

use mailx or mail,
in your bash script

schedule it to trigger once in every month in your cron job.

could you please post your requirement with some sample input and output?

rharris72 01-25-2006 10:45 AM

what i want it to do

get user input from a user via bash say

echo "enter subject"
read sub

echo "enter message"
read mess

echo "mail to"
read letter

echo "please enter day of month to send mail"
read day

sed "s/mail/$mail/g" mail.sh > mail1.sh
rm -rf mail.sh
sed "s/mess/$mess/g" mail1.sh > mail2.sh
rm -rf mail1.sh
sed "s/sub/$sub/g" mail2 > mail.sh
rm -rf mail2.sh

echo "ok mail will be sent on $day of every month"

then i want it to edit another bash script with the info which i know how to do then place that bash script in cron to run on the date specified by the user in the varible $day


the other bash looks like this

#! /bin/bash

./sendmail -f admin@blah.com -t letter -m mess -u sub

thanks

rharris72 01-25-2006 09:32 PM

can anyone help with this please thanks ?

twantrd 01-25-2006 11:00 PM

This is what I would do:

Code:

#!/bin/bash

echo "enter subject"
read sub

echo "enter message"
read mess

echo "mail to"
read letter

echo "please enter day of month to send mail"
read day

echo "0 23 $day * * cat \"$mess\" | mail -s \"$sub\" $letter" > /tmp/cron.mail
crontab /tmp/cron.mail


And here are the results:

twantrd@twantrd:~$ /tmp/test
enter subject
Hi there
enter message
How are you?
mail to
me@mydomain.com
please enter day of month to send mail
15
twantrd@twantrd:~$ crontab -l
0 23 15 * * cat "How are you?" | mail -s "Hi there" me@mydomain.com

So, this is just one script that does all the work for you and it's pretty simple once you look at it. This will set the cronjob for that user to mail out that message at 11:00pm on the 15th of every month.

-twantrd

kshkid 01-26-2006 02:27 AM

Quote:

echo "0 23 $day * * cat \"$mess\" | mail -s \"$sub\" $letter" > /tmp/cron.mail
crontab /tmp/cron.mail
appending to the existing entries in cron file would be the best option.

rharris72 01-26-2006 05:33 AM

how would i enter that cron command if i wanted it to run a bash script

thanks for all your help

i think linux people are far more helpful than dudes that use windows

kshkid 01-26-2006 07:15 AM

after the saving the functionality in the bash script

edit your crontab file

crontab -e

and add the entry,
Code:

0 23 * * * <bashscriptname>

rharris72 01-26-2006 10:18 AM

script ./mail1.sh

#!/bin/bash
chmod 777 /home/mailshot -R

echo "enter name"
read name

echo "enter subject"
read sub

echo "enter message"
read mess

echo "mail to"
read letter

echo "please enter day of month to send mail"
read day

cd /home/mailshot

sed "s/letter/$letter/g" mail.sh > mail1.sh
sed "s/mess/$mess/g" mail1.sh > mail2.sh
rm -rf mail1.sh
sed "s/sub/$sub/g" mail2.sh > $clan.sh
rm -rf mail2.sh

echo "ok mail will be sent on $day of every month"

new="10 16 $day * * /home/mailshot/$name.sh"

crontab /home/mailshot/mail.cron

mail=`cat /home/mailshot/mail.cron`

# make Filedescriptor(FD) 6 a copy of stdout (FD 1)
exec 6>&1
# open file "mail.cron" for writing
exec 1>mail.cron
# produce some content
echo "$mail"
echo "$new"
# close file "mail.cron"
exec 1>&-
# make stdout a copy of FD 6 (reset stdout)
exec 1>&6
# close FD6

exec 6>&-
exit 0


script mail.sh contains this

#! /bin/bash

/home/mailshot/sendmail -f admin@blah.com -t "letter" -m "mess" -u "sub"

then i have the sendmailbin file in same dir and also a file mail.cron but for some reason its not showing cron jobs in webmin for admin user once i exec script

also i dont want it to send a mail to admin evertime it exec the cron job

can anyone help

cheers

i know scripts are a bit messy but learning

rharris72 01-26-2006 03:59 PM

please help if you can i have added the scripts im using as to show you what im doing or trying to do

thanks

if i use this code then it only processes the last inputed crontab ??????

#!/bin/bash

echo "enter name"
read name

echo "enter subject"
read sub

echo "enter message"
read mess

echo "mail to"
read letter

echo "please enter day of month to send mail"
read day


echo "50 21 $day * * /home/mailshot/sendmail -f sales@blah.co.uk -t "$letter" -m "$mess" -u "$sub"" > admin.cron
echo "50 21 $day * * /home/mailshot/sendmail -f sales@blah.co.uk -t "jerry@blah.co.uk" -m "$mess" -u "$name $sub"" > admin1.cron
echo "50 21 $day * * /home/mailshot/sendmail -f sales@blah.co.uk -t "phil@blah.co.uk" -m "$mess" -u "$name $sub"" > admin2.cron

mv admin.cron /home/mailshot/
mv admin1.cron /home/mailshot/
mv admin2.cron /home/mailshot/

crontab /home/mailshot/admin.cron
crontab /home/mailshot/admin1.cron
crontab /home/mailshot/admin2.cron

twantrd 01-27-2006 02:22 AM

Quote:

if i use this code then it only processes the last inputed crontab ??????
That is correct with what you have as cron will read from the last file. Put 'admin.cron', 'admin1.cron', and 'admin2.cron' into a single file and then crontab that file.

-twantrd

rharris72 01-27-2006 05:26 AM

ok if i put contents of 3 files into one file would i be able to specify diff times and days for the mails to be sent ?

also how would i go about echoing content to one file i know your prob thinking this guy is a nood

well the answer to that is yes yesy yes but i love learning linux tried learning windows and gave up

thanks in advance

rharris72 01-27-2006 07:16 AM

is there a way of putting the 3 lines of cron into 1 line and use bash newline command which i have read about but dont understand how to use it ?

bigearsbilly 01-27-2006 08:33 AM

you shouldn't really write directly to crontab files.
not really what they're designed for.

when you use 'crontab -e' it checks it and reads it.
not sure if cron would register the crontab update
if you write it like so, unless you did a crontab -e afterwards.

I would strongly discourage it.
just what I think ;)

rharris72 01-27-2006 02:33 PM

proberbly long winded but i did it this way and it worked so thanks to all that helped but if there is an easier way can someone elaborate thanks....


#!/bin/bash

echo "enter name"
read name

echo "enter subject"
read sub

echo "enter message"
read mess

echo "mail to"
read letter

echo "please enter day of month to send mail"
read day


echo "0 20 $day * * /home/mailshot/sendmail -f sales@blah.co.uk -t "$letter" -m "$mess" -u "$sub"" > /home/mailshot/1.txt
echo "0 20 $day * * /home/mailshot/sendmail -f sales@blah.co.uk -t "jerry@blah.co.uk" -m "$mess" -u "$name $sub"" > /home/mailshot/2.txt
echo "0 20 $day * * /home/mailshot/sendmail -f sales@blah.co.uk -t "phil@blah.co.uk" -m "$mess" -u "$name $sub"" > /home/mailshot/3.txt

a=`cat /home/mailshot/1.txt`
b=`cat /home/mailshot/2.txt`
c=`cat /home/mailshot/3.txt`
z=`cat /home/mailshot/mail.cron`

# make Filedescriptor(FD) 6 a copy of stdout (FD 1)
exec 6>&1
# open file "mail.cron" for writing
exec 1>/home/mailshot/mail.cron

# produce some content
echo "$z"
echo "$a"
echo "$b"
echo "$c"

# close file "mail.cron"
exec 1>&-
# make stdout a copy of FD 6 (reset stdout)
exec 1>&6
# close FD6
exec 6>&-

crontab /home/mailshot/mail.cron
rm -rf /home/mailshot/1.txt
rm -rf /home/mailshot/2.txt
rm -rf /home/mailshot/3.txt
exit 0


All times are GMT -5. The time now is 11:18 PM.