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

时间:2011-08-29 15:14:45

标签: python smtplib

我已成功使用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”以在发送的电子邮件中包含SUBJECT。

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

8 个答案:

答案 0 :(得分:110)

将其作为标题附加:

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

然后:

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

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

答案 1 :(得分:10)

试试这个:

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

答案 2 :(得分:6)

你应该修改你的代码:

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

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

答案 3 :(得分: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

答案 4 :(得分:3)

这将与Gmail和Python 3.6及更高版本一起使用新的“ EmailMessage”对象:

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

答案 5 :(得分:2)

请参阅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文档示例部分的链接,它确实显示了创建带主题行的消息。 http://docs.python.org/library/email-examples.html#email-examples

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

答案 6 :(得分:0)

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

答案 7 :(得分:0)

对于 sendmail 方法,我使用以下内容(在 3.9 中)。在 msg 中包含主题,然后添加两行:

email_connection.sendmail(
    from_addr=my_email,
    to_addrs=to_email,
    msg="Subject:Working subject\n\nTest 2 from info email address with subject."
)