2017-12-30 32 views
0

私は電子メールを送信するためにPythonライブラリを使っています。私が送るメッセージに時間を置いた場合を除いて、すべてが機能します。 は、7:30 pmです。Python SMTPライブラリ空白の電子メール

電子メールメッセージに時間を入れると、電子メール受信者は空白の電子メールを受信します。私が時間を取れば、電子メールはうまくいく。私は時間のあるものが電子メールをねじ込むと思っています。コード内で何を変更する必要があるので、メール本文に時間を置くことができます。ありがとう!

from smtplib import SMTP 
def sendEmail(message,address): 
    debuglevel = 0 

    smtp = SMTP() 
    smtp.set_debuglevel(debuglevel) 
    #connect to the email server 
    smtp.connect('smtp.gmail.com', 587) 
    smtp.starttls() 
    smtp.login('[email protected]', 'ourpassword') 
    #set the email to be sent from 
    from_addr = "Auto Notification <sender email>" 
    #send the email from our address and the message and recepients provided in the function definition. 
    smtp.sendmail(from_addr, address, message) 
    smtp.quit() 

finalStr = "The Girls Varsity Basketball has been changed to 02/06/2018 at 7:30 pm * - (previously 6:15 pm ) at Clear Lake High School" 

sendEmail(finalStr, '[email protected]') 
+0

あなたは、電子メールの生のソースで見たことがありますか?それも空白ですか?私はなぜ電子メールが時間のために空白である何らかの理由を考えることができない。 – Barmar

+0

唯一の違いは、行が低いことですが、それ以外は同じに見えます。 @Barmar – LewisCallaway

+0

これは良いメールや悪いメールの行が下がっていますか?メッセージのヘッダーと本文の間に空白行があるはずですが、それは空白の行にはありませんか? – Barmar

答えて

0

一部の文字は、SMTPサーバーへの送信中に制御文字として誤って解釈されています。文字列をMIMETextとしてエンコードしてみてください。

from email.mime.text import MIMEText 

#send the email from our address and the message and recepients provided in the function definition. 
encoded_message = MIMEText(message).as_string() 
smtp.sendmail(from_addr, address, encoded_message) 

ここでは、結果がどのように異なるかを示します。エンコーディングは実際の例で宣言されていることに注意してください。後

send: b'The Girls Varsity Basketball has been changed to 02/06/2018 at 7:30 pm * - (previously 6:15 pm ) at Clear Lake High School\r\n.\r\n' 

send: b'Content-Type: text/plain; charset="us-ascii"\r\nMIME-Version: 1.0\r\nContent-Transfer-Encoding: 7bit\r\n\r\nThe Girls Varsity Basketball has been changed to 02/06/2018 at 7:30 pm * - (previously 6:15 pm ) at Clear Lake High School\r\n.\r\n' 
関連する問題