E-mailing just got handy

The terminal is the most lethal weapon in the arsenal for a sysadmin. No GUI can ever replace the efficiency and power of Command Line Interface for a hard-core system engineer. All software applications make use of the power of the terminal behind the scenes. Interestingly, at the server level, there’s no GUI at all. The only two things you get is the a black screen, and a keyboard, and there it is, the entire organisation’s infrastructure is at your commands.

Well, the CLI isn’t loved initially. And there you got covered by the beautifully designed and smartly crafted UI for almost all utilities. Infact, the UI’s are superflexible and the go-to destination for all the new comers. But with time, one understands the capabilities of the terminal and its commands, and guess what, there’s no return! You can do anything from the terminal. There are utilities and packages for all functions, but the hard part is to find the best one for you and your needs.

But, what about emailing from the server?

I’m here to increase your on-terminal screen time. In this, I’ll help you set-up your messaging service from terminal, so that you don’t have to migrate to Gmail, everytime you need to send an email.

In this I’ll be using the SSMTP package to configure email services. It is a linux utility and requires a quick initial setup before you’re good-to-go.

Note :

  1. This is only one way mailing system. You can only send emails, and not receive from the other end.
  2. There are many such emailing services for systems, but I found this one the quickest way to get your set-up done.
  3. It can be very handy to send short code-snippets to your colleagues or quick messages to your loved ones, from a server itself.
  4. One of its primary use cases of ssmtp service is forwarding automated email (like system alerts) off your machine and to the admin email address for him to remain notified about system health.

Let’s Dive In

Get you system up and running.

  1. Update and get youself the latest of all packages.
$ sudo apt-get update$ sudo apt-get upgrade

2. Install the ssmtp utility.

$ sudo apt-get install ssmtp

3. After this, you need to configure the ssmtp service from the ssmtp.conf file at the /etc/ssmtp/ folder. (I’ve used vim, but you can use you editor of your choice)

$ cd /etc/ssmtp/
$ sudo vim ssmtp.conf

Fill the following fields in the conf file.

root=sender@gmail.com
mailhub=smtp.gmail.com
AuthUser=sender@gmail.com
AuthPass=password
UseSTRTTLS=YES
FromLineOverride=YES

After editing the above fields, your conf file must look like this.

ssmtp.conf

4. Final and the most important step. Go to the settings of your gmail account, and allow Less secure app access of the email.

allow access from gmail

Da..da..done!

There are multiple ways to send messages with this. It all depends on you, and how you want it to be sent.

1. The Quickest of all :

$ echo "Greetings from terminal" | sendmail receiver@gmail.com

(This doesn’t conatin any subject. Just the most relevant message)

email received at the other end

2. A bit descriptive :

$ sendmail receiver@gmail.com
Subject: Hello Boss
Wanted to tell you, the job's done!
Get the party ready.

This is self-descriptive email

It’s important to give double new-lines between subject body of email

After you’re done with the body, press Enter and then Ctrl-d. This takes you out of the interactive shell and sends the email.

3. Full explanatory email :

You can make an email file, which you wish to send.

$ touch message
$ vim message
detailed email
$ sendmail -t < message
or
$ ssmtp -t < message

I’ve saved the file with name message.

The -t option takes the arguments like To, Cc, etc from the message body itself.

Troubleshooting

  1. With UseSTARTTLS=yes you can only set port to 587, and not to 465.
  2. Errors like authorization falied could be due to denied access to unsecure apps from gmail account. Refer final but most important step-4.
  3. Multi-Factor authentication doesn’t allow to to grant access to less secure apps. So remove the MFA from your account.
  4. Errors might occur due to parsing error in its To, From sections.

Hacks : my fav

  1. You can send emails with differThent usernames. The username in the email, is system username.

I sent the quickest mail with root account, hence the username is set to root.

2. Your gmail account password stored in the conf file in plain text. This could be fatal, but keep calm because the conf file is only visible to root.

3. This ssmtp utility can be very well configured with docker containers to notify you about its health status.

4. Instead of sendmail, you can also use ssmtp for sending mails.

5. The final steps gives less secure apps, to acess you email account. This is most important, for messaging. Hence, I personally suggest using an entirely new account with no personal data like photos, contacts, drive in it.

Well, I’m quite sure google handles this pretty well, but who knows when…

Kudos! Now you won’t have to switch back-and-forth the gmail for simple messaging. More importantly, you can configure your system to act smart, and alert you everytime it detects something fishy, like DoS attack, unauthorized login attempts, etc.

That’s it for this blog. If you’ve come uptill here, I am sure you found this interesting. Give it a clap and share it with your nerdy friends. Consider getting connected with me on linkedin :)

In love with open-source ❤️