【问题标题】:How to send gmail emails in python while including default email signature and font size?python - 如何在包含默认电子邮件签名和字体大小的同时在python中发送gmail电子邮件?
【发布时间】:2021-07-25 18:21:39
【问题描述】:

我正在尝试使用 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()

【问题讨论】:

    标签: python gmail


    【解决方案1】:

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

    如果您想嵌入格式,请发送 HTML 电子邮件。

    顺便说一下,您的代码看起来像是 Python 3.6 之前的版本; email 库在该版本中进行了重大改革。新代码通常应该针对新的EmailMessage API,它比旧的更直接,也更通用,旧的需要你在每条消息中显式地设置一个 MIME 结构。请参阅the examples in the documentation 以获得良好的起点。

    【讨论】:

    • ...虽然就个人而言,我更喜欢纯文本,并且对试图覆盖我的字体偏好的电子邮件感到冒犯。
    猜你喜欢
    • 2021-07-04
    • 2016-09-09
    • 2020-07-12
    • 1970-01-01
    • 2021-08-09
    • 1970-01-01
    • 2016-03-22
    • 1970-01-01
    • 2023-03-18
    相关资源
    最近更新 更多