简体   繁体   中英

Python: SMTPLIB and MIMETEXT Adding an attachment to an email

iv'e just started with these new modules in python and have been learing how to send an e-mail. I can't find a solution on the web... How can I add an image/video attachment to this e-mail script?

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()

To include attachments, the email message should have a MIME type of multipart/mixed. You can create a multipart message using email.mime.multipart.MIMEMultipart .

You can send an image attachment like so:

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()

If you want the pictures to be used in your HTML, they need to have a Content-ID header:

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:picture@example.com" 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', '<picture@example.com>')
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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM