7

同様のトピックに関する投稿の1つで提案されているように、次のコードを使用して電子メールを送信しました。しかし、メールは送信されていません。助言がありますか?

import subprocess
recipient = 'xxxxx@gmail.com'
subject = 'test'
body = 'testing mail through python'
def send_message(recipient, subject, body):
    process = subprocess.Popen(['mail', '-s', subject, recipient],
                               stdin=subprocess.PIPE)
    process.communicate(body)

print("sent the email")
4

1 に答える 1

14

関数が呼び出されない可能性があります。次のコードを試してください:

import subprocess

recipient = 'xxxxx@gmail.com'
subject = 'test'
body = 'testing mail through python'

def send_message(recipient, subject, body):
    try:
      process = subprocess.Popen(['mail', '-s', subject, recipient],
                               stdin=subprocess.PIPE)
    except Exception, error:
      print error
    process.communicate(body)

send_message(recipient, subject, body)

print("sent the email")

動作する可能性があります。幸運を。

于 2015-01-10T08:14:45.420 に答える