【问题标题】:sending mail from gmail account - Python从 gmail 帐户发送邮件 - Python
【发布时间】:2013-11-08 13:16:52
【问题描述】:

有人可以告诉我,下面的代码有什么问题吗? TIA :)))

import smtplib
if raw_input("if you want to send a message from a gmail account, type yes: ") == 'yes':
    try:      
        sender = raw_input("from:\n")
        senders_pwd = raw_input("password:\n")
        recipient = raw_input("to:\n")
        print 'ok, now compile your message:'
        subject = raw_input("subject:\n")
        body = raw_input("your message:\n")
        message = "subject: %s\n%s" %(subject,body)

        server = smtplib.SMTP("smtp.gmail.com",587)
        server.ehlo()
        server.starttls()
        server.ehlo()
        print "ok, I've sent your email"
    except:
        print 'failed to send'

【问题讨论】:

    标签: python email gmail


    【解决方案1】:

    您需要调用 sendmail() 函数。在最后一个 server.ehlo() 之后添加类似这三行的内容:

    server.login(sender, senders_pwd)
    server.sendmail(sender, recipient, message)
    server.close()
    

    【讨论】:

      猜你喜欢
      • 2021-10-22
      • 1970-01-01
      • 2013-03-24
      • 2016-03-15
      • 2021-11-10
      • 1970-01-01
      • 2011-12-15
      • 2012-03-30
      • 2022-11-08
      相关资源
      最近更新 更多