Proper Methods for Sending Emails to Multiple Recipients Using Sendmail in Bash Scripts

Nov 26, 2025 · Programming · 6 views · 7.8

Keywords: Bash Script | Sendmail | Multiple Recipients | Email Sending | RFC 822

Abstract: This technical paper comprehensively examines common issues and solutions when using the sendmail command in Bash scripts to send emails to multiple recipients. By analyzing RFC 822 email format specifications, it explains why simple recipient lists may cause some recipients to not receive emails and provides two effective solutions: using comma-separated recipient lists and here-doc syntax. The paper delves into proper email header formatting requirements, including setting subject, from, and recipient fields, and ensuring correct separation between headers and body. Through specific code examples and detailed explanations, it helps readers understand sendmail command mechanics and best practices.

Problem Background and Common Mistakes

Using email notifications in automated scripts is a common requirement in system administration. Many developers and system administrators choose to integrate the sendmail command in Bash scripts to implement email functionality. However, when sending emails to multiple recipients, a confusing issue often arises: only some recipients successfully receive the email.

From the provided Q&A data, a typical incorrect implementation is as follows:

subject="Subject"
from="user@example.com"
recipients="user1@mail.example user2@mail.example"
mail="subject:$subject\nfrom:$from\nExample Message"

echo -e $mail | /usr/sbin/sendmail "$recipients"

The problem with this approach is that the recipient list uses space separation, while the sendmail command expects RFC-compliant email address formats. When multiple email addresses are separated by spaces, sendmail may only recognize the last address, causing other recipients to not receive the email.

Solution 1: Correct Recipient Format

According to RFC 822 email format standards, multiple recipients should be separated by commas. This is the most straightforward and standards-compliant solution:

recipients="user1@mail.example,user2@mail.example,user3@mail.example"

This format ensures that the sendmail command can correctly parse all recipient addresses. Comma separation is a widely accepted standard method in email systems, guaranteeing that emails are delivered to all specified recipients.

Solution 2: Using Here-doc Syntax

Another more reliable method is to use Shell's here-doc syntax to construct complete email content. This approach not only solves the recipient issue but also ensures correct email formatting:

/usr/sbin/sendmail "$recipients" <<EOF
subject:$subject
from:$from

Example Message
EOF

Several key points should be noted here:

RFC Standard Requirements for Email Format

According to RFC 822 standards, email messages must follow specific formatting specifications. One of the most important requirements is that there must be a blank line separating email headers from the body. This blank line tells email clients where headers end and the body begins.

In the initial incorrect implementation, \n was used to separate header fields, but no blank line was inserted between the last header field and the body. This may cause some mail servers to fail to properly parse the email content.

Advanced Technique: Using Multiple To Headers

The reference article mentions a more advanced technique: using multiple To headers to specify recipients. This method may be more advantageous in certain scenarios:

/usr/sbin/sendmail "$recipients" <<EOF
To: user1@mail.example
To: user2@mail.example
Subject: Did You Both Receive It?
From: sendmail

I hope you did
EOF

Characteristics of this method include:

Practical Implementation Recommendations

When implementing email functionality in actual Bash scripts, the following best practices are recommended:

#!/bin/bash

# Set email parameters
subject="System Notification"
from="noreply@example.com"
recipients="admin@example.com,user1@example.com,user2@example.com"

# Send email using here-doc
/usr/sbin/sendmail "$recipients" <<EOF
Subject: $subject
From: $from

Dear User:

This is an automatically generated system notification email.
Please handle relevant matters promptly.

Thank you!
System Administrator
EOF

Advantages of this implementation approach include:

Error Troubleshooting and Debugging

If issues persist during implementation, consider the following debugging steps:

  1. Verify that the sendmail command path is correct
  2. Check if email address formats are proper
  3. Ensure the script has permission to execute sendmail
  4. Examine system logs for more detailed error information
  5. Test if single recipient functionality works correctly

By following these best practices and solutions, you can ensure that the sendmail command in Bash scripts reliably sends emails to all specified recipients.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.