Send Emails from the Command Line with Swaks

Send emails through AhaSend’s SMTP servers directly from your terminal using Swaks, a powerful command-line tool designed for testing and debugging SMTP servers. Perfect for scripting, automation, and troubleshooting email delivery issues.
What is Swaks? Swaks (Swiss Army Knife for SMTP) is a versatile, scriptable tool for testing SMTP servers and sending emails from the command line. It supports TLS encryption, various authentication methods, and is designed as a diagnostic tool rather than for high-volume sending.

Prerequisites

Before you begin, ensure you have:
Need SMTP Credentials? If you haven’t created SMTP credentials yet, check out our SMTP Credentials guide for step-by-step instructions.

Installation

Install Swaks using your system’s package manager:
sudo apt-get update
sudo apt-get install swaks

Connection Settings

Use these settings for all Swaks commands with AhaSend:

Primary Server

Host: send.ahasend.com Ports: 587 (recommended), 25, 2525 Security: TLS (—tls flag) Authentication: Plain (—auth plain)

US Server

Host: send-us.ahasend.com Ports: 587 (recommended), 25, 2525 Security: TLS (—tls flag) Authentication: Plain (—auth plain)

Basic Email Examples

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: Welcome to our platform!' \
  --body "Thanks for signing up. We're excited to have you!"

Advanced Examples

swaks \
  --from [email protected] \
  --to [email protected],[email protected] \
  --server send.ahasend.com \
  --port 587 \
  --auth plain \
  --tls \
  --auth-user 'YOUR_SMTP_USERNAME' \
  --auth-password 'YOUR_SMTP_PASSWORD' \
  --header 'Subject: Newsletter Update' \
  --body "This is our latest newsletter update."

Using Special Headers

Add AhaSend’s special headers to control tracking, retention, and other features:
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: Newsletter with Tracking' \
  --add-header "ahasend-track-opens: true" \
  --add-header "ahasend-track-clicks: true" \
  --add-header "ahasend-tags: newsletter,cli,swaks" \
  --body "<html><body><h1>Newsletter</h1><p>Check out our <a href='https://yourdomain.com/features'>latest features</a>!</p></body></html>" \
  --add-header "Content-Type: text/html"

Testing with Sandbox Mode

Use sandbox mode to safely test your email integration:
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: Sandbox Test Email' \
  --add-header "AhaSend-Sandbox: true" \
  --add-header "AhaSend-Sandbox-Result: deliver" \
  --body "This email is sent in sandbox mode for testing."
Sandbox Benefits: Emails sent in sandbox mode are free, trigger webhooks normally, and never actually deliver to recipients - perfect for development and testing.

Scripting and Automation

Shell Script Example

Email Script Example
#!/bin/bash

# Configuration
SMTP_SERVER="send.ahasend.com"
SMTP_PORT="587"
SMTP_USERNAME="YOUR_SMTP_USERNAME"
SMTP_PASSWORD="YOUR_SMTP_PASSWORD"
FROM_EMAIL="[email protected]"

# Function to send email
send_email() {
    local to_email="$1"
    local subject="$2"
    local body="$3"

    swaks \
        --from "$FROM_EMAIL" \
        --to "$to_email" \
        --server "$SMTP_SERVER" \
        --port "$SMTP_PORT" \
        --auth plain \
        --tls \
        --auth-user "$SMTP_USERNAME" \
        --auth-password "$SMTP_PASSWORD" \
        --header "Subject: $subject" \
        --body "$body"
}

# Send notification email
send_email "[email protected]" "System Alert" "The backup process has completed successfully."

# Send welcome email
send_email "[email protected]" "Welcome!" "Thanks for joining our platform."

Environment Variables

For security, use environment variables for credentials:
Environment Setup
# Set environment variables
export AHASEND_SMTP_USERNAME="your_smtp_username"
export AHASEND_SMTP_PASSWORD="your_smtp_password"
export AHASEND_FROM_EMAIL="[email protected]"

# Use in swaks command
swaks \
  --from "$AHASEND_FROM_EMAIL" \
  --to [email protected] \
  --server send.ahasend.com \
  --port 587 \
  --auth plain \
  --tls \
  --auth-user "$AHASEND_SMTP_USERNAME" \
  --auth-password "$AHASEND_SMTP_PASSWORD" \
  --header 'Subject: Secure Email' \
  --body "This email uses environment variables for credentials."

Resources

Pro Tip: Start with sandbox mode and basic commands before building complex automation. Use environment variables for credentials and always test scripts thoroughly before production use.