96

我可以使用 smtplib 模块成功发送电子邮件。但是发送电子邮件时,它不包括发送电子邮件中的主题。

import smtplib

SERVER = <localhost>

FROM = <from-address>
TO = [<to-addres>]

SUBJECT = "Hello!"

message = "Test"

TEXT = "This message was sent with Python's smtplib."
server = smtplib.SMTP(SERVER)
server.sendmail(FROM, TO, message)
server.quit()

我应该如何编写“server.sendmail”以在发送的电子邮件中包含 SUBJECT。

如果我使用 server.sendmail(FROM, TO, message, SUBJECT),它会给出关于“smtplib.SMTPSenderRefused”的错误

4

8 回答 8

183

将其作为标题附加:

message = 'Subject: {}\n\n{}'.format(SUBJECT, TEXT)

进而:

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

还可以考虑使用标准的 Python 模块email——它会在撰写电子邮件时为您提供很多帮助。

于 2011-08-29T15:23:19.087 回答
28

这将适用于使用新的“EmailMessage”对象的 Gmail 和 Python 3.6+:

import smtplib
from email.message import EmailMessage

msg = EmailMessage()
msg.set_content('This is my message')

msg['Subject'] = 'Subject'
msg['From'] = "me@gmail.com"
msg['To'] = "you@gmail.com"

# Send the message via our own SMTP server.
server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
server.login("me@gmail.com", "password")
server.send_message(msg)
server.quit()
于 2019-08-07T20:11:07.383 回答
18

试试这个:

import smtplib
from email.mime.multipart import MIMEMultipart
msg = MIMEMultipart()
msg['From'] = 'sender_address'
msg['To'] = 'reciver_address'
msg['Subject'] = 'your_subject'
server = smtplib.SMTP('localhost')
server.sendmail('from_addr','to_addr',msg.as_string())
于 2014-10-16T21:53:35.010 回答
7

您可能应该将代码修改为以下内容:

from smtplib import SMTP as smtp
from email.mime.text import MIMEText as text

s = smtp(server)

s.login(<mail-user>, <mail-pass>)

m = text(message)

m['Subject'] = 'Hello!'
m['From'] = <from-address>
m['To'] = <to-address>

s.sendmail(<from-address>, <to-address>, m.as_string())

显然,<>变量需要是实际的字符串值,或者是有效的变量,我只是将它们作为占位符填充。在发送带有主题的消息时,这对我有用。

于 2011-08-29T15:23:31.780 回答
5

请参阅 smtplib 文档底部的注释:

In general, you will want to use the email package’s features to construct an email message, which you can then convert to a string and send via sendmail(); see email: Examples.

这是 's 文档的示例部分的链接email,它确实显示了带有主题行的消息的创建。https://docs.python.org/3/library/email.examples.html

似乎 smtplib 不直接支持主题添加,并希望 msg 已经用主题等格式化。这就是email模块的用武之地。

于 2011-08-29T15:23:02.267 回答
4

我认为您必须将其包含在消息中:

import smtplib

message = """From: From Person <from@fromdomain.com>
To: To Person <to@todomain.com>
MIME-Version: 1.0
Content-type: text/html
Subject: SMTP HTML e-mail test

This is an e-mail message to be sent in HTML format

<b>This is HTML message.</b>
<h1>This is headline.</h1>
"""

try:
   smtpObj = smtplib.SMTP('localhost')
   smtpObj.sendmail(sender, receivers, message)         
   print "Successfully sent email"
except SMTPException:
   print "Error: unable to send email"

代码来自:http ://www.tutorialspoint.com/python/python_sending_email.htm

于 2011-08-29T15:25:20.020 回答
3
import smtplib
 
# creates SMTP session 

List item

s = smtplib.SMTP('smtp.gmail.com', 587)
 
# start TLS for security   
s.starttls()
 
# Authentication  
s.login("login mail ID", "password")
 
 
# message to be sent   
SUBJECT = "Subject"   
TEXT = "Message body"
 
message = 'Subject: {}\n\n{}'.format(SUBJECT, TEXT)
 
# sending the mail    
s.sendmail("from", "to", message)
 
# terminating the session    
s.quit()
于 2019-06-16T14:39:40.323 回答
0

如果将它包装在一个函数中,这应该作为一个模板。

def send_email(login, password, destinations, subject, message):
    server = smtplib.SMTP_SSL("smtp.gmail.com", 465)

    server.login(login, password)
    message = 'Subject: {}\n\n{}'.format(subject, message)

    for destination in destinations:
        print("Sending email to:", destination)
        server.sendmail(login, destinations, message)

    server.quit()
于 2021-12-15T19:32:41.017 回答