How to put mail cntent in linux script mailx

The mailx command in Linux allows you to send emails from the command line. To put mail content in a Linux script using mailx, you can use the -s option to specify the subject and the -b option to specify the body of the email.

Here is an example of how you can use mailx in a Linux script:

#!/bin/bash

# Set the recipient's email address
RECIPIENT="[email protected]"

# Set the subject of the email
SUBJECT="Test email from script"

# Set the body of the email
BODY="This is a test email sent from a Linux script."

# Use mailx to send the email
mailx -s "$SUBJECT" -b "$BODY" $RECIPIENT

In this example, the script sets the recipient's email address, subject, and body, and then uses mailx to send the email.

You can also use the -a option to attach a file to the email:

#!/bin/bash

# Set the recipient's email address
RECIPIENT="[email protected]"

# Set the subject of the email
SUBJECT="Test email from script"

# Set the body of the email
BODY="This is a test email sent from a Linux script."

# Set the file to attach
FILE="example.txt"

# Use mailx to send the email
mailx -s "$SUBJECT" -b "$BODY" -a "$FILE" $RECIPIENT

In this example, the script sets the recipient's email address, subject, body, and file to attach, and then uses mailx to send the email.

You can also use the -c option to specify a file that contains the email content:

#!/bin/bash

# Set the recipient's email address
RECIPIENT="[email protected]"

# Set the subject of the email
SUBJECT="Test email from script"

# Set the file that contains the email content
FILE="email.txt"

# Use mailx to send the email
mailx -s "$SUBJECT" -c "$FILE" $RECIPIENT

In this example, the script sets the recipient's email address, subject, and file that contains the email content, and then uses mailx to send the email.

You can also use the -F option to specify a file that contains the email content and format:

#!/bin/bash

# Set the recipient's email address
RECIPIENT="[email protected]"

# Set the subject of the email
SUBJECT="Test email from script"

# Set the file that contains the email content and format
FILE="email.txt"

# Use mailx to send the email
mailx -s "$SUBJECT" -F "$FILE" $RECIPIENT

In this example, the script sets the recipient's email address, subject, and file that contains the email content and format, and then uses mailx to send the email.

You can also use the -t option to specify the email content as a template:

#!/bin/bash

# Set the recipient's email address
RECIPIENT="[email protected]"

# Set the subject of the email
SUBJECT="Test email from script"

# Set the template for the email content
TEMPLATE="Hello %s, this is a test email sent from a Linux script."

# Use mailx to send the email
mailx -s "$SUBJECT" -t "$TEMPLATE" $RECIPIENT

In this example, the script sets the recipient's email address, subject, and template for the email content, and then uses mailx to send the email.

You can also use the -r option to specify the sender's email address:

#!/bin/bash

# Set the recipient's email address
RECIPIENT="[email protected]"

# Set the subject of the email
SUBJECT="Test email from script"

# Set the sender's email address
SENDER="[email protected]"

# Set the body of the email
BODY="This is a test email sent from a Linux script."

# Use mailx to send the email
mailx -s "$SUBJECT" -b "$BODY" -r "$SENDER" $RECIPIENT

In this example, the script sets the recipient's email address, subject, sender's email address, and body, and then uses mailx to send the email.

You can also use the -f option to specify the from address:

#!/bin/bash

# Set the recipient's email address
RECIPIENT="[email protected]"

# Set the subject of the email
SUBJECT="Test email from script"

# Set the from address
FROM="[email protected]"

# Set the body of the email
BODY="This is a test email sent from a Linux script."

# Use mailx to send the email
mailx -s "$SUBJECT" -b "$BODY" -f "$FROM" $RECIPIENT

In this example, the script sets the recipient's email address, subject, from address, and body, and then uses mailx to send the email.

You can also use the -m option to specify the message ID:

#!/bin/bash

# Set the recipient's email address
RECIPIENT="[email protected]"

# Set the subject of the email
SUBJECT="Test email from script"

# Set the message ID
MESSAGE_ID="1234567890"

# Set the body of the email
BODY="This is a test email sent from a Linux script."

# Use mailx to send the email
mailx -s "$SUBJECT" -b "$BODY" -m "$MESSAGE_ID" $RECIPIENT

In this example, the script sets the recipient's email address, subject, message ID, and body, and then uses mailx to send the email.

You can also use the -p option to specify the priority of the email:

#!/bin/bash

# Set the recipient's email address
RECIPIENT="[email protected]"

# Set the subject of the email
SUBJECT="Test email from script"

# Set the priority of the email
PRIORITY="high"

# Set the body of the email
BODY="This is a test email sent from a Linux script."

# Use mailx to send the email
mailx -s "$SUBJECT" -b "$BODY" -p "$PRIORITY" $RECIPIENT

In this example, the script sets the recipient's email address, subject, priority, and body, and then uses mailx to send the email.

You can also use the -q option to specify the queue name:


#!/bin/bash

# Set the recipient's email address
RECIPIENT="[email protected]"

# Set the subject of the email
SUBJECT="Test email from script"

# Set the queue name
QUEUE="myqueue"

# Set the body of the email
BODY="This is a test email sent from a Linux script."

# Use mailx to send the email
mailx -s "$SUBJECT" -b "$BODY" -q "$QUEUE" $RECIPIEN