sSMTP
Simple sendmail emulator (sSMTP) can compose an email message based on parameters you specify and relay it to an SMTP server. The tool does not work like a standalone SMTP server; in fact, it requires one for email delivery.
sSMTP is available through the package managers of multiple Linux distributions, and via source. For CentOS you can get the RPM from the Fedora Project's EPEL repository, or via Linux Packages Search.
Once you got your package installed, you need to configure it by editing the file /etc/ssmtp/ssmtp.conf as root. Each parameter in the file is well-documented, so you're unlikely to do something wrong. To get sSMTP up and running in a minimal configuration, make sure the ssmtp.conf file has at least three lines:
hostname=yourcompany.com root=yourname@yourcompany.com mailhub=smtp.yourcompany.com:25
These parameters specify your hostname, the email address for the
From:
field in the messages sSMTP sends, and the SMTP server and port for email delivery. Hostname is optional, but if you do not specify it, your email will contain the name of your computer as the sending host, which is not always what you want, and in some cases can lead to errors.This basic configuration is enough if you are going to use an SMTP server in your own network, or maybe your ISP's SMTP server, but this SMTP connection will not be secure. If you want to use a secure connection between your computer and SMTP server, for example to use Google's Gmail or Microsoft's Windows Live SMTP servers, you need to add more details into your configuration file. Here is a sample configuration for Gmail:
hostname=yourcompany.com root=yourname@gmail.com mailhub=smtp.gmail.com:587 AuthUser=yourname AuthPass=yourpassword UseSTARTTLS=YES UseTLS=YES AuthMethod=LOGIN
As you can see, a secure connection uses a different standard port on an SMTP server. You also need to provide your username and password. This password is kept in clear text in the .conf file, so make sure you provide proper security for the file. You don't have to keep the username and password in the configuration file, but if you don't, you'll need to provide them every time you use sSMTP, using the parameters
–au
and –ap
. As a workaround, if neither of these methods suits you, I suggest creating a new Gmail ID just for sending these automated emails.Once you have sSMTP installed and configured, you can use it by typing in some text manually:
$ ssmtp recipient@domain.com To:formal_recipient@domain.com Subject:test ssmtp Hello, world!
Here I specified two different recipients. The one in the command line is mandatory, and will actually receive the email. The formal_recipient will be mentioned as the recipient in email header. If you skip the
To:
field, the recipient@domain.com will still get the email, but the message will say it's for "undisclosed recipients."Note the empty line between the
Subject:
line and main body: this is the key for sSMTP to tell where the email body starts. You can type your body in several lines, then use Ctrl-D to send the message.Instead of typing the email body in the sSMTP interface, you can supply it from a text file. Let's create a file /tmp/ssmtp.txt with the following content:
To:recipient@domain.com Subject:Files can be sent from sSMTP You can email text from the file!
You still need an empty line before the body of your email. Now you can specify the file on your sSMTP command line:
$ ssmtp darkduck@darkduck.com </tmp/ssmtp.txt
This all is interesting, but manual usage of sSMTP is not something you need for automated jobs. Let's see how sSMTP works with a simple bash file.
First, create a text file /tmp/ssmtptemplate.txt with following contents:
To:recipient@domain.com Subject:results of bash script Here are the results of bash script:
Even if you don't need any text in the body of your message except for what comes from your bash script results, don't forget to leave an empty line after the
Subject:
line in the template.Now create a bash script:
#copy template to home folder cp /tmp/ssmtptemplate.txt ~ #rename file in home folder mv ~/ssmtptemplate.txt send.txt #copy files, adding results to template file cp -r -u -v -L ~/Documents/* ~/bkup/ >>~/send.txt #send the file ssmtp recipient@domain.com <~/send.txt #remove the temporary file rm ~/send.txt
This script copies the template from its location, adds the results of the jobs you want to monitor into the file, sends the file to the recipient, and removes the temporary file. If you remember, the
cp
command does not produce any output by default, so use the –v
parameter for verbose mode to ensure that the results of the copying are shown in the output file.Now you just need to make the bash script file executable and schedule it using cron so you can get regular updates with results in your inbox.
Using the same or similar methods, you can inject sSMTP command into other automated scripts – for example into shutdown scripts, or into a script that pings one server from another and sends an email in case of bad results. Both of these methods should inform you about unexpected server shutdowns or other issues that require immediate response.
Blat
While sSMTP is a good tool for Linux, you might also need something similar for Windows. Blat is a free open source Win32 emailer. You can run it on any version of Windows, either 32- or 64-bit – there are separate versions for these architectures. You can download the latest version from the project's SourceForge page.
Blat is distributed as an archive file. You need to extract the folder
full
from the archive onto your hard disk. For this example I put it in c:/blat.As with sSMTP, you can specify connection parameters directly in the command line, but it is easier to use Blat if you "install" it and get these parameters stored in the Windows registry. For a simple installation of Blat, from the Windows command line, run:
> c:/blat/blat.exe -installSMTP smtp.yourcompany.com yourname@yourcompany.com
There are many more parameters, which you can read about in the official Blat documentation, but these two, which specify the SMTP server for your outgoing mail and your email address, are the most important ones. The installation command creates an entry in the Windows registry under HKEY_LOCAL_MACHINE\SOFTWARE\Public Domain\Blat that contains the specified information.
Unfortunately, Blat does not allow secure connections by default. To use secure servers, you can install and configure the free open source application stunnel, then specify servers and ports as per stunnel's configuration. If you use stunnel on your local machine, your SMTP server should be 127.0.0.1, and stunnel will map it to the correct external secure SMTP server and port.
To use Blat on the command line, specify everything in a single command:
> c:/blat/blat.exe - -to recipient@domain.com -subject "Blat is working!" -body "Here is an email from Blat"
Use standard Windows syntax for filenames and parameters in the Blat command. If a parameter contains any spaces, it should be surrounded by quotation marks.
The first hyphen in the command, right after blat.exe, indicates the place where a text file with the email body should be. Here, I typed the body manually in the corresponding parameter instead. But Blat lets you use files to accumulate the information you need, just as sSMTP does.
To automate message transfers, create a .bat file with following contents:
mkdir "c:\backup folder" copy "c:\source folder\*.*" "c:\backup folder\" > "c:\blat body.txt" c:\blat\blat.exe "c:\blat body.txt" -to recipient@domain.com -subject "Batch job has finished" del "c:\blat body.txt"
This batch file creates a backup folder, copies the source folder files into the backup, accumulating the results in the log file, and then uses the log file as the email body in Blat. You can schedule the execution of the batch file using the built-in Windows scheduler.
sSMTP and Blat show that whatever operating system you use, you can find free open source applications that allow you to send messages to any email client.
Originally posted on Wazi
0 comments:
Post a Comment