在邮件中嵌入图片
我现在有一个程序,可以从一个列表中随机选择名言并发送邮件。现在我想在邮件中嵌入一张图片。但是我遇到了一个问题:我可以把图片作为附件发送,但我的名言就不再工作了。我在网上查了很多资料,但找到的解决办法对我都没用。请注意,我使用的是Python 3.2.2。
如果能给点建议,我会很感激。
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.image import MIMEImage
attachment = 'bob.jpg'
msg = MIMEMultipart()
msg["To"] = to_addr
msg["From"] = from_addr
msg["Subject"] = subject_header
#msgText = MIMEText(<b>%s</b><br><img src="cid:bob.jpg"><br>, 'html') % body
fp = open(attachment, 'rb')
img = MIMEImage(fp.read())
fp.close()
msg.attach(img)
#email_message = '%s\n%s\n%s' % (subject_header, body, img)
email_message = '%s\n%s' % (subject_header, body)
emailRezi = smtplib.SMTP(mail_server, mail_server_port)
emailRezi.set_debuglevel(1)
emailRezi.login(mail_username, mail_password)
emailRezi.sendmail(from_addr, to_addr, email_message)
#emailRezi.sendmail(from_addr, to_addr, msg.as_string())
emailRezi.quit()
从上面的代码可以看出,我尝试了不同的方法(参考#)。
2 个回答
9
我已经编辑了内容,以便在消息正文和HTML模板中附加图片。
import smtplib
from email import encoders
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email.mime.image import MIMEImage
strFrom = 'zzzzzz@gmail.com'
strTo = 'xxxxx@gmail.com'
# Create the root message
msgRoot = MIMEMultipart('related')
msgRoot['Subject'] = 'test message'
msgRoot['From'] = strFrom
msgRoot['To'] = strTo
msgRoot['Cc'] =cc
msgRoot.preamble = 'Multi-part message in MIME format.'
msgAlternative = MIMEMultipart('alternative')
msgRoot.attach(msgAlternative)
msgText = MIMEText('Alternative plain text message.')
msgAlternative.attach(msgText)
msgText = MIMEText('<b>Some <i>HTML</i> text</b> and an image.<br><img src="cid:image1"><br>KPI-DATA!', 'html')
msgAlternative.attach(msgText)
#Attach Image
fp = open('test.png', 'rb') #Read image
msgImage = MIMEImage(fp.read())
fp.close()
# Define the image's ID as referenced above
msgImage.add_header('Content-ID', '<image1>')
msgRoot.attach(msgImage)
import smtplib
smtp = smtplib.SMTP()
smtp.connect('smtp.gmail.com') #SMTp Server Details
smtp.login('exampleuser', 'examplepass') #Username and Password of Account
smtp.sendmail(strFrom, strTo, msgRoot.as_string())
smtp.quit()
47
你在费尽心思地构建一个有效的MIME消息在msg
里,然后却放弃它,转而发送一个简单的字符串email_message
。
你可能应该先了解一下正确的MIME结构是什么样的。一个多部分的消息本身是没有内容的,如果你想要有文本部分,就得自己添加一个文本部分。
下面是你脚本的一个修改版,补充了缺失的部分。我没有尝试发送生成的消息。不过,下面有一个现代化的版本。
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText # Added
from email.mime.image import MIMEImage
attachment = 'bob.jpg'
msg = MIMEMultipart()
msg["To"] = to_addr
msg["From"] = from_addr
msg["Subject"] = subject
msgText = MIMEText('<b>%s</b><br/><img src="cid:%s"/><br/>' % (body, attachment), 'html')
msg.attach(msgText) # Added, and edited the previous line
with open(attachment, 'rb') as fp:
img = MIMEImage(fp.read())
img.add_header('Content-ID', '<{}>'.format(attachment))
msg.attach(img)
print(msg.as_string()) # or go ahead and send it
(我也稍微整理了一下HTML。)
自从Python 3.6以来,Python的email
库已经升级得更加模块化、逻辑清晰和独立(其实从3.3开始就有了,但在3.6时新版本成为了推荐使用的)。新的代码应该避免像上面那样显式地创建单独的MIME部分,可能更像下面这样:
from email.message import EmailMessage
from email.utils import make_msgid
attachment = 'bob.jpg'
msg = EmailMessage()
msg["To"] = to_addr
msg["From"] = from_addr
msg["Subject"] = subject
attachment_cid = make_msgid()
msg.set_content(
'<b>%s</b><br/><img src="cid:%s"/><br/>' % (
body, attachment_cid[1:-1]), 'html')
with open(attachment, 'rb') as fp:
msg.add_related(
fp.read(), 'image', 'jpeg', cid=attachment_cid)
# print(msg.as_string()), or go ahead and send
你会发现这和Python标准库中的email
示例文档里的“芦笋”例子非常相似。