def send_email(subject, plain_text, fromaddr=None, toaddr=None, attach_images=None): # Define these once; use them twice! if not fromaddr: fromaddr = os.environ.get('USER') if not toaddr: toaddr = os.environ.get('USER') if attach_images is None: attach_images = [] # Create the root message and fill in the from, to, and subject headers message = email.mime.multipart.MIMEMultipart('related') message['Subject'] = subject message['From'] = fromaddr message['To'] = toaddr # message.preamble = 'This is a multi-part message in MIME format.' # Encapsulate the plain and HTML versions of the message body in an # 'alternative' part, so message agents can decide which they want to display. alt_part = email.mime.multipart.MIMEMultipart('alternative') message.attach(alt_part) if plain_text: alt_part.attach(email.mime.text.MIMEText(plain_text)) # We reference the image in the IMG SRC attribute by the ID we give it below if _HAVE_MD: msg_html = markdown.markdown(plain_text) else: msg_html = plain_text.replace("\n", "<br>") for attach_file in attach_images: msg_html += '<img src="cid:%s" width="100%%"><br>' % attach_file if msg_html or attach_images: alt_part.attach(email.mime.text.MIMEText(msg_html, 'html')) for attach_file in attach_images: # This example assumes the image is in the current directory with open(attach_file, 'rb') as image_fp: image = email.mime.image.MIMEImage(image_fp.read()) # Define the image's ID as referenced above image.add_header('Content-ID', '<%s>' % attach_file) message.attach(image) # Send the email (this example assumes SMTP authentication is required) smtp = smtplib.SMTP() smtp.connect() # smtp.login('exampleuser', 'examplepass') smtp.sendmail(fromaddr, toaddr, message.as_string()) smtp.quit()
def test_get_mailparts(self): """test get_mailparts()""" import email.mime.multipart import email.mime.text import email.mime.image msg=email.mime.multipart.MIMEMultipart(boundary='===limit1==') txt=email.mime.text.MIMEText('The text.', 'plain', 'us-ascii') msg.attach(txt) image=email.mime.image.MIMEImage(b'data', 'png') image.add_header('Content-Disposition', 'attachment', filename='image.png') image.add_header('Content-Description', 'the description') image.add_header('Content-ID', '<this.is.the.normaly.unique.contentid>') msg.attach(image) raw=msg.as_string(unixfrom=False) expected_raw="""Content-Type: multipart/mixed; boundary="===limit1==" MIME-Version: 1.0 --===limit1== Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit The text. --===limit1== Content-Type: image/png MIME-Version: 1.0 Content-Transfer-Encoding: base64 Content-Disposition: attachment; filename="image.png" Content-Description: the description Content-ID: <this.is.the.normaly.unique.contentid> ZGF0YQ==<HERE1> --===limit1==-- """ if sys.version_info<(3, 0): expected_raw=expected_raw.replace('<HERE1>','') else: expected_raw=expected_raw.replace('<HERE1>','\n') self.assertEqual(raw, expected_raw) parts=get_mail_parts(msg) # [MailPart<*text/plain charset=us-ascii len=9>, MailPart<image/png filename=image.png len=4>] self.assertEqual(len(parts), 2) self.assertEqual(parts[0].type, 'text/plain') self.assertEqual(parts[0].is_body, 'text/plain') # not a error, is_body must be type self.assertEqual(parts[0].charset, 'us-ascii') self.assertEqual(parts[0].get_payload().decode(parts[0].charset), 'The text.') self.assertEqual(parts[1].type, 'image/png') self.assertEqual(parts[1].is_body, False) self.assertEqual(parts[1].charset, None) self.assertEqual(parts[1].filename, 'image.png') self.assertEqual(parts[1].description, 'the description') self.assertEqual(parts[1].content_id, 'this.is.the.normaly.unique.contentid') self.assertEqual(parts[1].get_payload(), b'data')
def AttachImage(self,partId,fname): fp = open(fname) image = email.mime.image.MIMEImage(fp.read()) fp.close() image.add_header("Content-ID","<" + partId + ">") self.msg.attach(image)