LinuxQuestions.org
Review your favorite Linux distribution.
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 05-07-2011, 08:24 AM   #1
ramecare
Member
 
Registered: Feb 2011
Posts: 179

Rep: Reputation: 0
How to send mail through script


Hi all,

I have written a shell script to take backup and by doubt is after tat i need a script tat it should send mail automatically to eg:user@domain.com tat 'the backup job has done successfull',after tat i need anyone help to write a shell program tat mail should send automatically after the backup job completed for the below program.

#!/bin/bash
mount -t cifs -o user="$1",password="$2" "$3"/"$1" "$4"
echo "Mounted Successfully">>/var/log/kannan
cd "$4""$5"
date=`/bin/date "+%Y.%m.%d.%H.%M.%S"`
mysqldump -u"$6" -p"$7" "$8" > $date.sql
echo "Backup taken">>/var/log/kannan
tar -zcvf $date.sql.tgz $date.sql
echo "mysqldump to tar">>/var/log/kannan
rm -rf $date.sql
echo "Removed old one">>/var/log/kannan

Thanks,
 
Old 05-07-2011, 08:31 AM   #2
EricTRA
LQ Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297
Hello,

You can mail from the command with:
Code:
mail -s "subject" user@domain.com
if your system is set up to forward that mail to a mailserver, i.e. if you have for example Postfix configured to relay messages to a mailserver/domain.

But as mentioned in your other thread you're forgetting an important issue. You'll receive a message, be it on console, or by mail, on ANY occasion, even if the command fails. You should incorporate a conditional message depending on the exit code of your commands for example. Have a look at this at TLDP: 3.2.5. Special parameters.

To learn more about Bash scripting I'd advice you to read the following:
Bash Guide for Beginners
Advanced Bash Scripting Guide

Please put code you post between CODE tags for readability.

Kind regards,

Eric
 
Old 05-09-2011, 01:35 AM   #3
ramecare
Member
 
Registered: Feb 2011
Posts: 179

Original Poster
Rep: Reputation: 0
If i configure imap in my server i can able to send mail from command


Thanks,
 
Old 05-09-2011, 01:37 AM   #4
EricTRA
LQ Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297
Hello,

Does this mean you've got your problem solved? If so, then please mark this thread as solved. Have fun with Linux.

Kind regards,

Eric
 
Old 05-09-2011, 02:24 AM   #5
kaushalpatel1982
Member
 
Registered: Aug 2007
Location: INDIA
Distribution: CentOS, RHEL, Fedora, Debian, Ubuntu, LinuxMint, Kali Linux, Raspbian
Posts: 166

Rep: Reputation: 10
There is utility called sendEmail . you can download it from here. I am using it and it's good and easy tool. you can use it in your script without any extra efforts.
 
Old 05-09-2011, 05:03 AM   #6
ramecare
Member
 
Registered: Feb 2011
Posts: 179

Original Poster
Rep: Reputation: 0
No my problem was not solved i have configured sendmail but not able to send mail,can u help me.


Thanks,
 
Old 05-09-2011, 05:09 AM   #7
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by ramecare View Post
No my problem was not solved i have configured sendmail but not able to send mail,can u help me.


Thanks,
What command have you tried? And what error message do you get (if any)? Please use CODE tags to embed the code and make it more readable.
 
Old 05-09-2011, 05:38 AM   #8
ramecare
Member
 
Registered: Feb 2011
Posts: 179

Original Poster
Rep: Reputation: 0
Hi,

I used the below command.

mail -s "hi" ramkannan1988@gmail.com

When i try to send mail i got this error from error log:

May 9 16:04:09 itsupport sendmail[30433]: p49AY9qi030433: to=ramkannan_88@yahoo.com,ramkannan1988@gmail.com, ctladdr=root (0/0), delay=00:00:00, xdelay=00:00:00, mailer=relay, pri=60108, relay=[127.0.0.1] [127.0.0.1], dsn=4.0.0, stat=Deferred: Connection refused by [127.0.0.1]
I was not able to identify wat would be the problem,can u give me some suggesion.

vi /etc/mail/sendmail.mc
DAEMON_OPTIONS(`Port=smtp,Addr=58.68.21.67, Name=MTA')dnl
:wq!
make -C /etc/mail


Thanks,

Last edited by ramecare; 05-09-2011 at 05:42 AM.
 
Old 05-09-2011, 05:47 AM   #9
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
It looks like your sendmail daemon is not running, so it's not listening on port 25. Please, show us the output of the following commands to confirm my suspicion:
Code:
# ps -ef sendmail
# netstat -an | grep -w LISTEN
 
Old 05-09-2011, 05:54 AM   #10
ramecare
Member
 
Registered: Feb 2011
Posts: 179

Original Poster
Rep: Reputation: 0
[root@itsupport ~]# netstat -an | grep -w LISTEN
tcp 0 0 127.0.0.1:2208 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:2401 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:35175 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:139 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:10000 0.0.0.0:* LISTEN
tcp 0 0 192.168.122.1:53 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:21 0.0.0.0:* LISTEN
tcp 0 0 10.200.1.66:53 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:53 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:953 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:445 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:733 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:2207 0.0.0.0:* LISTEN
tcp 0 0 :::80 :::* LISTEN
tcp 0 0 :::22 :::* LISTEN
tcp 0 0 ::1:953 :::* LISTEN
[root@itsupport ~]# ps -ef sendmail
ERROR: Unsupported option (BSD syntax)
********* simple selection ********* ********* selection by list *********
-A all processes -C by command name
-N negate selection -G by real group ID (supports names)
-a all w/ tty except session leaders -U by real user ID (supports names)
-d all except session leaders -g by session OR by effective group name
-e all processes -p by process ID
T all processes on this terminal -s processes in the sessions given
a all w/ tty, including other users -t by tty
g OBSOLETE -- DO NOT USE -u by effective user ID (supports names)
r only running processes U processes for specified users
x processes w/o controlling ttys t by tty
*********** output format ********** *********** long options ***********
-o,o user-defined -f full --Group --User --pid --cols --ppid
-j,j job control s signal --group --user --sid --rows --info
-O,O preloaded -o v virtual memory --cumulative --format --deselect
-l,l long u user-oriented --sort --tty --forest --version
-F extra full X registers --heading --no-heading --context
********* misc options *********
-V,V show version L list format codes f ASCII art forest
-m,m,-L,-T,H threads S children in sum -y change -l format
-M,Z security data c true command name -c scheduling class
-w,w wide output n numeric WCHAN,UID -H process hierarchy

Thanks,
 
Old 05-09-2011, 06:04 AM   #11
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
There is no service listening on port 25 (the default smtp port). Be sure to have a line like this in your sendmail.mc:
Code:
DAEMON_OPTIONS(`Port=smtp,Addr=127.0.0.1, Name=MTA')dnl
then try to restart the mail daemon.
 
Old 05-09-2011, 06:11 AM   #12
ramecare
Member
 
Registered: Feb 2011
Posts: 179

Original Poster
Rep: Reputation: 0
Again i have to make this DAEMON_OPTIONS(`Port=smtp,Addr=58.68.21.67, Name=MTA')dnl to DAEMON_OPTIONS(`Port=smtp,Addr=127.0.0.1, Name=MTA')dnl

Thanks,
 
Old 05-09-2011, 06:29 AM   #13
ramecare
Member
 
Registered: Feb 2011
Posts: 179

Original Poster
Rep: Reputation: 0
Now ia have make this to DAEMON_OPTIONS(`Port=smtp,Addr=127.0.0.1, Name=MTA')dnl
I send mail to
[root@itsupport ~]# mail ramkannan_88@yahoo.com
Subject: testmail by Ram kannan
ramkannan
.
Cc: harish@ecaretechlabs.com
[root@itsupport ~]#

i did not recieve any mail & in the mail log i got the error

May 9 16:53:49 itsupport sendmail[32518]: p49BNnnK032518: from=root, size=84, class=0, nrcpts=2, msgid=<201105091123.p49BNnnK032518@itsupport.ecaretechlabs.com>, relay=root@localhost
May 9 16:53:49 itsupport sendmail[32519]: p49BNnx5032519: from=<root@itsupport.ecaretechlabs.com>, size=375, class=0, nrcpts=2, msgid=<201105091123.p49BNnnK032518@itsupport.ecaretechlabs.com>, proto=ESMTP, daemon=MTA, relay=localhost.localdomain [127.0.0.1]
May 9 16:53:49 itsupport sendmail[32518]: p49BNnnK032518: to=ramkannan_88@yahoo.com,harish@ecaretechlabs.com, ctladdr=root (0/0), delay=00:00:00, xdelay=00:00:00, mailer=relay, pri=60084, relay=[127.0.0.1] [127.0.0.1], dsn=2.0.0, stat=Sent (p49BNnx5032519 Message accepted for delivery)
May 9 16:53:52 itsupport sendmail[32521]: STARTTLS=client, relay=ecaretechlabs.com., version=TLSv1/SSLv3, verify=FAIL, cipher=AES256-SHA, bits=256/256
May 9 16:53:53 itsupport sendmail[32521]: p49BNnx5032519: to=<harish@ecaretechlabs.com>, ctladdr=<root@itsupport.ecaretechlabs.com> (0/0), delay=00:00:04, xdelay=00:00:04, mailer=esmtp, pri=150375, relay=ecaretechlabs.com. [204.93.168.132], dsn=5.1.1, stat=User unknown

I was not able to find the solution,can anyone helpme on this
Thanks,

Last edited by ramecare; 05-09-2011 at 06:43 AM.
 
Old 05-10-2011, 11:44 AM   #14
hua
Member
 
Registered: Oct 2006
Location: Slovak Republic
Distribution: Slackware 14.2, current
Posts: 461

Rep: Reputation: 78
Lets take this from the begining. It seems to me that you want to make too many steps at once.
1. Your first step should be the sendmail configuration.
2. After this you can think about to write a script which sends mail

Maybe I missed this but it is useful to know these basic things:

1. What distribution of Linux you use.
2. What are the steps which you do when you configure your sendmail.

(1. You should edit your sendmail.mc file, 2. use m4 to generate your sendmail.cf file from the sendmail.mc, 3. copy it to the corect directory ...)
3. What command you use to start your sendmail.

Because it is obvious that you did't configured your sendmail correctly yet, you should focus on this step first. Please answer the questions above.

Last edited by hua; 05-10-2011 at 11:46 AM.
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
Using 'mail' in a script to send mail on an alternate port? tim914 Linux - Software 4 01-16-2009 03:23 PM
Send mail through script aabfm Linux - Networking 5 07-01-2006 04:46 AM
How do I send mail from inside a script? bruno buys Linux - Software 3 10-06-2004 07:57 PM
CGI Script runs to send mail, but mail is never sent robertwo Linux - Newbie 2 06-10-2004 09:57 AM
bash script to send mail Cruger Programming 1 03-06-2004 08:25 AM

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

All times are GMT -5. The time now is 04:14 PM.

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