LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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-22-2009, 02:47 AM   #1
B-Boy
Member
 
Registered: Jan 2008
Posts: 103

Rep: Reputation: 15
Send a mail from bash


Hi guys


How can I send a mail from bash to my exchange email address?
 
Old 05-22-2009, 02:59 AM   #2
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
Use sendmail. Here is an example:
Code:
/usr/sbin/sendmail "user@some.somain" << EOF
From: Me <user@another.domain>
To: Mario Rossi <user@some.domain>
Subject: testmail - $status

$(date "+%A %e %b %Y %T") - status is - $status

$(cat $logfile)

$([ -f $errfile ] && cat $errfile)

EOF
You can also send an e-mail in HTML format. I use this to colorize some flags in a log report:
Code:
(
 echo "From: Me <user@domain>"
 echo "To: Mario Rossi <user@some.domain>"
 echo "Cc: Luigi Bianchi <user@domain>"
 echo "Subject: Report - $status"
 echo "MIME-Version: 1.0"
 echo "Content-Type: text/html"
 echo "Content-Disposition: inline"
 echo
 echo "<pre>$(date +"%A %_d %b %Y %H:%M:%S") - status is $status_c</pre>"
 echo
 cat $HOME/log.html
 echo
) | /usr/sbin/sendmail "user@domain"
As you can see you can embed shell variables and command substitution in the mail body or in the Subject field.
 
Old 05-22-2009, 03:05 AM   #3
B-Boy
Member
 
Registered: Jan 2008
Posts: 103

Original Poster
Rep: Reputation: 15
Quote:
/usr/sbin/sendmail "user@some.somain" << EOF
From: Me <user@another.domain>
To: Mario Rossi <user@some.domain>
Subject: testmail - $status

$(date "+%A %e %b %Y %T") - status is - $status

$(cat $logfile)

$([ -f $errfile ] && cat $errfile)

EOF
when i use this one it just hangs?

Is there a log filed i can see for errors?
 
Old 05-22-2009, 03:22 AM   #4
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 B-Boy View Post
Is there a log filed i can see for errors?
Nope. However, you can launch the script using
Code:
bash -x script.sh
so that you will see a trace of all the executed commands and the point at which it hangs. Be sure to put the closing EOF string at the beginning of a new line (without leading spaces or tabs) otherwise it waits for the input to be closed.
 
Old 05-22-2009, 04:29 AM   #5
rikijpn
Member
 
Registered: Jun 2007
Location: Japan
Distribution: Debian lenny, DSL, Solaris 10
Posts: 157

Rep: Reputation: 33
just get mailx. Then you'd be able to do things like:
Code:
mail -s "My subject" someone@somewhere.com
this is my email's body.
and even put a line like that on cron to e-mail somewhere automatically whenever you want.

Last edited by rikijpn; 05-22-2009 at 04:30 AM.
 
  


Reply



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
Send e-mail by bash script kalimat Programming 11 02-10-2009 05:54 AM
Postfix : mail cannot send to send outside ( can send/receive locally) bobbinsupport Linux - Networking 3 12-15-2007 10:40 PM
bash script to send a mail attachment aeby Linux - General 6 05-28-2007 06:13 AM
How to send mail using sh/bash in Solaris just_a_kid Linux - Newbie 3 01-26-2007 11:06 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 12:29 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