To send an email with an attachment using the sendmail command in a shell script, we can follow the steps explained in this article. Bobcares, as a part of our Server Management Service offers solutions to every query that comes our way.
Overview
- Sendmail Command in Shell Script to Send a Mail with an Attachment
- Sendmail Command in Shell Script to Send a Mail with Multiple Attachments
- Possible Issues & Fixes
- How to Test the Script?
- Conclusion
Sendmail Command in Shell Script to Send a Mail with an Attachment
In Linux, the sendmail command is a useful tool for sending emails from shell scripts or the command line. Sendmail is simple to use in its basic form, however sending an email with an attachment takes a little more effort because sendmail does not directly support attachments. But, we can obtain this by using MIME (Multipurpose Internet Mail Extensions) format for the email and encoding the attachment. The steps to send the mail with attachment using Sendmail command is as follows:
1. Prepare the email body: The email body must be written first, together with headers such as From, To, Subject, and the attachment within a MIME framework.
2. Use sendmail to Deliver the Email: Once the email has been prepared correctly, we may send it by piping the message to sendmail.
An Example
#!/bin/bash # Define email variables FROM="sender@example.com" TO="recipient@example.com" SUBJECT="This is a test email with attachment" BODY="This is the body of the email." ATTACHMENT="/path/to/your/file.txt" FILENAME=$(basename "$ATTACHMENT") BOUNDARY="ZZ_/afg6432dfgkl.94531q" # Create the email header and body { echo "From: $FROM" echo "To: $TO" echo "Subject: $SUBJECT" echo "MIME-Version: 1.0" echo "Content-Type: multipart/mixed; boundary=\"$BOUNDARY\"" echo "" echo "--$BOUNDARY" echo "Content-Type: text/plain; charset=US-ASCII" echo "Content-Transfer-Encoding: 7bit" echo "" echo "$BODY" echo "" # Add the attachment echo "--$BOUNDARY" echo "Content-Type: application/octet-stream; name=\"$FILENAME\"" echo "Content-Transfer-Encoding: base64" echo "Content-Disposition: attachment; filename=\"$FILENAME\"" echo "" # Encode the attachment using base64 base64 "$ATTACHMENT" echo "" # End the email echo "--$BOUNDARY--" } | sendmail -t
The explanation of the script is as follows:
Headers: Includes sender (From), recipient (To), and Subject.
MIME: Uses MIME format to support attachments.
Email Body: Plain text section.
Attachment: Encoded in base64 and added as a separate section.
Sendmail: Pipes the complete email to sendmail -t to send it.
We must also make sure sendmail is installed and configured correctly on the system.
Sendmail Command in Shell Script to Send a Mail with Multiple Attachments
We just need to repeat the MIME section for each file to send several attachments. For example:
echo "--$BOUNDARY" echo "Content-Type: application/octet-stream; name=\"file1.txt\"" echo "Content-Transfer-Encoding: base64" echo "Content-Disposition: attachment; filename=\"file1.txt\"" echo "" base64 "/path/to/file1.txt" echo "" echo "--$BOUNDARY" echo "Content-Type: application/octet-stream; name=\"file2.pdf\"" echo "Content-Transfer-Encoding: base64" echo "Content-Disposition: attachment; filename=\"file2.pdf\"" echo "" base64 "/path/to/file2.pdf" echo ""
Each attachment gets its own MIME boundary, type, and base64-encoded content.
Base64 Encoding: Including non-text files (such PDFs, pictures, etc.) as email attachments requires base64, a command-line tool that translates binary data into ASCII text. Email protocols were initially intended to handle text-based data, hence encoding is required since binary data must be transformed into a secure format before being sent. Within the script:
base64 “$ATTACHMENT”
Before attaching the file to the email body, this command encodes it in base64 format.
Possible Issues & Fixes
1. File Not Found: An error will appear if the file path to the attachment is not right. Verify the file exists and that the path to the attachment is correct.
2. Un-Installed Sendmail: We can install sendmail using the package management if it’s not installed on the server:
On Ubuntu/Debian:
sudo apt install sendmail
On CentOS/RHEL:
sudo yum install sendmail
3. Sendmail Configuration: To send emails, make sure the sendmail service is set up properly. Usually, this entails configuring an MTA (Mail Transfer Agent) on the server.
4. Email Clients Blocking Attachments: Certain file types (such as.exe files) or big attachments may be blocked by some email clients or servers. If this problem persists, check the settings on the client or server.
How to Test the Script?
To help in debugging, we can change the script so that it prints the contents of the email to the terminal rather than sending it. In place of the sendmail line, use:
} > /tmp/email.txt
This allows us to examine the newly created email before sending it by saving it to /tmp/email.txt.
[Need to know more? We’re available 24/7.]
Conclusion
In conclusion, we must set up the email using MIME format in order to send an attachment using sendmail on Linux. Creating email headers, base64 encoding the attachment, and sending it via sendmail are the steps in this process. Multiple attachments can be added using this method, but it does require path setup and suitable system setup for sendmail. So, we’ve to check file paths and make sure sendmail is installed and set correctly in order to debug.
0 Comments