-
Notifications
You must be signed in to change notification settings - Fork 0
/
outlook.py
54 lines (42 loc) · 1.76 KB
/
outlook.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import win32com.client
import os
import requests
class Outlook:
def __init__(self):
self.outlook = win32com.client.Dispatch("Outlook.Application")
def send_email(self, subject, to="", body="", cc="", bcc="", attachments=None, html_body_table=""):
self.mail = self.outlook.CreateItem(0) # 0 significa email
self.mail.Display() # Abre a janela de composição de e-mail
signature = self.get_outlook_signature(self.mail) # Obtém a assinatura do Outlook
body += "<br>" + html_body_table + "<br>" + signature # Adiciona a assinatura ao corpo do e-mail
self.mail.Subject = subject
self.mail.To = to
self.mail.CC = cc
self.mail.BCC = bcc
self.mail.HTMLBody = body
if attachments:
if isinstance(attachments, str): # Se houver apenas um anexo
if os.path.exists(attachments.strip()):
self.mail.Attachments.Add(attachments.strip())
elif isinstance(attachments, list): # Se houver múltiplos anexos
for attachment in attachments:
if os.path.exists(str(attachment).strip()):
self.mail.Attachments.Add(str(attachment).strip())
#mail.Send()
def get_outlook_signature(self, mail):
# Obtém a assinatura do Outlook
try:
signature = mail.HTMLbody
except AttributeError:
signature = ""
return signature
if __name__ == "__main__":
outlook_sender = Outlook()
# Exemplo de uso:
subject = "E-mail Subject"
to = "robertn@weg.net"
body = "E-mail Body"
cc = "robertn@weg.net"
bcc = "robertn@weg.net"
attachments = [""]
outlook_sender.send_email(subject, to, body, cc, bcc, attachments)