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

31moq8wy  于 2023-01-04  发布在  Python
关注(0)|答案(9)|浏览(187)

我能够成功地使用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()

我应该如何写“服务器。sendmail”包括主题以及在发送的电子邮件。
如果我使用server.sendmail(发件人,收件人,邮件,主题),它会给出关于“smtplib.SMTPSenderRefused”的错误

svdrlsy4

svdrlsy41#

将其附加为标题:

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

然后:

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

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

from email.message import EmailMessage

msg = EmailMessage()
msg['Subject'] = SUBJECT
msg['From'] = FROM
msg['To'] = TO
msg.set_content(TEXT)

server.send_message(msg)
q7solyqu

q7solyqu2#

这将在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()
relj7zay

relj7zay3#

试试这个:

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

t9aqgxwy4#

您可能应该将代码修改为如下形式:

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

显然,<>变量需要是实际的字符串值,或者是有效的变量,我只是将它们作为占位符填充。

zu0ti5jz

zu0ti5jz5#

参见smtplib文档底部的注解:
第一个月
下面是email文档中示例部分的链接,该部分实际上展示了创建带有主题行的消息。
smtplib似乎不直接支持主题添加,而是希望msg已经用主题等进行了格式化。这就是email模块的作用所在。

67up9zun

67up9zun6#

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

u7up0aaq7#

我觉得你应该把它写进信息里:

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

34gzjxbg

34gzjxbg8#

如果将其 Package 在函数中,则应将其作为模板。

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

t2a7ltrp9#

试试这个:

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

相关问题