Python:使用smtplib模块发送电子邮件时未显示“主题”


70

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

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”以在发送的电子邮件中也包含主题。

如果使用server.sendmail(FROM,TO,消息,SUBJECT),则会显示有关“ smtplib.SMTPSenderRefused”的错误

Answers:


155

将其作为标题附加:

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

接着:

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

还可以考虑使用标准的Python模块email-在撰写电子邮件时会大有帮助。


17

尝试这个:

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())

7
邮件的正文呢?那去哪儿了?
Dss 2015年

3
我最终按照以下说明放入正文docs.python.org/2/library/email-examples.html#id5
Nico

在当前的python版本中,此功能现在由email.message.MailMessage管理。
chiffa

13

使用新的“ 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()

这是Python 3.6+的最佳答案。谢谢!
prrao

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())

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


我收到以下错误:从email.mime.text导入MIMEText作为文本ImportError:没有名为mime.text的模块
nsh

@nsh-使用什么版本的Python?我在此特定安装上使用2.6.6。完全有可能在3.x中它的位置稍有不同。
gddc 2011年

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


一种观察:例如,从,到和主题字段必须位于变量“ message”的开头,否则,将不会解释该字段,因为它必须是预期的。我有只插入“主题”字段的经验,而不是在变量的开头,并且消息没有主题就到达了收件人的邮箱。干杯。
ivanleoncz

4

请参阅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.

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

看来smtplib不直接支持主题添加,并且希望味精已经用主题进行了格式化,等等。这就是该email模块的所在。


1
 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()
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.