user6438693
user6438693

Reputation:

How to Add Date Dynamically in Email Subject

I need to add date dynamically in subject email, so that when ever the job run it picks the date automatically.

cat <<'EOF' - daily_status_email.html | /usr/lib/sendmail -t 
Content-type: text/html 
Subject : Daily Job Status : "Present date and time" 
From : [email protected]
To : [email protected] 
EOF

Can anyone guide me on this?

Upvotes: 1

Views: 3248

Answers (2)

Matias Barrios
Matias Barrios

Reputation: 5056

As I can see more people already answered your question so Im sharing with all this function I just did and that I recommend putting in your .bash_profile for easy access. This function solves the headache of having an html mail with attachments. There is a commented example of how to use it in the code. For your use case you might use it as :

  SENDMAIL "THE.SENDER" "[email protected]" "$(date) - This is a subject" "<html>This is <b>bold</b></html>" "test.txt"

For this to work you need to have unix2dos installed on your machine.

        function SENDMAIL () {
        #this is an example
        # SENDMAIL "THE.SENDER" "[email protected]" "This is a subject" "<html>This is <b>bold</b></html>" "test.txt"
                function get_mimetype(){
                file --mime-type "$1" | sed 's/.*: //'
                }
                rm -f -r /tmp/mail_smd.html

                from="$1"
                to="$2"
                subject="$3"
                body="$4"

                boundary="=== Boundary ==="

                if [ -z "$from" ] || [ -z "$to" ] || [ -z "$subject" ] || [ -z "$body" ]
                then 
                    echo "ERROR!!! ";
                    echo "All parameters are mandatory (except for the attachments).";
                    echo "First parameter : FROM";
                    echo "Second parameter : TO";
                    echo "Third parameter : SUBJECT";
                    echo "Fourth parameter : BODY";
                    echo "Fifth parameter: FILES TO ATTACH (This is optional)"
                    echo "Example : SENDMAIL \"IAMTHESENDER\" \"[email protected]\" \"This is a subject\" \"<html>This is <b>bold</b></html>\"  \"./smd_files/input_prod_tte.txt\" ";
                    return 1;
                fi

                attached="$5"
                declare -a attachments
                attachments=($attached )

                echo "From : $from"
                echo "To : $to"
                echo "Subject : $subject"
                echo "Body : $body"
                echo "Attachments : $attached"
                # Build headers


                echo "From:$from" > /tmp/mail_smd.html
                echo "To:$to"  >> /tmp/mail_smd.html
                echo "Subject: $subject" >> /tmp/mail_smd.html
                echo "Mime-Version: 1.0" >> /tmp/mail_smd.html
                echo "Content-Type: multipart/mixed;boundary=\"$boundary\"" >> /tmp/mail_smd.html
                echo "--${boundary}" >> /tmp/mail_smd.html
                echo "Content-Type: text/html;charset=iso-8859-1"$'\r'$'\n' >> /tmp/mail_smd.html

                echo "$body"$'\r'$'\n' >> /tmp/mail_smd.html


                for file in "${attachments[@]}"; do

                      [ ! -f "$file" ] && echo "Attachment $file not found, omitting file" >&2 && continue

                        mimetype=$(get_mimetype "$file")

                    echo "--${boundary}"  >> /tmp/mail_smd.html
                    echo "Content-Type: application/octet-stream; name=\"$(basename $file)\""   >> /tmp/mail_smd.html
                    echo "Content-Transfer-Encoding: base64 "   >> /tmp/mail_smd.html
                    echo "Content-Disposition: attachment; filename=\"$(basename $file)\" "$'\r'$'\n'   >> /tmp/mail_smd.html
                    unix2dos $file
                    echo "$(/usr/bin/base64 $file)" >> /tmp/mail_smd.html
                done

                # print last boundary with closing --
                echo "--${boundary}--" >> /tmp/mail_smd.html

                /usr/sbin/sendmail -t -oi < /tmp/mail_smd.html
        }

Thanks!

Upvotes: 0

Vadim Beskrovnov
Vadim Beskrovnov

Reputation: 961

Remove ' from EOF and try date:

cat <<EOF - daily_status_email.html | /usr/lib/sendmail -t  
Content-type: text/html 
Subject : Daily Job Status : $(date) 
From : [email protected]
To : [email protected] 
EOF

Upvotes: 3

Related Questions