3

次の内容はファイルにあります。シェルスクリプトを使用してPythonスクリプトを呼び出します。このPythonスクリプトはメールを送信します。しかし、メールの内容では、以下に示すように出力が表示されます。

 /usr/bin/python $DIR/sm.py "$message"  "`cat /tmp/alert.txt`"

入力: 以下は、alert.txt の内容です。

  Thu Jun 28 14:29:26 IST 2012

  Disk usage limit exceeded -Current disk usage is 167G-Configured disk usage is 200HTTPD connections exceeded configured usage limit -Current HTTPD connections is 21-Configured HTTPD connection is 20


  ========================OTHER INFO==================
  Total fds: 8
  Socket fds: 0
  Other fds: 8
  Free memory :Free Memory:183732
  Buffered memory Buffered Memory:78224
  Cache memory : Cache Memory:579040
  Disk usage is 167G
  DB connections 1
  Network connections 21
  CPU Usage: 0.0

出力:

  Thu Jun 28 14:29:26 IST 2012 Disk usage limit exceeded -Current disk usage is 167G-Configured disk usage is 200HTTPD connections exceeded configured usage limit -Current HTTPD connections is 21-Configured HTTPD connection is 20 ========================OTHER INFO================== Total fds: 8 Socket fds: 0 Other fds: 8 Free memory :Free Memory:183732 Buffered memory Buffered Memory:78224 Cache memory : Cache Memory:579040 Disk usage is 167G DB connections 1 Network connections 21 CPU Usage: 0.0

これはsm.pyです

import logging
import smtplib
import sys
from email.MIMEText import MIMEText
from email.MIMEMultipart import MIMEMultipart


      try:
         smaid = qs[0].id
         gmailUser = 'no-reply@xxxxxxxxxxx.com'
         gmailPassword = '12345'
         mailServer = smtplib.SMTP('smtp.gmail.com', 587)
         mailServer.ehlo()
         mailServer.starttls()
         mailServer.ehlo()
         mailServer.login(gmailUser, gmailPassword)

         to_addr = "xxxxx@xx.com"
         subject = sys.argv[1]
         body = sys.argv[2]
         try:
             msg = MIMEMultipart()
             msg['From'] = gmailUser
             msg['To'] = to_addr
             msg["Content-type"] = "text/html"
             sub = subject
             msg['Subject'] = sub
             body = body

             msg.attach(MIMEText(body, 'html'))
             mailServer.sendmail(gmailUser, to_addr, msg.as_string())
         except:
             write_exception("send exception")

         mailServer.close()
     except:
          write_exception("send exception1")
4

4 に答える 4

2

HTML をどうしても送信したい場合は、HTML の改行が必要です。

import cgi
# escape special HTML characters
body = cgi.escape(body)
# use HTML line breaks
body = body.replace("\r\n", "\n").replace("\n", "<br />\n")

ただし、おそらく Multipart も HTML も必要ないため、次を使用できますsmtplib.SMTP.sendmail

headers = (('From', gmailUser), 
           ('To', to_addr), 
           ('Subject', subject))

# normalize newlines to CR+LF, as required by SMTP
body = body.replace("\r\n", "\n").replace("\n", "\r\n")

msg = '\r\n'.join("%s: %s" % kv for kv in headers) + '\r\n'*2 + body

mailServer.sendmail(gmailUser, [to_addr], msg)

また、コマンド ラインは長さに制限があるため、コマンド ラインからファイルの内容を指定しないでください。代わりに、のようにSTDIN経由でファイルを提供し、経由python ... < /tmp/alert.txtで読み取る必要があります

import sys
body = sys.stdin.read()
于 2012-06-28T10:37:01.347 に答える
2

text/plainの代わりにとして送信してくださいtext/html。そこには HTML がありません。テキストを正しく表示するのはクライアント次第であるため、これで修正されるはずです。HTML では改行は同じ効果を持ちません。

于 2012-06-28T10:39:13.437 に答える
0

どのシェルを使用していますか?

"`cat xxx`" 

csh では、すべての改行が削除され、スペースで区切られた文字列が返されます

> cat >testfile
line 1
line 2
<ctrl-d>
> echo "`cat testfile`"
line 1 line 2
于 2012-06-28T10:44:29.990 に答える
0

メールのコンテンツ タイプを「text/html」に設定すると、ユーザーのメール エージェントは明らかに本文を解釈して html としてレンダリングします。プレーン テキスト形式を維持したい場合は、コンテンツ タイプを text/html に設定しないでください。

于 2012-06-28T10:42:39.927 に答える