使用SMTP从Python发送邮件


118

我正在使用以下方法使用SMTP从Python发送邮件。这是使用的正确方法,还是我想念的陷阱?

from smtplib import SMTP
import datetime

debuglevel = 0

smtp = SMTP()
smtp.set_debuglevel(debuglevel)
smtp.connect('YOUR.MAIL.SERVER', 26)
smtp.login('USERNAME@DOMAIN', 'PASSWORD')

from_addr = "John Doe <john@doe.net>"
to_addr = "foo@bar.com"

subj = "hello"
date = datetime.datetime.now().strftime( "%d/%m/%Y %H:%M" )

message_text = "Hello\nThis is a mail from your server\n\nBye\n"

msg = "From: %s\nTo: %s\nSubject: %s\nDate: %s\n\n%s" 
        % ( from_addr, to_addr, subj, date, message_text )

smtp.sendmail(from_addr, to_addr, msg)
smtp.quit()

2
确保您正确输入日期/时间。我发现下面的功能非常有用,这为您提供的日期,标题完全格式化值: docs.python.org/py3k/library/...
BastiBen

这是一个代码示例,该代码示例允许在主题和/或正文中发送包含Unicode文本的电子邮件
jfs 2013年

这是一个代码示例,演示如何内联发送图像(以及带有html和纯文本部分的电子邮件)。它还显示了如何在旧的Python版本上配置ssl参数。
jfs 2013年

2
请注意,有可用的包装器库,使发送电子邮件的代码更少(例如yagmail
PascalVKooten 2015年

Answers:


111

我使用的脚本非常相似。我将其张贴在此处,作为如何使用email。*模块生成MIME消息的示例。因此可以轻松修改此脚本以附加图片等。

我依靠我的ISP添加日期时间标头。

我的ISP要求我使用安全的smtp连接发送邮件,我依靠smtplib模块(可从http://www1.cs.columbia.edu/~db2501/ssmtplib.py下载)

就像在脚本中一样,用于在SMTP服务器上进行身份验证的用户名和密码(下面提供了伪值)在源中为纯文本格式。这是一个安全漏洞。但是最好的选择取决于您对这些保护有多认真(想要?)。

======================================

#! /usr/local/bin/python


SMTPserver = 'smtp.att.yahoo.com'
sender =     'me@my_email_domain.net'
destination = ['recipient@her_email_domain.com']

USERNAME = "USER_NAME_FOR_INTERNET_SERVICE_PROVIDER"
PASSWORD = "PASSWORD_INTERNET_SERVICE_PROVIDER"

# typical values for text_subtype are plain, html, xml
text_subtype = 'plain'


content="""\
Test message
"""

subject="Sent from Python"

import sys
import os
import re

from smtplib import SMTP_SSL as SMTP       # this invokes the secure SMTP protocol (port 465, uses SSL)
# from smtplib import SMTP                  # use this for standard SMTP protocol   (port 25, no encryption)

# old version
# from email.MIMEText import MIMEText
from email.mime.text import MIMEText

try:
    msg = MIMEText(content, text_subtype)
    msg['Subject']=       subject
    msg['From']   = sender # some SMTP servers will do this automatically, not all

    conn = SMTP(SMTPserver)
    conn.set_debuglevel(False)
    conn.login(USERNAME, PASSWORD)
    try:
        conn.sendmail(sender, destination, msg.as_string())
    finally:
        conn.quit()

except:
    sys.exit( "mail failed; %s" % "CUSTOM_ERROR" ) # give an error message

1
@Vincent:邮件失败;'模块'对象没有属性'SSLFakeSocket'-使用Gmail :(
RadiantHex 2010年

这听起来像是版本或导入问题,以帮助您查找到该问题:您正在运行哪个版本的Python?-是否需要通过SSL连接到SMTP服务器(如果是,则如上所述导入ssmtplib)吗?您可以直接从python Interactive导入smtplib吗?如果是,是否定义了smtplib.SSLFakeSocket类?希望我能帮忙
Vincent Marchetti 2010年

2
使用smtplib.SMTP_SSL(Python最新版本中的标准)来创建连接,而不是ssmtplib.STMP_SSL(上面暗示的第三方模块)。注意,标准模块以单个“ s”开头。那对我有用。
JulioGorgé10年

2
更换from ssmtplib import SMTP_SSL as SMTPfrom smtplib import SMTP_SSL as SMTP,而这个例子从标准Python库工作。
亚当·马坦

9
添加msg['To'] = ','.join(destination),否则将无法在gmail中查看目的地
Taha Jahangir 2012年

88

我通常使用的方法...没什么大的不同

import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText

msg = MIMEMultipart()
msg['From'] = 'me@gmail.com'
msg['To'] = 'you@gmail.com'
msg['Subject'] = 'simple email in python'
message = 'here is the email'
msg.attach(MIMEText(message))

mailserver = smtplib.SMTP('smtp.gmail.com',587)
# identify ourselves to smtp gmail client
mailserver.ehlo()
# secure our email with tls encryption
mailserver.starttls()
# re-identify ourselves as an encrypted connection
mailserver.ehlo()
mailserver.login('me@gmail.com', 'mypassword')

mailserver.sendmail('me@gmail.com','you@gmail.com',msg.as_string())

mailserver.quit()

而已


如果您使用两步验证,则必须先创建一个应用专用密码,然后用普通密码替换。请参阅使用应用程序密码登录
Suzana 2014年

2
我同意,这是最好的答案,应该接受。实际被接受的是次等的。
HelloWorld

6
对于python3,请使用:from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText
art

21

另外,如果您想使用TLS(而不是SSL)执行smtp身份验证,则只需更改端口(使用587)并执行smtp.starttls()。这对我有用:

...
smtp.connect('YOUR.MAIL.SERVER', 587)
smtp.ehlo()
smtp.starttls()
smtp.ehlo()
smtp.login('USERNAME@DOMAIN', 'PASSWORD')
...

6

我看到的主要问题是,您没有处理任何错误:.login()和.sendmail()都记录了可以抛出的异常,而且看来.connect()必须有某种方式表明它是无法连接-可能是底层套接字代码引发的异常。


6

确保您没有任何阻止SMTP的防火墙。我第一次尝试发送电子邮件时,它同时被Windows防火墙和McAfee阻止-花费了很多时间才能找到它们。


6

那这个呢?

import smtplib

SERVER = "localhost"

FROM = "sender@example.com"
TO = ["user@example.com"] # must be a list

SUBJECT = "Hello!"

TEXT = "This message was sent with Python's smtplib."

# Prepare actual message

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

%s
""" % (FROM, ", ".join(TO), SUBJECT, TEXT)

# Send the mail

server = smtplib.SMTP(SERVER)
server.sendmail(FROM, TO, message)
server.quit()

4

以下代码对我来说很好用:

import smtplib

to = 'mkyong2002@yahoo.com'
gmail_user = 'mkyong2002@gmail.com'
gmail_pwd = 'yourpassword'
smtpserver = smtplib.SMTP("smtp.gmail.com",587)
smtpserver.ehlo()
smtpserver.starttls()
smtpserver.ehlo() # extra characters to permit edit
smtpserver.login(gmail_user, gmail_pwd)
header = 'To:' + to + '\n' + 'From: ' + gmail_user + '\n' + 'Subject:testing \n'
print header
msg = header + '\n this is test msg from mkyong.com \n\n'
smtpserver.sendmail(gmail_user, to, msg)
print 'done!'
smtpserver.quit()

参考:http : //www.mkyong.com/python/how-do-send-email-in-python-via-smtplib/


1
Flask有一个用于电子邮件的框架:从flask.ext.mail导入Mail。我正在对其进行故障排除,并认为我将返回Python代码以查看是否可以工作。我喜欢这个答案,因为它没有骨头。哦,是的,而且有效!

注意:答案的先前版本包括以下行:smtpserver.close()它必须为:smtpserver.quit(),因为close()将无法正确终止TLS连接!close()将在期间调用quit()
aronadaal

嗨,我在执行上述命令时遇到麻烦。当我使用smtpserver.starttls()时,出现SMTP错误“ SMTPServerDisconnected:连接意外关闭:[Errno 10054]” ..在stackoverflow.com/questions/46094175/中
fazkan


3

我使用SMTP发送邮件的示例代码。

import smtplib, ssl

smtp_server = "smtp.gmail.com"
port = 587  # For starttls
sender_email = "sender@email"
receiver_email = "receiver@email"
password = "<your password here>"
message = """ Subject: Hi there

This message is sent from Python."""


# Create a secure SSL context
context = ssl.create_default_context()

# Try to log in to server and send email
server = smtplib.SMTP(smtp_server,port)

try:
    server.ehlo() # Can be omitted
    server.starttls(context=context) # Secure the connection
    server.ehlo() # Can be omitted
    server.login(sender_email, password)
    server.sendmail(sender_email, receiver_email, message)
except Exception as e:
    # Print any error messages to stdout
    print(e)
finally:
    server.quit()

2

查看所有这些宽大的答案?请允许我通过几行来自我推广。

导入并连接:

import yagmail
yag = yagmail.SMTP('john@doe.net', host = 'YOUR.MAIL.SERVER', port = 26)

然后,它只是一线:

yag.send('foo@bar.com', 'hello', 'Hello\nThis is a mail from your server\n\nBye\n')

当它超出范围时,它将实际上关闭(或可以手动关闭)。此外,它将允许您在密钥环中注册用户名,从而不必在脚本中写出密码(在写之前,这确实困扰了我yagmail!)

有关软件包/安装,提示和技巧,请查看gitpip,它们可用于Python 2和3。


@PascalvKoolen我安装了yagmail,并尝试通过提供我的电子邮件ID和密码进行连接。但是它给了我一个认证错误
fazkan

0

你可以那样做

import smtplib
from email.mime.text import MIMEText
from email.header import Header


server = smtplib.SMTP('mail.servername.com', 25)
server.ehlo()
server.starttls()

server.login('username', 'password')
from = 'me@servername.com'
to = 'mygfriend@servername.com'
body = 'That A Message For My Girl Friend For tell Him If We will go to eat Something This Nigth'
subject = 'Invite to A Diner'
msg = MIMEText(body,'plain','utf-8')
msg['Subject'] = Header(subject, 'utf-8')
msg['From'] = Header(from, 'utf-8')
msg['To'] = Header(to, 'utf-8')
message = msg.as_string()
server.sendmail(from, to, message)

0

这是Python 3.x的工作示例

#!/usr/bin/env python3

from email.message import EmailMessage
from getpass import getpass
from smtplib import SMTP_SSL
from sys import exit

smtp_server = 'smtp.gmail.com'
username = 'your_email_address@gmail.com'
password = getpass('Enter Gmail password: ')

sender = 'your_email_address@gmail.com'
destination = 'recipient_email_address@gmail.com'
subject = 'Sent from Python 3.x'
content = 'Hello! This was sent to you via Python 3.x!'

# Create a text/plain message
msg = EmailMessage()
msg.set_content(content)

msg['Subject'] = subject
msg['From'] = sender
msg['To'] = destination

try:
    s = SMTP_SSL(smtp_server)
    s.login(username, password)
    try:
        s.send_message(msg)
    finally:
        s.quit()

except Exception as E:
    exit('Mail failed: {}'.format(str(E)))

0

基于此示例,我做了以下功能:

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

def send_email(host, port, user, pwd, recipients, subject, body, html=None, from_=None):
    """ copied and adapted from
        /programming/10147455/how-to-send-an-email-with-gmail-as-provider-using-python#12424439
    returns None if all ok, but if problem then returns exception object
    """

    PORT_LIST = (25, 587, 465)

    FROM = from_ if from_ else user 
    TO = recipients if isinstance(recipients, (list, tuple)) else [recipients]
    SUBJECT = subject
    TEXT = body.encode("utf8") if isinstance(body, unicode) else body
    HTML = html.encode("utf8") if isinstance(html, unicode) else html

    if not html:
        # Prepare actual message
        message = """From: %s\nTo: %s\nSubject: %s\n\n%s
        """ % (FROM, ", ".join(TO), SUBJECT, TEXT)
    else:
                # /programming/882712/sending-html-email-using-python#882770
        msg = MIMEMultipart('alternative')
        msg['Subject'] = SUBJECT
        msg['From'] = FROM
        msg['To'] = ", ".join(TO)

        # Record the MIME types of both parts - text/plain and text/html.
        # utf-8 -> /programming/5910104/python-how-to-send-utf-8-e-mail#5910530
        part1 = MIMEText(TEXT, 'plain', "utf-8")
        part2 = MIMEText(HTML, 'html', "utf-8")

        # Attach parts into message container.
        # According to RFC 2046, the last part of a multipart message, in this case
        # the HTML message, is best and preferred.
        msg.attach(part1)
        msg.attach(part2)

        message = msg.as_string()


    try:
        if port not in PORT_LIST: 
            raise Exception("Port %s not one of %s" % (port, PORT_LIST))

        if port in (465,):
            server = smtplib.SMTP_SSL(host, port)
        else:
            server = smtplib.SMTP(host, port)

        # optional
        server.ehlo()

        if port in (587,): 
            server.starttls()

        server.login(user, pwd)
        server.sendmail(FROM, TO, message)
        server.close()
        # logger.info("SENT_EMAIL to %s: %s" % (recipients, subject))
    except Exception, ex:
        return ex

    return None

如果仅通过,body则将发送纯文本邮件,但是,如果将html论点与论点一起传递body,则将发送html电子邮件(对于不支持html / mime类型的电子邮件客户端,将回退到文本内容)。

用法示例:

ex = send_email(
      host        = 'smtp.gmail.com'
   #, port        = 465 # OK
    , port        = 587  #OK
    , user        = "xxx@gmail.com"
    , pwd         = "xxx"
    , from_       = 'xxx@gmail.com'
    , recipients  = ['yyy@gmail.com']
    , subject     = "Test from python"
    , body        = "Test from python - body"
    )
if ex: 
    print("Mail sending failed: %s" % ex)
else:
    print("OK - mail sent"

顺便说一句。如果要将gmail用作测试或生产SMTP服务器,请启用对安全性较低的应用程序的临时访问或永久访问:

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.