繁体   English   中英

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

[英]Python: "subject" not shown when sending email using smtplib module

我可以使用 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”的错误

将其附加为标题:

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

接着:

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

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

试试这个:

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

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

您可能应该将代码修改为如下所示:

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

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

请参阅 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 不直接支持主题添加,并希望 msg 已经使用主题等进行格式化。这就是email模块的用武之地。

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

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

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

对于 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."
)

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

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

试试这个:

from = "myemail@site.com"
to= "someemail@site.com"

subject = "Hello there!"
body = "Have a good day."

message = "Subject:" + subject + "\n" + body


server.sendmail(from, , message)
server.quit()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM