Script to Send an email using mail command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script to Send an email using mail command
# 1  
Old 12-04-2011
Java Script to Send an email using mail command

Hello all

I'm trying to write a script to send an email, so I can be nnotified when something is going wrong with my system.

I've tried many options with no luck:
Code:
mail -s "You've got mail"  somebody@example.com

echo "Mail Body" | mail -s "Subject" somebody@example.com

I tried also to pass parameter via a script with no luck:
Code:
#!/bin/bash
# script to send simple email
# email subject
SUBJECT="SUBJECT"
# Email To ?
EMAIL="s.odeh@masiya.net"
# Email text/message
EMAILMESSAGE="/tmp/emailmessage.txt"
echo "This is an email message test"> $EMAILMESSAGE
echo "This is email text"
echo "This is email text" >>$EMAILMESSAGE
# send an email using /bin/mail
/bin/mail -s "$SUBJECT" "$EMAIL" < $EMAILMESSAGE
echo "done"

Does "mail" command requires any special config on my server to let it work.
I'm running Solaris 10.

Many thanks for help.

Last edited by Scott; 12-04-2011 at 10:03 AM.. Reason: Code tags, please...
# 2  
Old 12-04-2011
This is generic, usually works in most of *nix systems. But MTA must be configured (sendmail, postfix, exim, ...). Smart relay is usually the easiest method.
Code:
#!/usr/bin/somesh     bash,ksh,dash,...
[ $# -lt 4 ] && echo "usage:$0 From To Subject msgfile" >&2 && exit 1
From="$1"
To="$2"
Subject="$3"
MsgFile="$4"
cat <<EOF | /usr/lib/sendmail  -i -t
From: $From
To: $To
Subject : $Subject

$(cat $MsgFile)

EOF

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need command to capture word from shell script and send email

Hello Experts, Greeting to all of you. I have a requirement, that we have a shell script status.sh that check the status of server and server status shows as status.sh Enterprise Server - Running Admin Server - Shutdown Requirement is like whenever the output shows shutdown it should... (2 Replies)
Discussion started by: aks_1902
2 Replies

2. Shell Programming and Scripting

Use of java mail to send email from UNIX

Can someone help me to understand how to configure java mail on unix or what is syntex for that to put in script so email can be sent. earlier i was using 'send mail' to send email and not SMPT is stopped in unix machine and they asked us to use java mail. Before I was using this ... (0 Replies)
Discussion started by: mirwasim
0 Replies

3. UNIX for Advanced & Expert Users

Unable to send mail with 'mail' command

I am unable to cause the 'mail' command to send mail from my linux ubuntu 15.10 computer. File 'mail.log' typically reports Connection timed out. I issue the command: mail -s "my subject" recipient@domain.com < filenamewhere filename is a file containing my message. Specifically, the... (3 Replies)
Discussion started by: tcnm
3 Replies

4. Windows & DOS: Issues & Discussions

[Template] Send SMTP email with Visual Basic Script (vbs) from Command Prompt using CDO

This is a script in visual basic that basically sends an email from an existing account, in this case the example is provided for live.com. It uses CDO (Collaboration Data Objects). All you need to do is replace the fields with the proper information. The email is send from Command Prompt... (0 Replies)
Discussion started by: Tribe
0 Replies

5. Red Hat

Send email using mail

I need to send an email from linux using mail command. The problem is I can't hardcode the to: emailaddress, The email address is saved in a recipient.lst file on the server in this format: To: a@b.com Thanks a lot. il_labor (1 Reply)
Discussion started by: il_labor
1 Replies

6. Linux

Send email using mail

I need to send an email from linux using mail command. The problem is I can't hardcode the to: emailaddress, The email address is saved in a recipient.lst file on the server in this format: To: a@b.com Thanks a lot. il_labor (1 Reply)
Discussion started by: il_labor
1 Replies

7. UNIX for Dummies Questions & Answers

Configuring mail to send email from server to client

Hi, I want to send an email from server to my gmail account for all critical messages reported in the server. Kindly help me in configuring the same. Regards Rochit (1 Reply)
Discussion started by: rochitsharma
1 Replies

8. Shell Programming and Scripting

Script to send email after comparing the folder permissions to a certain permission & send email

Hello , I am trying to write a unix shell script to compare folder permission to say drwxr-x-wx and then send an email to my id in case the folders don't have the drwxr-x-wx permissions set for them . I have been trying to come up with a script for few days now , pls help me:( (2 Replies)
Discussion started by: nairshar
2 Replies

9. AIX

Command line/Script to send E-mail with HTML body and binary attachment

I apoligize for the cross-post but I'm not getting much in the way of help in the dummies forum: I'm trying to script sending an e-mail message on an AIX 5.x server with the following requirements: 1. command line switch to specify file name containing message body in HTML format 2. command... (3 Replies)
Discussion started by: G-Man
3 Replies

10. UNIX for Dummies Questions & Answers

how to send mail from server to client's email-id

i want to send email from server to the client's email-id. server is solaris. please help me out.i want to send mail to the email-id not to the var/spool/mail. (8 Replies)
Discussion started by: parmeet
8 Replies
Login or Register to Ask a Question