SHOW:
|
|
- or go back to the newest paste.
| 1 | import smtplib | |
| 2 | from email.message import EmailMessage | |
| 3 | from email.message import MIMEPart | |
| 4 | from email.mime.multipart import MIMEMultipart | |
| 5 | from base64 import encodebytes | |
| 6 | import mimetypes | |
| 7 | ||
| 8 | mSubject = 'Another new family reunion' | |
| 9 | mFrom = '[email protected]' | |
| 10 | mTo = '[email protected]' | |
| 11 | mCC = '[email protected]' | |
| 12 | sLogin = '[email protected]' | |
| 13 | sPass = 'mypass' | |
| 14 | mServer = "smtp.gmail.com" | |
| 15 | mPort = 465 | |
| 16 | ||
| 17 | filename1 = "file1.jpg" | |
| 18 | filename2 = "file2.jpg" | |
| 19 | filename3 = "file3.jpg" | |
| 20 | image_cid1 = filename1 + "@xxxx1" | |
| 21 | image_cid2 = filename2 + "@xxxx2" | |
| 22 | ||
| 23 | ||
| 24 | def inline_part(filename, icid): | |
| 25 | """ Creates a MIMEMultipart for embedded images """ | |
| 26 | part = MIMEMultipart('related') | |
| 27 | ||
| 28 | fp = open(filename, 'rb') | |
| 29 | part.set_payload(encodebytes(fp.read()).decode()) | |
| 30 | fp.close() | |
| 31 | part.add_header('Content-ID', '<' + icid + '>') | |
| 32 | part.add_header("Content-Transfer-Encoding", "base64") | |
| 33 | return part | |
| 34 | ||
| 35 | def attach_part(filename): | |
| 36 | """ Creates a MIMEPart for attached images """ | |
| 37 | part = MIMEPart() | |
| 38 | fp = open(filename, 'rb') | |
| 39 | part.set_payload(encodebytes(fp.read()).decode()) | |
| 40 | fp.close() | |
| 41 | ||
| 42 | part.add_header("Content-Type", mimetypes.guess_type(filename2)[0] + '; filename="%s"' % filename) | |
| 43 | part.add_header("Content-Transfer-Encoding", "base64") | |
| 44 | part.add_header('Content-Disposition', 'attachment; filename="%s"' % filename) | |
| 45 | return part | |
| 46 | ||
| 47 | #set up the mime types | |
| 48 | mimetypes.init() | |
| 49 | ||
| 50 | # Create the container email message. | |
| 51 | msg = EmailMessage() | |
| 52 | ||
| 53 | msg['Subject'] = mSubject | |
| 54 | msg['From'] = mFrom | |
| 55 | msg['To'] = mTo | |
| 56 | msg['Bcc'] = mCC | |
| 57 | ||
| 58 | body ="""<HTML> | |
| 59 | <head></head> | |
| 60 | <body> | |
| 61 | This is a test email to send an attachment. | |
| 62 | This is a linked image: | |
| 63 | - | <div align="center"><p><img src="http://www.freedigitalphotos.net/images/previews/success-road-sign-10031660.jpg"><br></p><p> </p></div> |
| 63 | + | <div align="center"><p><img src="http://www.freedigitalphotos.net/images/previews/success-road-sign-10031660.jpg"><br></p><p> </p></div> |
| 64 | we will try to add two in-line images as well, named file1.jpg and file2.jpg | |
| 65 | """ | |
| 66 | # add each of the the in-line photo body info | |
| 67 | body += ' <p>This is file 1</p>n<p><img id="'+ filename1 + '" src="cid:' + image_cid1 + '" alt="file01.jpg"></p>n' | |
| 68 | body += ' <p>This is file 2</p>n<p><img id="'+ filename2 + '" src="cid:' + image_cid2 + '" alt="file02.jpg"></p>n' | |
| 69 | ||
| 70 | #add the rest of the body | |
| 71 | body +=""" <p>and we have attached one file "file3.jpg" | |
| 72 | </body> | |
| 73 | </HTML> | |
| 74 | """ | |
| 75 | ||
| 76 | # add the body to the message | |
| 77 | msg.add_alternative(body, subtype='html') | |
| 78 | msg.set_boundary("===myBoundry") | |
| 79 | # attach the in-line files | |
| 80 | msg.attach(inline_part(filename1, image_cid1)) | |
| 81 | msg.attach(inline_part(filename2, image_cid2)) | |
| 82 | # attach the attachment file | |
| 83 | msg.attach(attach_part(filename3)) | |
| 84 | ||
| 85 | # Open a connection to the mail server, log-on, send the message and disconnect | |
| 86 | try: | |
| 87 | server = smtplib.SMTP_SSL(mServer, mPort) | |
| 88 | server.set_debuglevel(0) | |
| 89 | server.login(sLogin, sPass) | |
| 90 | server.send_message(msg) | |
| 91 | server.quit() | |
| 92 | print ("Message sent Successfully") | |
| 93 | except Exception as e: | |
| 94 | print ("Error sending mail:nt" + str(e)) |