How to display date in mail body using unix script
To display the current date in the body of an email using a Unix script, you can use the date
command to get the current date and then use a text editor or a command-line email client to compose and send the email.
Here are a few examples:
Using date
and mail
command:
#!/bin/bash
# Get the current date
CURRENT_DATE=$(date +"%Y-%m-%d")
# Compose the email body
EMAIL_BODY="Hello, this is a test email sent on $(echo $CURRENT_DATE)."
# Send the email using the mail command
echo "$EMAIL_BODY" | mail -s "Test Email" [email protected]
In this example, the date
command is used to get the current date in the format YYYY-MM-DD
. The echo
command is used to compose the email body, which includes the current date. The mail
command is used to send the email to the specified recipient.
Using date
and mutt
command:
#!/bin/bash
# Get the current date
CURRENT_DATE=$(date +"%Y-%m-%d")
# Compose the email body
EMAIL_BODY="Hello, this is a test email sent on $(echo $CURRENT_DATE)."
# Send the email using mutt
mutt -s "Test Email" [email protected] << EOF
$EMAIL_BODY
EOF
In this example, the date
command is used to get the current date in the format YYYY-MM-DD
. The echo
command is used to compose the email body, which includes the current date. The mutt
command is used to send the email to the specified recipient.
Using date
and sendmail
command:
#!/bin/bash
# Get the current date
CURRENT_DATE=$(date +"%Y-%m-%d")
# Compose the email body
EMAIL_BODY="Hello, this is a test email sent on $(echo $CURRENT_DATE)."
# Send the email using sendmail
echo "To: [email protected]
Subject: Test Email
$EMAIL_BODY" | sendmail -t
In this example, the date
command is used to get the current date in the format YYYY-MM-DD
. The echo
command is used to compose the email body, which includes the current date. The sendmail
command is used to send the email to the specified recipient.
Note: You may need to modify the email body and the recipient's email address to suit your needs. Additionally, you may need to configure your email client or server to allow sending emails using the mail
, mutt
, or sendmail
commands.