如何在包含默认电子邮件签名和字体大小的同时用python发送gmail电子邮件?

2024-05-23 17:44:40 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用Python通过Gmail发送电子邮件。不过,我想添加我的电子邮件签名和字体大小(大),这已经在我的Gmail设置中指定。下面是发送电子邮件的代码;我需要添加什么来完成此要求

代码如下:

import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email import encoders

subject = "Offer"
message = "My offer is 2 dollars"


email = "sender@gmail.com"  
password ='mypassword'
send_to_email = "recipient@gmail.com"  

msg = MIMEMultipart()
msg["From"] = email
msg["To"] = send_to_email
msg["Subject"] = subject

msg.attach(MIMEText(message, 'plain'))

server = smtplib.SMTP("smtp.gmail.com", 587)
server.starttls()
server.login(email, password)
text = msg.as_string()
server.sendmail(email, send_to_email, text)
server.quit()

Tags: to代码textfromimportcomsendserver
1条回答
网友
1楼 · 发布于 2024-05-23 17:44:40

纯文本MIME部分(text/plain)没有字体大小;它们只是ASCII文本,没有任何格式化功能(超出ASCII提供的功能,即换行符和制表符)

如果要嵌入格式,请发送HTML电子邮件

顺便说一句,您的代码看起来像是Python 3.6之前的版本;在这个版本中email库经历了一次重大的修改。新代码通常应该以新的EmailMessageAPI为目标,它比旧的EmailMessageAPI更简单,也更通用,旧的EmailMessageAPI要求您在每条消息中明确地设置一个MIME结构。有关良好的起点,请参见the examples in the documentation

相关问题 更多 >