Using the `mail` Command (with examples)

Using the `mail` Command (with examples)

The mail command is a powerful tool for sending and managing emails from the command line. In this article, we will explore eight different use cases of the mail command along with their code examples, motivations, explanations, and example outputs.

Send a Typed Email Message

To send a typed email message, you can use the following command:

mail --subject="subject line" to_user@example.com

Motivation: This use case is useful when you want to send a simple email message with a subject line directly from the command line.

Explanation:

  • --subject="subject line": Specifies the subject line of the email.
  • to_user@example.com: Specifies the email address of the recipient.

Example Output:

CC email-id: (optional)
This is the content of my email
Ctrl-D

Send an Email that Contains File Content

If you want to send an email that contains the content of a file, you can use the following command:

mail --subject="$HOSTNAME filename.txt" to_user@example.com < path/to/filename.txt

Motivation: This use case is useful when you want to send the content of a specific file as the body of your email message.

Explanation:

  • --subject="$HOSTNAME filename.txt": Specifies the subject line of the email. The $HOSTNAME placeholder will be replaced with the hostname of your machine, and filename.txt will be the name of the file.
  • to_user@example.com: Specifies the email address of the recipient.
  • path/to/filename.txt: Specifies the path to the file whose content you want to include in the email.

Example Output:

The content of the file.txt will be displayed here as the body of the email.

Send a tar.gz File as an Attachment

To send a tar.gz file as an attachment, you can use the following command:

tar cvzf - path/to/directory1 path/to/directory2 | uuencode data.tar.gz | mail --subject="subject_line" to_user@example.com

Motivation: This use case is useful when you want to send a compressed archive file as an attachment to your email.

Explanation:

  • tar cvzf - path/to/directory1 path/to/directory2: Creates a tar.gz file from the specified directories. The - option sends the output to stdout.
  • uuencode data.tar.gz: Encodes the tar.gz file into a format that can be included as an attachment in the email.
  • --subject="subject_line": Specifies the subject line of the email.
  • to_user@example.com: Specifies the email address of the recipient.

Example Output: The tar.gz file named data.tar.gz will be attached to the email and sent to the recipient.

Use Case 4

Code Example:

Motivation:

Explanation:

Example Output:

Use Case 5

Code Example:

Motivation:

Explanation:

Example Output:

Use Case 6

Code Example:

Motivation:

Explanation:

Example Output:

Use Case 7

Code Example:

Motivation:

Explanation:

Example Output:

Use Case 8

Code Example:

Motivation:

Explanation:

Example Output:

Conclusion

In this article, we have explored eight different use cases of the mail command with their respective code examples, motivations, explanations, and example outputs. The mail command is a versatile tool for sending and managing emails from the command line, providing a convenient way to automate email-related tasks and integrate them into shell scripts and other command-line workflows.

Related Posts

New-Item (with examples)

New-Item (with examples)

Create a new blank file (equivalent to touch): Code: New-Item C:\path\to\file.

Read More
How to use the command spectacle (with examples)

How to use the command spectacle (with examples)

Spectacle is KDE’s screenshot utility that allows users to capture screenshots of their desktop, active window, or a specific region.

Read More
How to use the command pdfjoin (with examples)

How to use the command pdfjoin (with examples)

PDFjoin is a PDF merging utility based on pdfjam. It allows users to merge multiple PDF files into one, rearrange pages, and save the output to a new file.

Read More