SMTP from the Command Line
There aren't many options for sending emails using SMTP when it comes to the CLI. You either have to get low-level and connect to the SMTP server and issue SMTP commands using telnet, or use a tool designed for working with SMTP from the command line. Using telnet for sending emails through SMTP is too verbose, and in case the SMTP server requires TLS encryption (or the StartTLS command, as is the case with AhaSend), it becomes impossible unless you know're down to typing in long encrypted text into the terminal.
That leaves us with utility tools for working with SMTP from the command line, and one of the most powerful and widely used tools in this area is Swaks. That's what we'll be focusing on using in this article.
What is Swaks?
Swaks (Swiss Army Knife for SMTP) is a versatile, scriptable tool used for testing and debugging SMTP servers and sending emails from the command line. It supports a wide range of SMTP features, including TLS and various authentication methods. It is not intended to be used for sending large volumes of emails, but is designed to help as a diagnositcs tool for figuring out how to connect to and send emails through an SMTP server from the command line and debug SMTP issues you're facing in your code.
Prerequisites
- Ensure Swaks is installed on your system. You can usually install it via your system's package manager. For example, on Debian-based systems, you can use
sudo apt-get install swaks
. - You have created and verified your domain on AhaSend.
- You have created SMTP credentials (username and password) for authentication.
Sending emails using Swaks
1. Plaintext Email: This command sends a basic email with a subject and a plaintext body.
swaks \
--from [email protected] \
--to [email protected] \
--server send.ahasend.com \
--port 587 \
--auth plain \
--tls \
--auth-user 'YOUR_SMTP_USERNAME' \
--auth-password 'YOUR_SMTP_PASSWORD' \
--header 'Subject: Test email' \
--body "This is a test email."
[email protected]
, [email protected]
, YOUR_USERNAME
, and YOUR_PASSWORD
with the appropriate values.2. HTML Email: To send an HTML email, you need to specify the content type and provide the HTML content in the body.
swaks \
--from [email protected] \
--to [email protected] \
--server send.ahasend.com \
--port 587 \
--auth plain \
--tls \
--auth-user 'YOUR_SMTP_USERNAME' \
--auth-password 'YOUR_SMTP_PASSWORD' \
--header 'Subject: Test email' \
--body "<html><body><h1>This is a Heading</h1><p>This is a paragraph.</p></body></html>" \
--add-header "Content-Type: text/html"
3. Attachments: Sending an email with attachments involves adding the --attach
option. Here's how you can send an email with both plaintext and an attachment.
swaks \
--from [email protected] \
--to [email protected] \
--server send.ahasend.com \
--port 587 \
--auth plain \
--tls \
--auth-user 'YOUR_SMTP_USERNAME' \
--auth-password 'YOUR_SMTP_PASSWORD' \
--header 'Subject: Test email' \
--body "This email contains an attachment." \
--attach /path/to/file.txt
/path/to/file.txt
with the actual path to the file you want to attach.