2017-08-03 21 views
0

件名の電子メールに日付を動的に追加する必要があります。ジョブを実行するときに自動的に日付が選択されるようにします。電子メールに日付を動的に追加する方法

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 

誰もがこの上で私を導くことはできますか?

+0

何か試しましたか? 'date'を見てください – fzd

+0

私はSubject(" $ Y ")に変数を入れようとしましたが、同じテキストとして件名に入っています –

+0

*' date +%d /%m /%Y' * –

答えて

1

EOFから'を削除し、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 
+0

ご協力いただきありがとうございます。出来た –

1

私はイムは、私はやったこのすべての機能を共有するように、より多くの人々が既にあなたの質問に答えて、私は簡単のためにあなたの.bash_profileに入れてお勧めしますことを見ることができるようにアクセス。この関数は、添付ファイル付きのhtmlメールを持つという頭痛を解決します。コードでそれを使用する方法のコメント付きの例があります。あなたのマシンにインストールunix2dosといったを持っている必要があります動作するように、このために

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

:あなたのユースケースのためには、としてそれを使用する場合があります。

 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 
     } 

ありがとうございます!

関連する問題