LinuxCommandLibrary

swaks

Send and test email from command line

TLDR

Deliver a standard test email to user@example.com on port 25 of test-server.example.net

$ swaks [[-t|--to]] [user@example.com] [[-s|--server]] [test-server.example.net]
copy

Deliver a standard test email, requiring CRAM-MD5 authentication as user me@example.com. An "X-Test" header will be added to the email body
$ swaks [[-t|--to]] [user@example.com] [[-f|--from]] [me@example.com] [[-a|--auth]] [CRAM-MD5] [[-au|--auth-user]] [me@example.com] --header-X-Test "[test_email]"
copy

Test a virus scanner using EICAR in an attachment. Don't show the message DATA part
$ swaks [[-t|--to]] [user@example.com] --attach - [[-s|--server]] [test-server.example.com] [[-n|--suppress-data]] [path/to/eicar.txt]
copy

Test a spam scanner using GTUBE in the body of an email, routed via the MX records for example.com
$ swaks [[-t|--to]] [user@example.com] --body [path/to/gtube_file]
copy

Deliver a standard test email to user@example.com using the LMTP protocol via a UNIX domain socket file
$ swaks [[-t|--to]] [user@example.com] --socket [/var/lda.sock] --protocol [LMTP]
copy

SYNOPSIS

swaks [options]
swaks --to recipient@example.com [options]

Example 1: Basic email send
swaks --to test@example.com --from sender@example.com --server mail.example.com

Example 2: Authenticated TLS connection
swaks --to user@example.com --server smtp.gmail.com:587 --auth LOGIN --tls STARTTLS

PARAMETERS

--to


    Specifies the recipient's email address. Multiple --to options can be used to add more recipients.

--from

    Specifies the sender's email address.

--server
    Defines the SMTP server to connect to. If port is omitted, defaults to 25 or 587 with STARTTLS.

--port
    Sets the port number for the SMTP connection. Overrides port specified in --server.

--auth
    Specifies the authentication method (e.g., LOGIN, PLAIN, CRAM-MD5, DIGEST-MD5, NTLM, GSSAPI, EXTERNAL).

--auth-user
    Provides the username for authentication.

--auth-password
    Provides the password for authentication. Use with caution or consider --auth-password-file.

--tls
    Controls TLS/SSL usage. Options include STARTTLS (negotiate TLS), SSL (implicit SSL on typical SSL ports like 465), or NO (no TLS).

--data
    Reads the email body from the specified file.

--body
    Sets the email body directly from the command line.

--header
    Adds an arbitrary header to the email. Can be used multiple times. E.g., 'Subject: Test'.

--helo
    Sets the domain name used in the HELO/EHLO command. Defaults to the local hostname.

--debug
    Enables highly verbose debug output, showing the full SMTP conversation and internal workings.

--verbose
    Provides less verbose output than --debug, showing key parts of the SMTP conversation.

--quit
    Exits immediately after the initial connection, without sending any commands. Useful for quick connectivity tests.

--protocol
    Specifies the protocol to use (SMTP, ESMTP, LMTP). Defaults to ESMTP.

--attach
    Attaches a file to the email. Can be used multiple times.

--timeout
    Sets the connection and read/write timeout in seconds.

DESCRIPTION

swaks (Swiss Army Knife for SMTP) is a powerful, flexible, and feature-rich command-line tool designed for testing and debugging SMTP (Simple Mail Transfer Protocol) servers. It allows users to send emails with precise control over every aspect of the SMTP conversation, making it invaluable for network administrators, developers, and testers working with mail systems. Unlike simple mail clients, swaks enables users to simulate complex scenarios, including various authentication methods (LOGIN, PLAIN, CRAM-MD5, DIGEST-MD5, NTLM), different TLS/SSL configurations (STARTTLS, implicit SSL), custom headers, specific data payloads, and even simulating various client behaviors. It supports a wide range of SMTP extensions and can provide verbose debug output, making it an essential utility for troubleshooting mail delivery issues, verifying server configurations, and developing mail-aware applications. Its versatility stems from its extensive set of command-line options, allowing for granular control over the entire SMTP transaction.

CAVEATS

Due to its extensive options, swaks can have a steep learning curve for new users. A solid understanding of SMTP concepts, mail headers, and authentication mechanisms is beneficial for effective use. While designed for legitimate testing, its powerful capabilities could potentially be misused for sending unsolicited mail if not handled responsibly and ethically. Always ensure you have permission to test any mail server.

COMMON USE CASES


swaks is frequently used for:
- Testing email server connectivity and port accessibility.
- Verifying SMTP authentication methods and credentials.
- Debugging TLS/SSL negotiation issues.
- Sending test emails with specific headers or body content to check mail filtering rules.
- Simulating specific mail client behaviors for application development.
- Automating mail system health checks in scripts.

INSTALLATION


swaks is typically available in the package repositories of most Linux distributions. You can often install it using your distribution's package manager:
- On Debian/Ubuntu: sudo apt-get install swaks
- On Red Hat/CentOS: sudo yum install swaks or sudo dnf install swaks
- Alternatively, it can be downloaded as a Perl script from its official website and run directly.

HISTORY

swaks, standing for "Swiss Army Knife for SMTP", was created by Tony Finch. It is written in Perl and has been a staple in the toolkit of many system administrators and developers for over two decades. Its development has focused on providing comprehensive control over the SMTP protocol, enabling users to simulate almost any mail client behavior and debug complex server interactions. Its longevity and continued relevance stem from its adaptability to evolving SMTP standards and its robust feature set, making it a go-to utility for testing mail servers under various conditions.

SEE ALSO

sendmail(8), mail(1), netcat(1) or nc(1), telnet(1), openssl(1)

Copied to clipboard