我正在尝试使用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()
纯文本MIME部分(
text/plain
)没有字体大小;它们只是ASCII文本,没有任何格式化功能(超出ASCII提供的功能,即换行符和制表符)如果要嵌入格式,请发送HTML电子邮件
顺便说一句,您的代码看起来像是Python 3.6之前的版本;在这个版本中
email
库经历了一次重大的修改。新代码通常应该以新的EmailMessage
API为目标,它比旧的EmailMessage
API更简单,也更通用,旧的EmailMessage
API要求您在每条消息中明确地设置一个MIME结构。有关良好的起点,请参见the examples in the documentation相关问题 更多 >
编程相关推荐