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?
Good news hackers, I got you!
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.
- This is only one way mailing system. You can only send emails, and not receive from the other end.
- There are many such emailing services for systems, but I found this one the quickest way to get your set-up done.
- It can be very handy to send short code-snippets to your colleagues or quick messages to your loved ones, from a server itself.
- 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.
- 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.
After editing the above fields, your conf file must look like this.
4. Final and the most important step. Go to the settings of your gmail account, and allow Less secure app access of the email.
Let’s send messages
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 firstname.lastname@example.org
(This doesn’t conatin any subject. Just the most relevant message)
2. A bit descriptive :
$ sendmail email@example.com
Subject: Hello BossWanted 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
$ sendmail -t < message
$ 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.
- With UseSTARTTLS=yes you can only set port to 587, and not to 465.
- Errors like authorization falied could be due to denied access to unsecure apps from gmail account. Refer final but most important step-4.
- Multi-Factor authentication doesn’t allow to to grant access to less secure apps. So remove the MFA from your account.
- Errors might occur due to parsing error in its To, From sections.
Hacks : my fav
- 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 :)