如何在电子邮件主题中动态添加日期

时间:2017-08-03 06:41:00

标签: bash shell unix

我需要在主题电子邮件中动态添加日期,以便在作业运行时自动选择日期。

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

有人可以指导我吗?

2 个答案:

答案 0 :(得分:2)

'移除EOF并尝试date

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

答案 1 :(得分:1)

我可以看到更多人已经回答了你的问题所以我正在分享我刚才所做的所有这些功能,我建议你放入.bash_profile以方便访问。这个功能解决了带有附件的html邮件的麻烦。有一个注释示例,说明如何在代码中使用它。对于您的用例,您可以将其用作:

  SENDMAIL "THE.SENDER" "recipient@example.com" "$(date) - This is a subject" "<html>This is <b>bold</b></html>" "test.txt"

为此,您需要在计算机上安装unix2dos。

        function SENDMAIL () {
        #this is an example
        # SENDMAIL "THE.SENDER" "recipient@example.com" "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\" \"matias.barrios@target.com\" \"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
        }

谢谢!