0
0
Bash-scriptingHow-ToBeginner · 2 min read

Bash Script to Send Email Notification Easily

Use the mail command in Bash like this: echo "Message body" | mail -s "Subject" recipient@example.com to send an email notification.
📋

Examples

Inputecho "Hello!" | mail -s "Test Email" user@example.com
OutputSends an email with subject 'Test Email' and body 'Hello!' to user@example.com
Inputecho "Backup completed" | mail -s "Backup Status" admin@domain.com
OutputSends an email with subject 'Backup Status' and body 'Backup completed' to admin@domain.com
Inputecho "Disk space low" | mail -s "Alert" root@localhost
OutputSends an alert email with subject 'Alert' and body 'Disk space low' to root@localhost
🧠

How to Think About It

To send an email from a Bash script, you need to create the message content and use a command that can send emails, like mail. You provide the message body via standard input and specify the subject and recipient as command options.
📐

Algorithm

1
Create the message body as a string or from a command output
2
Use the <code>mail</code> command with the <code>-s</code> option to set the email subject
3
Pipe the message body into the <code>mail</code> command
4
Specify the recipient email address
5
Run the script to send the email notification
💻

Code

bash
#!/bin/bash

message="Hello, this is a notification from your script."
subject="Notification"
recipient="user@example.com"

echo "$message" | mail -s "$subject" "$recipient"
echo "Email sent to $recipient"
Output
Email sent to user@example.com
🔍

Dry Run

Let's trace sending a notification email with message 'Hello, this is a notification from your script.'

1

Set variables

message='Hello, this is a notification from your script.' subject='Notification' recipient='user@example.com'

2

Send email

echo "$message" | mail -s "$subject" "$recipient"

3

Print confirmation

echo 'Email sent to user@example.com'

StepActionValue
1Set messageHello, this is a notification from your script.
1Set subjectNotification
1Set recipientuser@example.com
2Send email commandecho "$message" | mail -s "$subject" "$recipient"
3Print outputEmail sent to user@example.com
💡

Why This Works

Step 1: Prepare message and details

The script stores the email message, subject, and recipient in variables for easy reuse and clarity.

Step 2: Send email using mail command

The mail command sends the email; the message is piped into it, and the subject and recipient are specified with options.

Step 3: Confirm email sent

A simple echo statement confirms the script ran and attempted to send the email.

🔄

Alternative Approaches

Using sendmail command
bash
#!/bin/bash

recipient="user@example.com"
subject="Notification"
message="Hello from sendmail!"

sendmail $recipient <<EOF
Subject: $subject

$message
EOF
echo "Email sent using sendmail to $recipient"
sendmail is more complex but powerful; requires correct headers and formatting.
Using mailx command
bash
#!/bin/bash

message="Hello from mailx!"
subject="Notification"
recipient="user@example.com"

echo "$message" | mailx -s "$subject" "$recipient"
echo "Email sent using mailx to $recipient"
mailx is similar to mail but may have more features depending on system.

Complexity: O(1) time, O(1) space

Time Complexity

Sending an email is a single operation with no loops, so it runs in constant time.

Space Complexity

The script uses a fixed amount of memory for variables and pipes, so space is constant.

Which Approach is Fastest?

All methods run in constant time; differences depend on system mail setup, not script complexity.

ApproachTimeSpaceBest For
mail commandO(1)O(1)Simple email notifications
sendmail commandO(1)O(1)Advanced email formatting
mailx commandO(1)O(1)Enhanced mail features
💡
Make sure your system has a mail transfer agent (like postfix) configured to send emails.
⚠️
Forgetting to install or configure the mail command or MTA, causing emails not to send.