Send Email Alert From Script [Solved]

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Locked
zbirdman777

Send Email Alert From Script [Solved]

Post by zbirdman777 »

[/b][/b]I've been working on a script to ping various servers that I'm running and alert me if they're up or not. So far I have it in a for loop that pings each server and prints a message about whether or not they are running.

The problem is that I can't seem to get the alert portion to work. I want to have the script send me an e-mail alert if a server is down. I've tried a couple of different methods but none of them seem to work.

Here is what I've tried:

We use a gmail domain for our email system. So the email is myemail@ourcompanyname.com but it's all just a Gmail account. I don't know if that matters.

mail command:
mail -s "Alert!: Your hair is on fire!" myemail@mydomain.com < /dev/null

This throws 0 errors but I do not receive an email

sendmail:
nano /tmp/email.txt

Subject: Alert! Your Hair is on Fire!

Server IP: <ipaddress> is down
Stop whatever you're doing and fix it.

sendmail myemail@mydomain.com < /tmp/email.txt


This throws 0 errors but I do not receive an email

ssmtp:

ssmtp myemail@mydomain.com
Subject: Your Hair is on Fire
Stop whatever your are doing now!
via SMTP server.


error: Cannot open mail:25

mutt command:

mutt -s "Alert!: Your hair is on fire" myemail@mydomain.com < /dev/null
sendmail: Cannot open mail:25
Error sending message, child exited 1 ().
Could not send the message.


It looks like it's something with ports but I don't know where to go from here.
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 2 times in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
zbirdman777

Re: Send Email Alert From Script

Post by zbirdman777 »

So after a lot of searching online I solved the problem. Here is the solution for sending email from command line from start to finish.

1) Update and install ssmtp
sudo apt-get update
sudo apt-get install ssmtp


2) Configure ssmtp.conf
sudo gedit /etc/ssmtp/ssmtp.conf

3) There are options in the configuration file that need to be modified.
These are the settings I used for GMail
#
# Config file for sSMTP sendmail
#
# The person who gets all mail for userids < 1000
# Make this empty to disable rewriting.
root=youremailaddress@yourdomain
mailhub=smtp.gmail.com:587 #change this according to your mail server
AuthUser=youremailaddress@yourdomain ##These lines will need to be added
AuthPass=yourpassword ##These lines will need to be added
UseTLS=YES
UseStartTTLS=YES

# Where will the mail seem to come from?

rewriteDomain=gmail.com

# The full hostname
hostname=youremail@yourdomain

# Are users allowed to set their own From: address?
# YES - Allow the user to specify their own From: address
# NO - Use the system generated From: address
FromLineOverride=YES

4) Next you will need to use the mail client of your choice, I used mutt
mutt -s "<subject>" recipient_email@domain.com </dev/null


I suspect it will also work with sendmail and ssmtp also since it seems all three work on ssmtp.

The commands I listed above for sendmail and ssmtp should work now too.

I'll set this to solved. If I missed something feel free to let me know.
micksulley
Level 4
Level 4
Posts: 207
Joined: Fri Feb 14, 2014 11:32 am

Re: Send Email Alert From Script [Solved]

Post by micksulley »

I cannot get this to work. Also using gmail, I have copied your ssmtp.conf file -

Code: Select all

#
# Config file for sSMTP sendmail
#
# The person who gets all mail for userids < 1000
# Make this empty to disable rewriting.
#root=postmaster
root=controlsulley@gmail.com

# The place where the mail goes. The actual machine name is required no 
# MX records are consulted. Commonly mailhosts are named mail.domain.com
#mailhub=mail
mailhub=smtp.gmail.com:587
AuthUser=controlsulley@gmail.com ##These lines will need to be added
AuthPass=******** ##These lines will need to be added
UseTLS=YES
UseStartTTLS=YES

# Where will the mail seem to come from?
#rewriteDomain=
rewriteDomain=gmail.com

# The full hostname
hostname=controlsulley@gmail.com

# Are users allowed to set their own From: address?
# YES - Allow the user to specify their own From: address
# NO - Use the system generated From: address
FromLineOverride=YES
but when I run it I get -
send-mail: Cannot open smtp.gmail.com:587
Any suggestions?
micksulley
Level 4
Level 4
Posts: 207
Joined: Fri Feb 14, 2014 11:32 am

Re: Send Email Alert From Script [Solved]

Post by micksulley »

Following a suggestion on another forum I changed
mailhub=smtp.gmail.com:587
to
mailhub=smtp.gmail.com:465

and now it works!
Locked

Return to “Scripts & Bash”