问题描述
我目前有一个程序,将从列表中随机选择引号并发送电子邮件。我现在也试图在电子邮件中嵌入一个图像。我遇到一个问题,我可以附加电子邮件,但我的报价不再工作。我在网上进行了研究,解决方案对我来说并不奏效。请注意,我使用的是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 ,'rb')
img = MIMEImage(fp.read())
fp.close()
msg.attach(img)
#email_message ='%s\\\
%s\\\
%s'%(subject_header,body,img)
email_message ='%s\\\
%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,m sg.as_string())
emailRezi.quit()
正如你可以从上面的代码我尝试过不同的方法(参考#)
你将通过皇家痛苦来构建一个有效的MIME消息在 msg
中,然后将其分开并发送一个简单的字符串 email_message
。
您应该首先了解什么是正确的MIME结构。多部分消息本身没有任何内容,如果您想要一个文本部分,您必须添加一个文本部分。
以下是您的脚本的编辑添加了丢失的部分我没有尝试发送结果信息。
from email.mime.multipart import MIMEMultipart
from email.mime .text import MIMEText#从email.mime.image导入MIMEImage
附件='bob.jpg'
msg = MIMEMultipart()
msg [To] = to
msg [From] = from
msg [Subject] = subject
msgText = MIMEText('< b> s< / b>< br>< img src =cid:%s>< br>'%(body,attachment),'html')
msg.attach(msgText) ,并编辑前一行
fp = open(attachment,'rb')
img = MIMEImage(fp.read())
fp.close()
img.add_header('Content-ID','< {}>'。format(attachment))
msg.attach(img)
打印msg.as_string()
退出(0)
I currently have a program that will randomly select quotes from a list and email them. I'm now trying to embed an image in the email as well. I've come across a problem where I can attach the email but my quotes no longer work. I have researched online and the solutions are not working for me. Note that I am using Python 3.2.2.
Any guidance would be appreciated.
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()
As you can tell from the code above I've tried different ways (referencing the #)
You are going through royal pains to construct a valid MIME message in msg
, then ditching it and sending a simple string email_message
instead.
You should probably begin by understanding what the proper MIME structure looks like. A multipart message by itself has no contents at all, you have to add a text part if you want a text part.
The following is an edit of your script with the missing pieces added. I have not attempted to send the resulting message.
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
msg["From"] = from
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
fp = open(attachment, 'rb')
img = MIMEImage(fp.read())
fp.close()
img.add_header('Content-ID', '<{}>'.format(attachment))
msg.attach(img)
print msg.as_string()
exit(0)
这篇关于在电子邮件中嵌入图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!