Python:通过SMTP发送html格式的电子邮件
我有一个完美发送电子邮件的代码,但现在我需要做一些更新,并以html格式发送电子邮件。 我试着研究,但他们对我不起作用。如何将格式更改为html?目前,我接收的代码是字符串,而不是htmlPython:通过SMTP发送html格式的电子邮件,python,smtp,Python,Smtp,我有一个完美发送电子邮件的代码,但现在我需要做一些更新,并以html格式发送电子邮件。 我试着研究,但他们对我不起作用。如何将格式更改为html?目前,我接收的代码是字符串,而不是html server = smtplib.SMTP('smtp.gmail.com', 587) server.ehlo() server.starttls() server.ehlo() server.login(f'{login_email}', f'{login_emai
server = smtplib.SMTP('smtp.gmail.com', 587)
server.ehlo()
server.starttls()
server.ehlo()
server.login(f'{login_email}', f'{login_email_pass}')
Subject = f'{program_name} : Launch verification'
server.add_header('Content-Type','text/html')
Body2 = """\
<!DOCTYPE html>
<html>
<body>
<h1 style="color:black;text-align:center;font-family:verdana">SERVER 51SV15J LAUNCH</h1>
<p style="color:black;text-align:center;font-family:courier;font-size:120%">Code for confirmation is - <b>51256fd.</b></p>
</body>
</html>
"""
Body = f'{service} just have been started.\nServer Details:\nServer IPV4 : {Ipv4_Address}\nServer IPV6 : {Ipv6_Address}\nServer Region : {Ip_region}\nServer Internet Providers : {Ip_org}\nServer SessionID : {SessionID}\n\n* To verify it, please enter the code : {Code}'
msg = f"Subject : {Subject}\n\n{Body2}"
server.sendmail(
f'{login_email}',
f'{test_email}',
msg
)
server=smtplib.SMTP('SMTP.gmail.com',587)
server.ehlo()
server.starttls()
server.ehlo()
登录(f'{login\u email}',f'{login\u email\u pass}'))
Subject=f'{program_name}:启动验证'
server.add_头('Content-Type','text/html'))
Body2=“”\
服务器51SV15J启动
确认代码为-51256fd
"""
Body=f{service}刚刚启动。\n服务器详细信息:\n服务器IPV4:{IPV4\u地址}\n服务器IPV6:{IPV6\u地址}\n服务器区域:{Ip\u区域}\n服务器Internet提供商:{Ip\u组织}\n服务器会话ID:{SessionID}\n\n*要验证它,请输入代码:{code}
msg=f“主题:{Subject}\n\n{Body2}”
server.sendmail(
f'{login_email}',
f'{test_email}',
味精
)
我不知道如何在执行smtp.ehlo()时使用它,但也可以按如下方式执行:-
import os
import smtplib
from email.message import EmailMessage #new
EMAIL = f'{login_email}
PASSWORD = f'{login_email_pass}'
message = EmailMessage()
message['Subject'] = f'{program_name} : Launch verification'
message['From'] = EMAIL
message['To'] = EMAIL
message.set_content('This email is sent using python.')
message.add_alternative("""\
<!DOCTYPE html>
<html>
<body>
<h1 style="color:black;text-align:center;font-family:verdana">SERVER 51SV15J LAUNCH</h1>
<p style="color:black;text-align:center;font-family:courier;font-size:120%">Code for confirmation is - <b>51256fd.</b></p>
</body>
</html>
""", subtype = 'html')
with smtplib.SMTP_SSL('smtp.gmail.com', 465) as smtp:
smtp.login(EMAIL, PASSWORD)
smtp.send_message(message)
导入操作系统
导入smtplib
从email.message导入EmailMessage#新建
EMAIL=f'{login_EMAIL}
密码=f'{login\u email\u pass}'
message=EmailMessage()
消息['Subject']=f'{program_name}:启动验证'
消息['From']=电子邮件
信息['To']=电子邮件
message.set_content('此电子邮件使用python发送')
消息。添加可选项(“”)\
服务器51SV15J启动
确认代码为-51256fd
“”,子类型='html')
使用smtplib.SMTP_SSL('SMTP.gmail.com',465)作为SMTP:
smtp.login(电子邮件、密码)
smtp.send_消息(message)
我希望这会有所帮助。我没有尝试过,但使用了中的代码并对其进行了编辑以满足您的情况 嗯,它似乎在工作,但在控制台中我确实得到了这个HTTP错误429:未知状态代码