2014-10-29 62 views
0

iv'e剛剛開始使用python中的這些新模塊,並一直在學習如何發送電子郵件。我無法在網絡上找到解決方案...如何將圖像/視頻附件添加到此電子郵件腳本中?Python:SMTPLIB和MIMETEXT向電子郵件添加附件

import smtplib 
from email.mime.text import MIMEText 

abc = 'BLACK TITLE' 
msg_content = '<h2>{title} <font color="green">TITLE HERE</font></h2>'.format(title=abc) 
p1 = '<p>new line (paragraph 1)<\p>' 
p2 = '<p>Image below soon hopefully...<\p>' 

message = MIMEText((msg_content+p1+p2), 'html') 

message['From'] = 'Sender Name <sender>' 
message['To'] = 'Receiver Name <receiver>' 
message['Cc'] = 'Receiver2 Name <>' 
message['Subject'] = 'Python Test E-mail' 
msg_full = message.as_string() 

server = smtplib.SMTP('smtp.gmail.com:587') 
server.starttls() 
server.login('sender', 'password') 
server.sendmail('sender',['receiver', 'receiver'],msg_full) 
server.quit() 
+0

你的回答被發現[在文檔中](https://docs.python.org/2/library/email-examples.html)。 – 2014-10-29 13:48:26

回答

0

要包含附件,電子郵件消息應該具有multipart/mixed的MIME類型。您可以使用email.mime.multipart.MIMEMultipart創建多部分消息。

您可以發送圖片附件,像這樣:

import smtplib 
from email.mime.text import MIMEText 
from email.mime.multipart import MIMEMultipart 
from email.mime.image import MIMEImage 

message = MIMEMultipart() 

abc = 'BLACK TITLE' 
msg_content = '<h2>{title} <font color="green">TITLE HERE</font></h2>'.format(title=abc) 
p1 = '<p>new line (paragraph 1)<\p>' 
p2 = '<p>Image below soon hopefully...<\p>' 
message.attach(MIMEText((msg_content+p1+p2), 'html')) 

with open('image.jpg', 'rb') as image_file: 
    message.attach(MIMEImage(image_file.read())) 


message['From'] = sender 
message['To'] = receiver 
message['Subject'] = 'Python Test E-mail' 
msg_full = message.as_string() 

server = smtplib.SMTP('smtp.gmail.com:587') 
server.starttls() 
server.login(sender, password) 
server.sendmail(sender,[receiver],msg_full) 
server.quit() 

如果你想在你的HTML中使用的圖片,他們需要有一個Content-ID標題:

import smtplib 
from email.mime.text import MIMEText 
from email.mime.multipart import MIMEMultipart 
from email.mime.image import MIMEImage 

message = MIMEMultipart('related') 

msg_content = ''' 
<html><body><p><table><tr> 
    <td>Hello,</td> 
    <td><img src="cid:[email protected]" width="150" height="150"></td> 
    <td>world</td> 
</tr></table></p></body></html>''' 
message.attach(MIMEText((msg_content), 'html')) 

with open('image.jpg', 'rb') as image_file: 
    image = MIMEImage(image_file.read()) 
image.add_header('Content-ID', '<[email protected]>') 
image.add_header('Content-Disposition', 'inline', filename='image.jpg') 
message.attach(image) 


message['From'] = sender 
message['To'] = receiver 
message['Subject'] = 'Python Test E-mail' 
msg_full = message.as_string() 

server = smtplib.SMTP('smtp.gmail.com:587') 
server.starttls() 
server.login(sender, password) 
server.sendmail(sender,[receiver],msg_full) 
server.quit() 
+0

不幸的是,當程序在輸入發送者和接收者數據的情況下運行時,shell會產生並返回錯誤:Traceback(最近一次調用最後一次): 文件「C:\ Users \ James \ OneDrive \ Documents \ email test 2.py」,第15行,在 message.attach(MIMEImage(image_file.read())) 文件「C:\ Python34 \ lib \ email \ mime \ image.py」,第43行,在__init__中 raise TypeError('Could not猜你喜歡的圖像MIME子類型') TypeError:無法猜測圖像MIME子類型 – CookieGuy 2014-10-29 18:20:10

+0

我已通過編輯消息行message.attach(MIMEImage(image_file.read(),_subtype =「jpg」))修復了此錯誤。非常感謝您的幫助! – CookieGuy 2014-10-29 18:44:57

+0

不客氣。對於我使用的JPG文件,不需要「子類型」。好吧。我很高興它有幫助。 – 2014-10-29 22:47:26