7

我已使用以下代码发送一封电子邮件,正如其中一篇关于类似主题的帖子所建议的那样。但是邮件还没有发出。有什么建议么?

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 回答