How to Use SWAKS with SparkPost

How to Use SWAKS with SparkPost

How to Use SWAKS with SparkPost

Jun 24, 2021

Published by

Published by

Bird

Bird

Category:

Category:

Email

Email

Ready to see Bird
in action?

Ready to see Bird
in action?

How to Use SWAKS with SparkPost

Last year, we published a blog article that shows how to use the ubiquitous and very useful telnet tool to check an SMTP connection.

The next step is to simplify the manual process by using an awesome “next level” tool (it refers to itself as the Swiss Army Knife of email) called SWAKS. This is a command line tool that can send emails via SMTP. It’s not only easy to use but also simultaneously provides a myriad of options when needed.

At first, I was intimidated by the amount of options, but it is perfectly usable with the most basic options. If you have any issues, just bookmark this page, copy and paste the examples, and modify it to your liking. At the end of this blog, once you have gained some familiarity and don’t have a lot of variance/dynamics to your SWAKS commands, I will show you how to create a configuration file that pre-sets all your defaults parameters. This makes running SWAKS literally a button (OK, “key”) away! 

For the purpose of this exercise, we’re going to run the commands from a CentOS 7 server but the commands are similar on all supported platforms. As mentioned above, if you are stuck, just copy and paste directly into your system and modify what you need. Here are the basics:


Install

sudo yum install epel-release -y && sudo yum install swaks -y

Once installed, here’s the basic syntax to send a message. In this example, we are using the server’s local address:

swaks -s 127.0.0.1 -f sender@from.com -t recipient@recipient.com

Send a message with a “Subject” line header + other headers and then include a “Hello World!” text in the body:

swaks -s 127.0.0.1 -f me@from.com -t them@recipient.com --header "Subject: Hello! This is the subject header\Second_header:123\nThird_Header:XYZ" --body “Hello World!”

Here’s how to add an attachment, where SWAKS does the base64 conversion for you, great for testing message size limits:

swaks -s 127.0.0.1 -f me@from.com -t them@recipient.com --attach /path/to/file.tgz

You’re pretty much set on the basics! Now let’s see how else you can up your setup game. Here are some ideas:

  • Use a scheduler like chron to do basic ‘does it work’ tests which can work as a foundation  or even augment your monitoring system.

  • Measure latency when connecting and closing a connection to a mail server.

  • Test different TLS versions and ciphers

You are not going to be left alone here, I will show you how to do all the above. This time we’re going to inject into our SparkPost servers.


Send a message to our SparkPost servers (How to create the API key here, and the SMTP options).Once you get your authentication key, paste it into the command (make sure to keep the key safe and secure!) :

swaks -server smtp.sparkpostmail.com:587 -tls --auth-user SMTP_Injection --auth-password [your auth key] -f me@from.com -t them@recipient.com

If you’re looking for guidance on how to troubleshoot sending via SMTP to our servers check out this article.

Echoing best security practices, SparkPost has deprecated all older TLS versions except v1.2 and above. If you want to see if your system is compatible, SWAKS allows you to test specific TLS versions and ciphers.

swaks -s smtp.sparkpostmail.com -tls  -p 587 -f me@from.com -t them@recipient.com -tlsp tlsv1_2 --tls-cipher ECDHE-RSA-AES128-GCM-SHA256

In this example, we are using “tlsv1_2” but go ahead and change that to “tlsv1_1” and use it on other domains to observe their compatibility.

What if you want to time how fast a mail server responds? SWAKS can go through the SMTP cycle and disengage at some points without sending a message, and you can time it using the “time” command which shows at end of output:

$ time swaks -s 127.0.0.1 -f sender@from.com -t them@recipient.com test--quit-after RCPT === Trying 127.0.0.1:25... === Connected to 127.0.0.1. …  -> RCPT TO:<them@recipient.com> <-  250 2.1.5 <them@recipient.com> ok  -> QUIT <-  221 2.0.0 v1pmta5.jasdevism.com says goodbye === Connection closed with remote host. real 0m0.049s user 0m0.038s sys 0m0.009s


As promised, if you made it this far the reward is this simple sing a configuration file to set default parameters:


$ Cd $HOME $ vim .swaksrc  # Copy and paste this into the file into the .swaksrc file --from swakstest@jasdevism.com h-From: "Jas Swaks" <swakstest@jasdevism.com> -s 127.0.0.1  --body "This is a test!" --to jsingh@sparkpost.com

Once you save it, just type “swaks“ and it pre-populates everything and sends it on its merry way! Even better, you can override by adding it on the command line, say for example you want a different recipient:


swaks -t recipient@somewherelse.com

Your new standard in Marketing, Payments & Sales. It's Bird

The right message -> to the right person -> at the right time.

Your new standard in Marketing, Payments & Sales. It's Bird

The right message -> to the right person -> at the right time.