How to Use the Command 'swaks' (with Examples)

How to Use the Command 'swaks' (with Examples)

The swaks command, short for “Swiss Army Knife SMTP,” is an all-purpose SMTP transaction tester. It’s a versatile tool employed by network administrators, developers, and security professionals to test email sending functionalities across servers. This command helps in diagnosing and troubleshooting email delivery issues, ensuring correct server configurations, and testing security protocols such as authentication and spam or virus filtering mechanisms. With capabilities to craft, send, and test emails as per the user’s requirements, swaks proves to be an indispensable tool in an email system’s testing and validation toolkit.

Use Case 1: Deliver a Standard Test Email to user@example.com on Port 25 of test-server.example.net

Code:

swaks --to user@example.com --server test-server.example.net

Motivation:

This basic use case simulates the scenario where an email needs to be sent to a specific user through a server for testing the server’s capability to handle SMTP transactions. Network administrators commonly perform this operation to verify that the server at test-server.example.net is operating correctly and to ensure standard mail delivery functionality without authentication or encryption.

Explanation:

  • --to user@example.com: This argument specifies the recipient’s email address, indicating where the test email should be delivered.
  • --server test-server.example.net: This denotes the server that will be utilized to send the email. By default, SMTP operates over port 25.

Example Output:

=== Trying test-server.example.net:25...
=== Connected to test-server.example.net.
<-  220 test-server ESMTP Service ready
 -> EHLO localhost
<-  250-test-server Hello
...
-> MAIL FROM:<sender@example.com>
<-  250 OK
-> RCPT TO:<user@example.com>
<-  250 Accepted
-> DATA
<-  354 Enter message, ending with "."
-> Date: Fri, 01 Oct 2023 12:34:56 +0000
-> To: user@example.com
-> From: sender@example.com
-> Subject: test mail
-> X-Mailer: swaks v20201014.0 jetmore.org/john/code/swaks/
-> 
-> This is a test mailing
-> .
<-  250 OK id=1mxyz123

Use Case 2: 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

Code:

swaks --to user@example.com --from me@example.com --auth CRAM-MD5 --auth-user me@example.com --header-X-Test "test_email"

Motivation:

This scenario covers testing email delivery with SMTP authentication and custom headers, specifically using the CRAM-MD5 method. This is crucial for environments where the server mandates authentication to send emails, and adding custom headers is often used for tracking or testing purposes.

Explanation:

  • --to user@example.com: Specifies the recipient’s address.
  • --from me@example.com: Sets the sender’s email address, projecting who the email is from.
  • --auth CRAM-MD5: Designates the CRAM-MD5 authentication method, a challenge-response mechanism suitable for email services that require strong authentication.
  • --auth-user me@example.com: Indicates the username for authentication, matching the sender’s address in this context.
  • --header-X-Test "test_email": Inserts a custom X-Test header into the email, labeled “test_email”, useful for identifying this particular test message in logs.

Example Output:

=== Trying server.example.com:25...
=== Connected to server.example.com.
<-  220 server.example.com ESMTP Service ready
 -> EHLO localhost
<-  250-server.example.com Hello
-> AUTH CRAM-MD5
<- 334 44rzYXNpc24xOA==
-> abc123== 4d6568656c6c6f204a6f686e
<- 235 Authentication successful
-> MAIL FROM:<me@example.com>
<- 250 OK
-> RCPT TO:<user@example.com>
<- 250 Accepted
-> DATA
<- 354 Enter message, ending with "."
-> Date: Fri, 01 Oct 2023 12:34:56 +0000
-> To: user@example.com
-> From: me@example.com
-> Subject: test mail
-> X-Mailer: swaks v20201014.0 jetmore.org/john/code/swaks/
-> X-Test: test_email
-> 
-> This is a test mailing
-> .
<- 250 OK id=1mxyz124

Use Case 3: Test a Virus Scanner Using EICAR in an Attachment, Without Showing the Message DATA Part

Code:

swaks -t user@example.com --attach - --server test-server.example.com --suppress-data path/to/eicar.txt

Motivation:

Email systems often employ virus scanners as part of their security defenses. To ensure these systems are functioning correctly, administrators need to test their response to known virus signatures. The EICAR test file is a standardized, non-malicious file recognized as a virus by all antivirus products, used in this case to validate the server’s virus scanning capability. The suppression of message data keeps sensitive details from being displayed on the terminal.

Explanation:

  • -t user@example.com: The shorthand notation for specifying the recipient.
  • --attach -: Indicates that the message contents, such as EICAR, are to be included as an attachment.
  • --server test-server.example.com: Specifies the server through which the message is sent.
  • --suppress-data: Hides the detailed message body from appearing in the terminal output, reducing clutter and maintaining confidentiality.
  • path/to/eicar.txt: Provides the path to the EICAR test file intended for attachment in the email.

Example Output:

=== Trying test-server.example.com:25...
=== Connected to test-server.example.com.
<-  220 test-server ESMTP Service ready
-> EHLO localhost
<- 250-test-server Hello
-> MAIL FROM:<sender@example.com>
<- 250 OK
-> RCPT TO:<user@example.com>
<- 250 Accepted
-> DATA
<- 354 Enter message, ending with "."
-> Attachment: eicar.txt
...
<- 554 Transaction failed, message perceived as spam

Use Case 4: Test a Spam Scanner Using GTUBE in the Body of an Email, Routed via the MX Records for example.com

Code:

swaks --to user@example.com --body path/to/gtube_file

Motivation:

Spam scanners are crucial components of email security infrastructure. Testing these systems with GTUBE, an equivalent to EICAR but for spam, helps verify that the anti-spam mechanisms can correctly identify and handle unwanted messages. This command routes emails via MX (Mail Exchange) records, ensuring the path follows the domain’s designated email handling process.

Explanation:

  • --to user@example.com: Specifies the email address to which the test email will be sent.
  • --body path/to/gtube_file: Provides the path to a file containing the GTUBE code, ensuring it is included as the body of the email.
  • MX records are used implicitly to determine the correct routing path for the domain example.com.

Example Output:

=== Trying mx1.example.com:25...
=== Connected to mx1.example.com.
<- 220 mx1.example.com ESMTP Service ready
-> EHLO localhost
<- 250-mx1.example.com Hello
-> MAIL FROM:<sender@example.com>
<- 250 OK
-> RCPT TO:<user@example.com>
<- 250 Accepted
-> DATA
<- 354 Enter message, ending with "."
-> Body: GTUBE Test Body
<- 554 Message rejected due to spam content

Use Case 5: Deliver a Standard Test Email to user@example.com using the LMTP Protocol via a UNIX Domain Socket File

Code:

swaks --to user@example.com --socket /var/lda.sock --protocol LMTP

Motivation:

Local Mail Transfer Protocol (LMTP) is similar to SMTP but optimized for connections over UNIX domain sockets, often seen in server environments like mail servers using Postfix with Dovecot. Testing delivery over a UNIX socket is crucial to ensure that communications within server boundaries are processed correctly and efficiently.

Explanation:

  • --to user@example.com: Directs the email to the specified recipient.
  • --socket /var/lda.sock: Uses a UNIX domain socket located at the specified path for the connection.
  • --protocol LMTP: Specifies that the LMTP protocol should be used instead of SMTP, aligning with the server’s configuration for handling local message deliveries.

Example Output:

=== Trying local socket: /var/lda.sock...
=== Connected to /var/lda.sock.
<- 220 server LMTP ready
-> EHLO localhost
<- 250-server Hello
-> MAIL FROM:<sender@example.com>
<- 250 OK
-> RCPT TO:<user@example.com>
<- 250 Accepted
-> DATA
<- 354 Enter message, ending with "."
-> Date: Fri, 01 Oct 2023 12:34:56 +0000
-> To: user@example.com
-> From: sender@example.com
-> Subject: test mail
-> X-Mailer: swaks v20201014.0 jetmore.org/john/code/swaks/
->
-> This is a test LMTP message
-> .
<- 250 2.1.5 Ok

Conclusion:

The swaks command proves to be an invaluable utility for testing various SMTP and email-related functionalities. Whether it involves simple email delivery tests, verifying authentication mechanisms, or ensuring that security systems such as spam and virus scanners are effectively operational, swaks provides the flexibility and detailed insights needed to maintain robust email infrastructure. Understanding and executing these example use cases can significantly aid network administrators and technicians in ensuring seamless and secure email operations.

Related Posts

Mastering the 'cradle deploy' Command (with Examples)

Mastering the 'cradle deploy' Command (with Examples)

The ‘cradle deploy’ command is a versatile tool used within the Cradle PHP framework to manage and streamline deployment processes.

Read More
How to Use the Command 'vue serve' (with Examples)

How to Use the Command 'vue serve' (with Examples)

The vue serve command is a subcommand provided by @vue/cli and @vue/cli-service-global, designed to streamline the process of rapid prototyping with Vue.

Read More
How to Use the Command 'pamixer' (with examples)

How to Use the Command 'pamixer' (with examples)

The pamixer command-line tool is a simple yet powerful mixer specifically designed for managing audio channels in PulseAudio.

Read More