Introduction

Sendmail is a widely used mail transfer agent (MTA) that allows you to send emails from the command line. It is a powerful tool that can be used for various purposes, such as sending notifications, alerts, or even automating email tasks. In this tutorial, we will explore how to use Sendmail from the command line, providing you with a quick and easy guide to get started.

Prerequisites

Before we dive into using Sendmail, make sure you have the following:

  • A Linux or Unix-based operating system
  • Sendmail installed on your system

Sending a Basic Email

To send a basic email using Sendmail, follow these steps:

  1. Open your terminal
  2. Type the following command:
echo "Subject: Hello World" | sendmail recipient@example.com

Replace recipient@example.com with the actual email address of the recipient. This command will send an email with the subject “Hello World” to the specified recipient.

Adding a Message Body

To include a message body in your email, you can use the -t option followed by the recipient’s email address. Here’s an example:

echo -e "Subject: Hello World\n\nThis is the message body." | sendmail -t recipient@example.com

In this example, we use the -e option to enable interpretation of backslash escapes, allowing us to include line breaks in the message body.

Attaching a File

To attach a file to your email, you can use the -a option followed by the file path. Here’s an example:

echo -e "Subject: Hello World\n\nThis is the message body." | sendmail -a /path/to/file.txt recipient@example.com

Replace /path/to/file.txt with the actual path to the file you want to attach. This command will attach the specified file to the email.

Specifying the Sender

By default, Sendmail uses the system’s default email address as the sender. However, you can specify a different sender using the -f option. Here’s an example:

echo -e "Subject: Hello World\n\nThis is the message body." | sendmail -f sender@example.com recipient@example.com

Replace sender@example.com with the actual email address of the sender. This command will send the email with the specified sender.

Frequently Asked Questions

1. How do I check if Sendmail is installed on my system?

To check if Sendmail is installed on your system, open your terminal and type the following command:

sendmail -v

If Sendmail is installed, you will see the version information. If it is not installed, you will receive an error message.

2. Can I send emails to multiple recipients?

Yes, you can send emails to multiple recipients by separating their email addresses with commas. For example:

echo "Subject: Hello World" | sendmail recipient1@example.com, recipient2@example.com

3. How can I send an email with HTML content?

To send an email with HTML content, you can use the -i option followed by the -t option. Here’s an example:

echo -e "Subject: Hello World\nContent-Type: text/html\n\n<html><body><h1>Hello</h1></body></html>" | sendmail -i -t recipient@example.com

In this example, we set the Content-Type header to text/html and include the HTML content within the email body.

Conclusion

Congratulations! You have learned how to use Sendmail from the command line. You can now send emails effortlessly using this powerful tool. Remember to explore the various options and commands available to customize your emails further. Happy emailing!

Sources: