我试图使用python发送电子邮件(Gmail),但我得到以下错误。

Traceback (most recent call last):  
File "emailSend.py", line 14, in <module>  
server.login(username,password)  
File "/usr/lib/python2.5/smtplib.py", line 554, in login  
raise SMTPException("SMTP AUTH extension not supported by server.")  
smtplib.SMTPException: SMTP AUTH extension not supported by server.

Python脚本如下所示。

import smtplib

fromaddr = 'user_me@gmail.com'
toaddrs  = 'user_you@gmail.com'
msg = 'Why,Oh why!'
username = 'user_me@gmail.com'
password = 'pwd'
server = smtplib.SMTP('smtp.gmail.com:587')
server.starttls()
server.login(username,password)
server.sendmail(fromaddr, toaddrs, msg)
server.quit()

当前回答

不直接相关,但仍然值得指出的是,我的包试图使发送gmail消息非常快速和轻松。它还试图维护一个错误列表,并试图立即指出解决方案。

它只需要下面的代码来完成你所写的:

import yagmail
yag = yagmail.SMTP('user_me@gmail.com')
yag.send('user_you@gmail.com', 'Why,Oh why!')

或者一句话:

yagmail.SMTP('user_me@gmail.com').send('user_you@gmail.com', 'Why,Oh why!')

关于包/安装,请查看git或pip,可用于Python 2和3。

其他回答

import smtplib
server = smtplib.SMTP('smtp.gmail.com', 587)
server.ehlo()
server.starttls()
server.login("fromaddress", "password")
msg = "HI!"
server.sendmail("fromaddress", "receiveraddress", msg)
server.quit()

意识到用Python发送电子邮件有多痛苦,所以我为它做了一个广泛的库。它也有Gmail预配置(所以你不需要记住Gmail的主机和端口):

from redmail import gmail
gmail.user_name = "you@gmail.com"
gmail.password = "<YOUR APPLICATION PASSWORD>"

# Send an email
gmail.send(
    subject="An example email",
    receivers=["recipient@example.com"],
    text="Hi, this is text body.",
    html="<h1>Hi, this is HTML body.</h1>"
)

当然你需要配置你的Gmail账号(别担心,很简单):

设置两步验证(如果尚未设置) 创建应用程序密码 将应用程序密码设置为gmail对象,完成!

红色邮件实际上是相当广泛的(包括附件,嵌入图像,发送与抄送和密件,模板与Jinja等),应该是所有你需要从电子邮件发件人。它也经过了良好的测试和文档记录。我希望它对你有用。

如何安装:

pip install redmail

文档:https://red-mail.readthedocs.io/en/latest/

源代码:https://github.com/Miksus/red-mail

注意,Gmail不允许更改发送方。发件人地址永远是你。

现在有一个gmail API,它可以让你通过REST发送电子邮件,阅读电子邮件和创建草稿。 与SMTP调用不同,它是非阻塞的,这对于在请求线程中发送电子邮件的基于线程的web服务器(如python web服务器)来说是一件好事。这个API也非常强大。

当然,电子邮件应该交给一个非web服务器队列,但有选择是很好的。

如果您在域上拥有谷歌应用程序管理员权限,则最容易设置,因为这样您就可以向您的客户端授予全面权限。否则,您必须摆弄OAuth身份验证和权限。

下面是一个要点:

https://gist.github.com/timrichardson/1154e29174926e462b7a

这是

创建Gmail APP密码!

创建之后,创建一个名为sendgmail.py的文件

然后添加以下代码:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# =============================================================================
# Created By  : Jeromie Kirchoff
# Created Date: Mon Aug 02 17:46:00 PDT 2018
# =============================================================================
# Imports
# =============================================================================
import smtplib

# =============================================================================
# SET EMAIL LOGIN REQUIREMENTS
# =============================================================================
gmail_user = 'THEFROM@gmail.com'
gmail_app_password = 'YOUR-GOOGLE-APPLICATION-PASSWORD!!!!'

# =============================================================================
# SET THE INFO ABOUT THE SAID EMAIL
# =============================================================================
sent_from = gmail_user
sent_to = ['THE-TO@gmail.com', 'THE-TO@gmail.com']
sent_subject = "Hey Friends!"
sent_body = ("Hey, what's up? friend!\n\n"
             "I hope you have been well!\n"
             "\n"
             "Cheers,\n"
             "Jay\n")

email_text = """\
From: %s
To: %s
Subject: %s

%s
""" % (sent_from, ", ".join(sent_to), sent_subject, sent_body)

# =============================================================================
# SEND EMAIL OR DIE TRYING!!!
# Details: http://www.samlogic.net/articles/smtp-commands-reference.htm
# =============================================================================

try:
    server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
    server.ehlo()
    server.login(gmail_user, gmail_app_password)
    server.sendmail(sent_from, sent_to, email_text)
    server.close()

    print('Email sent!')
except Exception as exception:
    print("Error: %s!\n\n" % exception)

所以,如果你成功了,你会看到这样的图像:

我通过给自己和自己发送电子邮件进行测试。

注意:我在我的帐户上启用了两步验证。应用程序密码与此工作!(对于gmail SMTP设置,您必须访问https://support.google.com/accounts/answer/185833?hl=en并遵循以下步骤) 此设置对于启用了两步验证的帐户不可用。这样的帐户需要特定于应用程序的密码来访问不太安全的应用程序。

澄清

导航到https://myaccount.google.com/apppasswords,并创建一个APP密码如上所述。

你同意OOP吗?

#!/usr/bin/env python


import smtplib

class Gmail(object):
    def __init__(self, email, password):
        self.email = email
        self.password = password
        self.server = 'smtp.gmail.com'
        self.port = 587
        session = smtplib.SMTP(self.server, self.port)        
        session.ehlo()
        session.starttls()
        session.ehlo
        session.login(self.email, self.password)
        self.session = session

    def send_message(self, subject, body):
        ''' This must be removed '''
        headers = [
            "From: " + self.email,
            "Subject: " + subject,
            "To: " + self.email,
            "MIME-Version: 1.0",
           "Content-Type: text/html"]
        headers = "\r\n".join(headers)
        self.session.sendmail(
            self.email,
            self.email,
            headers + "\r\n\r\n" + body)


gm = Gmail('Your Email', 'Password')

gm.send_message('Subject', 'Message')