Troubleshooting SMTP Connections Using Swaks

Sometimes, sending emails becomes an essential feature in an application’s functionality. This email delivery feature is supported by a protocol called SMTP. While SMTP is a “simple” protocol, it remains widely used today.

One of the most useful tools for verifying SMTP connectivity is swaks. This open-source tool allows you to test a server’s email-sending capability using a specific SMTP server directly from the CLI. This guide provides examples to help troubleshoot SMTP connection issues.

Prerequisites

You’ll need the following:

  • Access to the server requiring SMTP connectivity, preferably a Linux server;
  • The access details and credentials needed for SMTP.

Procedure

Installation

To install swaks on an RHEL-based Linux server, you can use the dnf command as follows:

sudo dnf install swaks

Plain SMTP Access

Plain SMTP access refers to a client-server connection without account authentication. This type of access usually relies on the server’s whitelist functionality, where the client’s IP address must be registered with the email server. Typically, plain SMTP access is conducted over port 25.

To test plain SMTP access, use the following command:

swaks --to <MAIL_RECIPIENT> \
  --server <MAIL_HOST> \
  --port 25 \
  --from <MAIL_FROM> \
  --header "Subject: <MAIL_SUBJECT>" \
  --body "<MAIL_BODY>"

NOTE

A successful email send request is usually indicated by the response Queued mail for delivery.

Access with Authentication

To test authenticated SMTP access using a username and password, use the following command:

swaks --to <MAIL_RECIPIENT> \
  --server <MAIL_HOST> \
  --port 587 \
  --auth LOGIN \
  --auth-user <MAIL_USERNAME> \
  --auth-password <MAIL_PASSWORD> \
  --tls \
  --from <MAIL_FROM> \
  --header "Subject: <MAIL_SUBJECT>" \
  --body "<MAIL_BODY>"

Conclusion

This guide on troubleshooting SMTP connections was created to assist you in diagnosing and resolving issues effectively. Hopefully, you find it useful!