问题描述
我使用此代码在电子邮件正文中发送图像文件,但是一次只有单个图像文件进入电子邮件正文。 我想将图像文件发送到电子邮件正文中,既可以左右发送,也可以上下发送
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
import smtplib
from datetime import datetime
import glob
dateTimeObj = datetime.Now()
timestampStr = dateTimeObj.strftime("%d-%b-%Y")
username = "devid.miller"
password = "abc@123"
fromaddr = "[email protected]"
toaddr = "[email protected]"
ccaddr = "[email protected]"
msg = MIMEMultipart()
msg['From'] = fromaddr
msg['To'] = toaddr
msg["Cc"] = ccaddr
msg['Subject'] = "Daily Trade Report"
file1 = r'D:\dt\daily_pi_Trade.png' # This image i send in email body
body = "Daily Server Report"
file2 = r'D:\dt\daily_Trade.png' # I want this image file also in email body,left/right or upper or bottom of file1 image
msgText = MIMEText('<b>%s</b><br><center><img src="cid:%s"style="width: 100px;"></center><br>' % (body,file1),'html')
msg.attach(msgText) # Added,and edited the prevIoUs line
fp = open(file1,'rb')
img = MIMEImage(fp.read())
fp.close()
img.add_header('Content-ID','<{}>'.format(file1))
msg.attach(img)
# print (msg.as_string())
# exit(0)
try:
s = smtplib.SMTP("mail.mydomain.com",25)
s.login(username,password)
text = msg.as_string()
s.sendmail(fromaddr,toaddr.split(",")+ccaddr.split(","),text)
print('Sent')
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)