-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathms_mail.py
87 lines (75 loc) · 3.06 KB
/
ms_mail.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import win32com.client
import smtplib
import email
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import logging
class EmailHandler:
def __init__(self, smtp_server, email_address, password, smtp_port=587):
self.smtp_server = smtp_server
self.email_address = email_address
self.password = password
self.smtp_port = smtp_port
self.smtp_connection = None
logging.basicConfig(level=logging.INFO)
def connect_imap(self):
try:
self.outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
logging.info("Successfully connected to Outlook MAPI")
except Exception as e:
logging.error(f"Failed to connect to Outlook MAPI: {e}")
def connect_smtp(self):
try:
self.smtp_connection = smtplib.SMTP(self.smtp_server, self.smtp_port)
self.smtp_connection.starttls()
self.smtp_connection.login(self.email_address, self.password)
logging.info("Successfully connected to SMTP server")
except Exception as e:
logging.error(f"Failed to connect to SMTP server: {e}")
def disconnect_smtp(self):
if self.smtp_connection:
self.smtp_connection.quit()
logging.info("Disconnected from SMTP server")
def fetch_unread_emails(self):
try:
self.connect_imap()
inbox = self.outlook.GetDefaultFolder(6)
messages = inbox.Items
unread_messages = messages.Restrict("[Unread]=True")
emails = []
for msg in unread_messages:
emails.append({
'subject': msg.Subject,
'from': msg.SenderName,
'to': msg.To,
'date': msg.ReceivedTime,
'body': msg.Body
})
return emails
except Exception as e:
logging.error(f"Failed to fetch unread emails: {e}")
return []
def send_email(self, to_address, subject, body):
try:
self.connect_smtp()
msg = MIMEMultipart()
msg['From'] = self.email_address
msg['To'] = to_address
msg['Subject'] = subject
msg.attach(MIMEText(body, 'plain'))
self.smtp_connection.sendmail(self.email_address, to_address, msg.as_string())
logging.info("Email sent successfully")
except Exception as e:
logging.error(f"Failed to send email: {e}")
finally:
self.disconnect_smtp()
# Example usage
if __name__ == "__main__":
smtp_server = "smtp-mail.outlook.com"
email_address = "[email protected]"
password = "141368068Andriesplein@6"
email_handler = EmailHandler(smtp_server, email_address, password)
unread_emails = email_handler.fetch_unread_emails()
for email in unread_emails:
print(f"Subject: {email['subject']}, From: {email['from']}")
email_handler.send_email("[email protected]", "Test Subject", "This is a test email.")