1 from email.mime.multipart import MIMEMultipart 2 from email.mime.text import MIMEText 3 4 import mimetypes 5 6 import smtplib 7 from email.mime.multipart import MIMEMultipart 8 from email.mime.text import MIMEText 9 10 11 12 def send_email_oauth(sender, to, subject, msgHtml, msgPlain, attachmentFile=None): 13 print(1) 14 username = "bungeeinternal@bungeetech.com" 15 password = "123@456#789Billion" 16 connection = smtplib.SMTP(host='smtp.office365.com', port=587) 17 connection.starttls() 18 connection.login(username,password) 19 message = 'Subject: {}\n\n{}'.format(subject, msgPlain) 20 try: 21 msg= CreateMessageHtml(sender, to,subject, msgHtml, msgPlain) 22 print(msg) 23 24 except Exception as e: 25 print(e) 26 27 28 29 30 31 def CreateMessageHtml(sender, to, subject, msgHtml, msgPlain): 32 msg = MIMEMultipart('alternative') 33 msg['Subject'] = subject 34 msg['From'] = sender 35 msg['To'] = to 36 msg.attach(MIMEText(msgPlain, 'plain')) 37 if msgHtml is None: 38 msgPlain = msgPlain.replace("\n", "<br />\n") 39 msgHtml = """\ 40 <html> 41 <head></head> 42 <body> 43 <p>{} 44 </p> 45 </body> 46 </html> 47 """.format(msgPlain) 48 msg.attach(MIMEText(msgHtml, 'html')) 49 return {'raw': base64.urlsafe_b64encode(msg.as_string().encode()).decode()} 50 51 52 def createMessageWithAttachment( 53 sender, to, subject, msgHtml, msgPlain, attachmentFile): 54 """Create a message for an email. 55 56 Args: 57 sender: Email address of the sender. 58 to: Email address of the receiver. 59 subject: The subject of the email message. 60 msgHtml: Html message to be sent 61 msgPlain: Alternative plain text message for older email clients 62 attachmentFile: The path to the file to be attached. 63 64 Returns: 65 An object containing a base64url encoded email object. 66 """ 67 message = MIMEMultipart('mixed') 68 message['to'] = to 69 message['from'] = sender 70 message['subject'] = subject 71 72 messageA = MIMEMultipart('alternative') 73 messageR = MIMEMultipart('related') 74 75 messageR.attach(MIMEText(msgHtml, 'html')) 76 messageA.attach(MIMEText(msgPlain, 'plain')) 77 messageA.attach(messageR) 78 79 message.attach(messageA) 80 81 print("create_message_with_attachment: file: %s" % attachmentFile) 82 content_type, encoding = mimetypes.guess_type(attachmentFile) 83 84 if content_type is None or encoding is not None: 85 content_type = 'application/octet-stream' 86 main_type, sub_type = content_type.split('/', 1) 87 if main_type == 'text': 88 fp = open(attachmentFile, 'rb') 89 msg = MIMEText(fp.read(), _subtype=sub_type) 90 fp.close() 91 elif main_type == 'image': 92 fp = open(attachmentFile, 'rb') 93 msg = MIMEImage(fp.read(), _subtype=sub_type) 94 fp.close() 95 elif main_type == 'audio': 96 fp = open(attachmentFile, 'rb') 97 msg = MIMEAudio(fp.read(), _subtype=sub_type) 98 fp.close() 99 else: 100 fp = open(attachmentFile, 'rb') 101 msg = MIMEBase(main_type, sub_type) 102 msg.set_payload(fp.read()) 103 fp.close() 104 filename = os.path.basename(attachmentFile) 105 msg.add_header('Content-Disposition', 'attachment', filename=filename) 106 message.attach(msg) 107 108 return {'raw': base64.urlsafe_b64encode(message.as_string())} 109 110 111 def main(): 112 to = "shubham@bungeetech.com" 113 sender = "bungeeinternal@bungeetech.com" 114 subject = "testing oauth" 115 116 msgPlain = "<span style=\"color:red\"><u><b><h2>red</h2></b></u></span> " 117 send_email_oauth(sender, to, subject, None, msgPlain) 118 # Send message with attachment: 119 # SendMessage(sender, to, subject, msgHtml, msgPlain, '/path/to/file.pdf') 120 121 122 main() 123