我正在尝试构建一个脚本,该脚本可以使用python来自Excel的电子邮件正文来发送电子邮件:
在浏览完该网站后,我能够构建一个有效的脚本,除了能够在保留格式的同时附加电子邮件正文。
我已经成功发送了HTML电子邮件,但是我进行的所有复制这些单元格并将其粘贴到电子邮件正文中的尝试均无效。我需要格式化才能保留粘贴的数据。
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email.mime.application import MIMEApplication
from email import encoders
import imghdr
import os.path
import openpyxl
import os
#SUBJECT LINE & EXCEL BODY
filepath = 'C:\\Users\\EXAMPLE\\Desktop\\EXAMPLE'
filename = 'EXAMPLE'
os.chdir(filepath)
wb = openpyxl.load_workbook(filename, data_only=True)
ws = wb.active
sheet = wb['Email']
email_subject = sheet['D24'].value
email_body_ = ws["I5:v18"] #this is the table i want pasted
#email_body= "<html><p>Python Email Test</p></html>" #example of html
that works
#Email Setup
email_user = "example@gmail.com"
email_send = "example@gmail.com"
email_pass = "example"
msg = MIMEMultipart('alternative')
msg['From'] = email_user
msg['To'] = email_send
msg['Subject'] = email_subject
msg.attach(MIMEText(email_body, 'html', 'utf-8'))
# Email Server
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(email_user, email_pass)
text = msg.as_string()
server.sendmail(email_user,email_send , msg.as_string())
print ('email sent')
server.quit