How to Send an Email Using the Sendmail Command In Linux?

7 minutes read

To send an email using the sendmail command in Linux, follow these steps:

  1. Open the terminal or command line on your Linux system.
  2. Enter the following command to start composing the email: echo -e "Subject: Your email subject\n\nYour email content" | sendmail recipient@example.com Replace "Your email subject" with the desired subject for your email and "Your email content" with the actual content you want to send.
  3. Replace "recipient@example.com" with the email address of the recipient. You can also add multiple recipients by separating their email addresses with commas.
  4. Hit Enter to send the email.
  5. If no errors are encountered, the email will be delivered to the specified recipient(s). You should see a confirmation message or the terminal will return to the command prompt.


Note: The sendmail command relies on a properly configured mail server on your Linux system. It is crucial to set up a functional email server beforehand; otherwise, the email may not be delivered. Additionally, check whether your Linux distribution has the sendmail command installed.

Best Linux Books to Read in 2024

1
Linux Bible

Rating is 5 out of 5

Linux Bible

2
CompTIA Linux+ Certification All-in-One Exam Guide, Second Edition (Exam XK0-005)

Rating is 4.9 out of 5

CompTIA Linux+ Certification All-in-One Exam Guide, Second Edition (Exam XK0-005)

3
How Linux Works, 3rd Edition: What Every Superuser Should Know

Rating is 4.8 out of 5

How Linux Works, 3rd Edition: What Every Superuser Should Know

4
CompTIA Linux+ Study Guide: Exam XK0-005

Rating is 4.7 out of 5

CompTIA Linux+ Study Guide: Exam XK0-005

5
Linux All-In-One For Dummies (For Dummies (Computer/Tech))

Rating is 4.6 out of 5

Linux All-In-One For Dummies (For Dummies (Computer/Tech))

6
The Linux Command Line, 2nd Edition: A Complete Introduction

Rating is 4.5 out of 5

The Linux Command Line, 2nd Edition: A Complete Introduction

7
Linux Basics for Hackers: Getting Started with Networking, Scripting, and Security in Kali

Rating is 4.4 out of 5

Linux Basics for Hackers: Getting Started with Networking, Scripting, and Security in Kali


What is the purpose of the -t flag in the sendmail command?

The -t flag in the sendmail command is used to specify that the email message should be read from standard input. This allows the user to manually enter the email message, including the headers and body, when composing the email.


By using the -t flag, the sendmail command will read the email message from the standard input until it encounters a line with a single period (.) indicating the end of the message. This enables the user to compose and send emails directly from the command line or use scripts to generate and send email messages programmatically.


How to specify the sender's name in the email sent via sendmail?

To specify the sender's name in the email sent via sendmail, you need to add the appropriate header to the email. The sender's name is typically added as part of the "From" header.


Here's an example of how you can specify the sender's name using sendmail:

1
2
3
4
5
6
7
subject="Subject: Your subject here"
from="From: Your Name <sender@example.com>"
to="To: recipient@example.com"

message="Your email message here"

echo -e "$subject\n$from\n$to\n\n$message" | sendmail -t


In the $from variable, replace "Your Name" with the desired sender's name, and "sender@example.com" with the actual email address of the sender.


Make sure to update the email addresses in the to variable as well.


Combine this example with the desired subject, message, and recipient address, and execute the script or command to send the email. Note that you need sendmail installed on your system to use this method.


What is the purpose of the -i flag in the sendmail command?

The purpose of the -i flag in the sendmail command is to specify that the message being sent does not end with a dot on a line by itself, which is the default behavior. By using the -i flag, the sendmail command treats the end of the message as if it were a blank line, allowing the message to be sent without needing to add a final dot. This is useful when sending messages from a file or in automated scripts where manually adding the final dot may be an inconvenience.

Facebook Twitter LinkedIn Whatsapp Pocket

Related Posts:

To send emails via Gmail from Dart, you can use the mailer package. Here are the steps to achieve this:Set up your Dart project by creating a new Dart file and importing the necessary packages. import &#39;package:mailer/mailer.dart&#39;; import &#39;package:m...
Visual Studio Code is a lightweight and versatile source code editor developed by Microsoft. Although originally designed for Windows, it is possible to run Visual Studio Code on Linux systems as well. Here are the steps to run Visual Studio Code on Linux:Down...
Antivirus software is essential for maintaining the security of your computer, even if you are using macOS or Linux systems. While these operating systems are generally more secure than Windows, they are still susceptible to malware and other threats. Here is ...