简体   繁体   English

无法从python发送电子邮件

[英]Unable to send email from python

I am using the following code to send email from python program in localhost, 我正在使用以下代码从localhost中的python程序发送电子邮件,

import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

me = "tonyr1291@gmail.com"
you = "testaccount@gmail.com"


msg = MIMEMultipart('alternative')
msg['Subject'] = "Link"
msg['From'] = me
msg['To'] = you

text = "Hi!\nHow are you?\nHere is the link you wanted:\nhttp://www.python.org"
html = """\
<html>
  <head></head>
   <body>
    <p>Hi!<br>
       How are you?<br>
       Here is the <a href="http://www.python.org">link</a> you wanted.
    </p>
  </body>
</html>
"""

part1 = MIMEText(text, 'plain')
part2 = MIMEText(html, 'html')

msg.attach(part1)
msg.attach(part2)

s = smtplib.SMTP('localhost',5000)
s.sendmail(me, you, msg.as_string())
s.quit()

This code is from python documentation. 此代码来自python文档。

When I run this code, it is running continuously but no email is sent. 当我运行此代码时,它正在连续运行,但是没有发送电子邮件。

I would like to know, do I have to make some other configurations anywhere else other than this code. 我想知道,除了此代码外,是否还需要进行其他配置。

I am not seeing any error. 我没有看到任何错误。

I am using python 2.7 我正在使用python 2.7

This is given as a solution in Sending HTML email using Python 这是使用Python发送HTML电子邮件中的解决方案

It seems that you're using a gmail id. 看来您使用的是Gmail ID。 Now, the SMTP server is not your tornado server. 现在,SMTP服务器不是您的龙卷风服务器。 it is the server of the email provider. 它是电子邮件提供商的服务器。

You can search online for the smtp settings for the gmail server and get the following: 您可以在线搜索gmail服务器的smtp设置,并获得以下信息:

  • Server name : smtp.gmail.com 伺服器名称:smtp.gmail.com
  • Server port for SSL : 465 SSL的服务器端口:465
  • Server port for TLS : 587 TLS的服务器端口:587

I've gotten them from http://email.about.com/od/accessinggmail/f/Gmail_SMTP_Settings.htm 我已经从http://email.about.com/od/accessinggmail/f/Gmail_SMTP_Settings.htm获得了它们

Also, you need to ensure you do not enable gmail's 2 step authentication when doing this, otherwise it will fail. 另外,您需要确保在执行此操作时不要启用gmail的两步验证,否则它将失败。 Also, gmail specifically may require you to send other things like ehlo and starttls. 另外,gmail可能特别要求您发送其他内容,例如ehlo和starttls。 You can find a previous answer with a complete example here : How to send an email with Gmail as provider using Python? 您可以在此处找到带有完整示例的先前答案: 如何使用Python作为提供商使用Gmail发送电子邮件?

    import smtplib

    gmail_user = user
    gmail_pwd = pwd
    FROM = user
    TO = recipient if type(recipient) is list else [recipient]
    SUBJECT = subject
    TEXT = body

    # Prepare actual message
    message = """\From: %s\nTo: %s\nSubject: %s\n\n%s
    """ % (FROM, ", ".join(TO), SUBJECT, TEXT)
    try:
        server = smtplib.SMTP("smtp.gmail.com", 587)
        server.ehlo()
        server.starttls()
        server.login(gmail_user, gmail_pwd)
        server.sendmail(FROM, TO, message)
        server.close()
        print 'successfully sent the mail'
    except:
        print "failed to send mail"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM