-
Notifications
You must be signed in to change notification settings - Fork 0
/
email_sender.py
72 lines (63 loc) · 2.92 KB
/
email_sender.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
from datetime import datetime
import platform
if platform.system() == "Windows":
import pythoncom
import win32com.client as win32
# Function to send an email with today's events
def send_email(events_today, recipient, subject, message):
"""
Sends an email with the list of today's economic events.
Args:
events_today (list): A list of dictionaries containing event details.
recipient (str): The email address of the recipient.
subject (str): The subject of the email.
message (str): The custom message to be included in the email body.
Returns:
str: A success or failure message.
"""
if platform.system() != "Windows":
return "Email sending is only supported on Windows."
try:
pythoncom.CoInitialize() # Initialize COM for use with Outlook
outlook = win32.Dispatch('outlook.application') # Get the Outlook application object
mail = outlook.CreateItem(0) # Create a new mail item (email)
mail.To = recipient # Set the recipient of the email
mail.Subject = subject # Set the subject of the email
# Create the HTML body of the email
body_html = f"""
<p>{message}</p>
<table border="1" style="border-collapse: collapse; width: 100%;">
<tr style="background-color: #f2f2f2;">
<th style="padding: 8px; text-align: left;">Date</th>
<th style="padding: 8px; text-align: left;">Time</th>
<th style="padding: 8px; text-align: left;">Currency</th>
<th style="padding: 8px; text-align: left;">Volatility</th>
<th style="padding: 8px; text-align: left;">Event</th>
<th style="padding: 8px; text-align: left;">Forecast</th>
<th style="padding: 8px; text-align: left;">Previous</th>
</tr>
"""
# Add each event to the HTML table
for event in events_today:
body_html += f"""
<tr>
<td style="padding: 8px;">{event['Date']}</td>
<td style="padding: 8px;">{event['Time']}</td>
<td style="padding: 8px;">{event['Currency']}</td>
<td style="padding: 8px;">{event['Volatility']}</td>
<td style="padding: 8px;">{event['Event']}</td>
<td style="padding: 8px;">{event['Forecast']}</td>
<td style="padding: 8px;">{event['Previous']}</td>
</tr>
"""
body_html += """
</table>
<p>If you need additional information, please let us know.</p>
<p>Best regards,<br>
Your Team.</p>
"""
mail.HTMLBody = body_html # Set the HTML body of the email
mail.Send() # Send the email
return "Email sent!" # Return a success message
except Exception as e:
return f"Failed to send email: {e}" # Return a failure message if an exception occurs