There are multiple ways of sending emails with attachments. Here I am showing you how you can send emails by generating an app-specific password while using Gmail. The mailx command will be used to send the mail. The -A option in mailx enables attaching files to the email. This demonstration will show you the whole process step by step.
Key Takeaways
- Sending email with the mailx command
- Sending mail with attachment.
- Different options of mailx command.
Free Downloads
Sending Email With Attachment by Setting up SMTP Server
Before sending an email with an attachment using Bash script there are multiple steps you must follow properly. I am going to discuss all the steps from generating an app-specific password to writing a Bash script for sending email in the following steps.
Step 1: Creating an App-Specific Password
The first step of sending an email with an attachment is to create an App-Specific Password. If you are a Gmail user follow the steps below to generate an App-Specific Password.
❶ At first, enable the 2FA. Follow this documentation to enable 2FA on Gmail.
❷ Once you have 2FA enabled, visit this site to generate an app-specific password. Read more about generating app-specific password from this article.
Step 2: Installing “mailx” command
The mailx command may not be already installed in your system. Run the following command to install it. Go through this documentation if you face any issues while installing the mailx command.
sudo apt-get install mailutils
Install SMTP using the following command:
sudo apt-get install ssmtp
- ssmtp: sSMTP delivers email from a local computer to a configured mailhost.
Run the following command to configure the smtp configuration file:
sudo nano /etc/ssmtp/ssmtp.conf
- nano: Opens a file in the nano text editor.
- /etc/ssmtp/ssmtp.conf: The path of the smtp configuration file in the system.
Read this guide to know more about installing and configuring the smtp configuration file.
Step 3: Specific Location of Attachment File
The file you want to attach with your email must be located somewhere in your system. You have to specify the pathname of the file while writing a script for sending email. Hence a clear idea about the attachment file location is necessary. For example, the Bash_Script.pdf can be found in the following path of my machine and I am going to attach this file with my email.
/home/laku/Bash_Script.pdf
Step 4: Bash Script for Sending Emails
At this point, we are going to write a Bash script that will ultimately send the email with an attachment.
Steps to Follow >
❶ Write the following command to open a sendmail.sh file in the build-in nano editor:
nano sendmail.sh
- nano: Opens a file in the Nano text editor.
- sendmail.sh: Name of the file.
Script (sendmail.sh) >
#!/bin/bash
# Email details
recipient="[email protected]"
subject="Bash Script"
body="This is a cheat sheet that contains all the basics of Bash scripting. Read this for primary idea about Bash scripting"
# File attachment
attachment="/home/laku/Bash_Script.pdf"
# Check if the attachment file exists
if [ ! -f "$attachment" ]; then
echo "Attachment file not found: $attachment"
exit 1
fi
# Sending the email with attachment
echo -e "$body" | mailx -s "$subject" -A "$attachment" "$recipient"
echo "Email sent successfully with attachment: $attachment"
This Bash script is designed to send an email with an attachment using the mailx command. The recipient‘s email address, email subject and the email body content is set initially. The path to the attachment file Bash_Script.pdf is stored in the attachment variable.
Before sending the email, the script checks if the attachment file exists using an if statement. If it exits the script proceeds to send the email with the specified details. The email body is passed to the mailx command using echo command. The -A option is used to attach the file. Once the email is sent successfully, a confirmation message is displayed, indicating the name of the attached file.
➌ Now, Use the following command to make the file executable:
chmod u+x sendmail.sh
- chmod: Changes permissions.
- u+x: Giving the owner executing permission.
- sendmail.sh: Name of the script.
➍ Finally, Run the script by the following command:
./sendmail.sh
When executed, this script will send an email to the recipient [email protected] with the subject “Bash Script” and the attachment Bash_Script.pdf with the given body text.
Conclusion
In summary, sending emails with attachments has become a routine activity in modern days. Although sending emails with attachments using a Bash script can be challenging, I believe now you can easily accomplish this task. Hope you enjoy reading this article.
People Also Ask
Related Articles
- How to Get Date in Bash [2 Methods with Examples]
- How to Print Time in Bash [2 Quick Methods]
- How to List Users in Bash [2 Easy Ways]
- How to Get Current Time in Bash [4 Practical Cases]
- How to Use Date Format in Bash [5 Examples]
- How to Get Timestamp in Bash [2 Practical Cases]
- How to Copy and Paste in Bash [2 Methods & Cases]
- How to Read Password in Bash [3 Practical Cases]
- How to Send Email in Bash [2 Easy Methods]
- How to Get IP Address in Bash [3 Methods]
- How to Find and Replace String in Bash [5 Methods]
- How to Get Script Name Using Bash Script? [3 Easy Ways]
- How to Call Another Script in Bash [2 Methods]
- How to Generate UUID in Bash [3 Simple Methods]
- 3 Easy Ways to Write to a File in Bash Script
- How to Write the Output to a File in Bash Script [5 Practical Cases]
- How to Create a List in Bash Scripts? [2 Easy Methods]
- How to Clear History in Bash [2 Practical Cases]
- How to Clear Screen Using Bash Script? [2 Effective Methods]
- How to Check Ubuntu Version Using Bash Scripts? [2 Methods]
<< Go Back to Bash Script Examples | Bash Scripting Basics | Bash Scripting Tutorial
FUNDAMENTALS A Complete Guide for Beginners