任何指导都不胜感激。
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)
更多推荐
在电子邮件中嵌入图片
发布评论