Send smtp mail from linux

Send mail from command line with external smtp server on Linux

The default mail command on the Linux terminal, uses the local smtp server (mta) on port 25 to send emails. However at times you need to specify an external smtp server to use for sending mails.

For example you have just setup an smtp server, like Postfix or Exim, then you would want to test it out to check if it is receiving and relaying emails properly or not.

Being able to send mails from command line using this external smtp server is quick rather than having to setup a mail client like Thunderbird on your local machine.

There are 2 command line utilites called mailx and swaks that can be used to send mails using external smtp server. These are quite useful when you need to send emails from a bash script in an automated manner.

1. mailx command

The mailx command is available from many different packages like mailutils, heirloom-mailx etc. First you need to use the aptitude command to search the mailx package available for your system. Here is an example

$ aptitude search mailx p bsd-mailx - simple mail user agent p bsd-mailx:i386 - simple mail user agent v mailx - v mailx:i386

To find out which mailx command your system is using, run the readlink command. Here is a sample output.

$ readlink -f /usr/bin/mailx /usr/bin/bsd-mailx $ readlink -f /usr/bin/mailx /usr/bin/mail.mailutils

Not all mailx variants can use external smtp servers to send mail. Only the one that comes from the s-nail package (pulled by heirloom-mailx) can do it.

We shall be using heirloom-mailx since it allows to specify smtp connection details in a single command and issue and email quickly.

$ sudo apt-get install heirloom-mailx

Now send an email with an external smtp server like this —

echo "This is the message body and contains the message" | mailx -v -r "[email protected]" -s "This is the subject" -S smtp="mail.example.com:587" -S smtp-use-starttls -S smtp-auth=login -S smtp-auth-user="[email protected]" -S smtp-auth-password="abc123" -S ssl-verify=ignore [email protected]

Here is a step by step version of the same command —

$ echo "This is the message body and contains the message" | mailx -v \ > -r "[email protected]" \ > -s "This is the subject" \ > -S smtp="mail.example.com:587" \ > -S smtp-use-starttls \ > -S smtp-auth=login \ > -S smtp-auth-user="[email protected]" \ > -S smtp-auth-password="abc123" \ > -S ssl-verify=ignore \ > [email protected]

Make sure to use the correct settings, like port number, authentication mechanism etc. The command would produce verbose output giving full details of the smtp communication that goes on behind, making it very easy to test and debug.

Читайте также:  Linux посмотреть днс сервера

Note: The package heirloom-mailx was removed from Ubuntu version 18 onwards. Now you have to install the s-nail package.

$ sudo apt-get install s-nail

The s-nail command is the same as heirloom mailx command. Just the name is different. Run the same command above by replacing mailx with s-nail and it should work.

If you are using the latest version of s-nail (14.9.x) the syntax for specifying the smtp server details might be slightly different. Check the latest manual here

2. Swaks command

Swaks (Swiss army knife for SMTP) is a simple command line tool that can be used to test smtp servers to check if they are doing they job properly. It supports TLS as well.

Install swaks on Ubuntu/Debian with the following command

$ sudo apt-get install swaks
$ echo "This is the message body" | swaks --to [email protected] --from "[email protected]" --server mail.example.com --auth LOGIN --auth-user "[email protected]" --auth-password "abc123" -tls

All the options are pretty self explanatory. The «—server» option specifies the external SMTP server to use, «—auth» specifies the type of authentication. The «-tls» option tells swaks to use STARTTLS.

Check the man page for more options.

A Tech Enthusiast, Blogger, Linux Fan and a Software Developer. Writes about Computer hardware, Linux and Open Source software and coding in Python, Php and Javascript. He can be reached at [email protected] .

5 Comments

  1. Boris July 16, 2021 at 3:03 pm Telnet method : telnet your.smtp.server 25
    HELO your.ip
    MAIL FROM:
    RCPT TO:
    DATA
    Subject: YOUR SUBJECT
    Your message then end with a point
    .
    (then disconnect)

Источник

Send smtp mail from linux

author image

Vishal Chauhan

Published on 2019-09-20· Updated on 2020-07-17

The author voluntarily contributed this tutorial as a part of Pepipost Write to Contribute program.

Introduction

Notifications are getting more important these days to stay updated among various online activities. Moreover, these notifications can be emails, SMS or push. This tutorial is going to demonstrate you the simplest way of sending notifications, over one of the most popular channels that is email. You are going to learn the process of sending emails from the terminal or a shell script from a Linux operating system, using some of the popular CLI tools.

Читайте также:  Linux server install openssh

This tutorial is going to help you in sending critical server-level emails like Cron reports, script logs, customer registrations, receipt, bank statement over email. There are many ways to send emails from the server, but let’s explore the tool that’s easy to install and easy to code.

With simple libraries and little configurations, you can have a CLI tool in your Linux OS that you can use to send mails from the terminal.

Prerequisites

  • Linux operating system
  • SMTP Configurations (SMTP server details and authentication credentials)
  • Your favourite editor (Optional)
  • Configure Gmail SMTP/ready with any other custom SMTP server details. In this tutorial, you are going to use Gmail SMTP to send emails, so make sure that;
    undefinedundefined

Google SMTP server configurations would look something like this:

  • SMTP Server/Hostname: smtp.gmail.com
  • SMTP Username: [Your Gmail Address]
  • SMTP password: [Your Gmail Password]
  • SMTP Port: 587
  • TLS/SSL: Required

Its time to now open terminal

There are various tools and libraries which you can install to send emails from the terminal. Few of the popular libraries are:

In this tutorial, you’re going to learn the steps on how to install and use sSMTP to send mails from your Linux command line. Click here, in case you want to learn how to install and use Mailx to send mails from your Linux command line.

How to install sSMTP to send mails from your Linux command line (CLI)

Step 1

Use the below command to install ssmtp:

sudo apt-get install ssmtp

Optional:
CentOS users can use the below command to install ssmtp:

In CentOS, you may see an error during installation as «package ssmtp is not available», in such a case below command, is going to be helpful to fix the issue:

sudo yum --enablerepo=extras install epel-release

Step 2

Once ssmtp installed successfully, you need to make the below global configurations which required for sending mail.

Open the following file in your favourite editor:

sudo vim /etc/ssmtp/ssmtp.conf

Edit the above file with the below details:

mailhub=smtp.gmail.com:587 useSTARTTLS=YES AuthUser=username-here AuthPass=password-here TLS_CA_File=/etc/pki/tls/certs/ca-bundle.crt

The above configuration is going to be used to send email using your Gmail SMTP. In case you want to use some other third-party SMTP, then mention the hostname of the same. e.g. if you want to use Pepipost SMTP, then instead of smtp.gmail.com, you need to mention smtp.pepipost.com in the mailhub parameter. mailhub is used for SMTP server address which consists of two part host:port

Читайте также:  Metasploit kali linux туториал

Now you are all set to sending mails from the command line (CLI).

Step 3

There are multiple ways to use ssmtp command to send emails.

Case 1: Send Mail Directly From The Command Line

For this, copy-paste the below command, and you’re ready to send email from your command line:

-vvv is the verbosity to see the logs while sending the mail

Case 2: Send Mail From A Shell Script

You can use the same ssmtp to send mail from a shell script too. For that, open your preferred editor and create a shell script file with name saymail.shand copy-paste the below code:

#!/bin/sh SUBJECT="Test Subject" TO /cdn-cgi/l/email-protection" data-cfemail="9dfeeffce7e4dde4f2edf0fcf4f1b3fef2f0">[email protected]" MESSAGE="Hey There! This is a test mail" echo $MESSAGE | sudo ssmtp -vvv $TO

Make sure you have set the right permission access to your script file. If not, here is the command to set the permission:

Now, the code is ready to be executed. Just run the shell script using the below command:

Hope now you’re able to send mails using the shell script too.

Below are few errors/exceptions which you may encounter while sending the mail using ssmtp:

Error 1

In case while sending the email, if might get the below error as output:
ssmtp: Authorization failed (535 5.7.8 https://support.google.com/mail/?p=BadCredentials u65smyez14952a76922r5pfui.104 — gsmtp)

Solution: In such a case, try doing following as solution:

  1. Enable «Allow less secure app» in your google accounts settings, as explained in the above prerequisites section.
  2. The provided login credentials can be invalid. Make sure you have the correct credentials.

Once the issue is fixed, re-run the shell script and the success output will be something like this;

[] EHLO kali [] STARTTLS [] EHLO kali [] AUTH LOGIN [] dmlzaGFsY2hhasd2dWhhbjIyMTJAZ21haWwuY29t [] MAIL FROM:[email protected]> [] RCPT TO:[email protected]> [] DATA [] Received: by kali (sSMTP sendmail emulation); Thu, 19 Sep 2019 21:45:14 +0530 [->] From: "root" [email protected]> [->] Date: Thu, 19 Sep 2019 21:45:14 +0530 [->] Hey There! This is a test mail [->] [->] . [] QUIT [

Conclusion

Hope the steps explained above were useful and you were able to successfully send mail using linux command line (CLI). Feel free to contribute, in case you encountered some issue which is not listed as a part of this tutorials.
Use below comments section to ask/share any feedback.

Grade My Email
Check your spam now?

Netcorecloud's toolkit is the solution to all your email problems.

Источник

Оцените статью
Adblock
detector