11import content
22import datetime
3+ import smtplib
4+ from email .message import EmailMessage
5+ from dotenv import load_dotenv # Import the load_dotenv function from the python-dotenv module
6+ import os
7+
8+
9+ load_dotenv () # Load environment variables from the .env file
10+
311
412class DailyEmail :
5- # Define the structure of the daily email content
613 def __init__ (self ):
714 self .content = {
815 "quote" : {"include" : True , "content" : content .get_random_quote ()},
916 "weather" : {"include" : True , "content" : content .get_weather_forecast ()},
1017 "wikipedia" : {"include" : True , "content" : content .get_wikipedia_article ()}
1118 }
19+
20+ self .recipient_list = {
21+ "email" : os .getenv ('RECIPIENT_EMAIL' ) # Get the recipient email address from environment variables
22+ }
23+
24+ self .sender_credentials = {
25+ "email" : os .getenv ('SENDER_EMAIL' ), # Get the sender email address from environment variables
26+ "password" : os .getenv ('SENDER_PASSWORD' ) # Get the sender email password from environment variables
27+ }
28+
29+
30+ """
31+ Generate email message body as plain text and HTML content.
32+ """
1233
13- # Format the general message of the email
1434 def format_email (self ):
15- # Define the plain text email message structure
16- message = f"Good morning!\n \n Here's your daily email for { datetime .datetime .now ().strftime ('%d-%m-%Y' )} :\n \n "
35+ # Enhanced text formatting with more visual separation and emphasis
36+ message = f"🌞 Daily Digest - { datetime .datetime .now ().strftime ('%d %B %Y' )} 🌞\n "
37+ message += "=" * 50 + "\n \n "
1738
18- # Add the quote to the message
39+ # Quote section with decorative borders
1940 if self .content ['quote' ]['include' ]:
2041 quote = self .content ['quote' ]['content' ]
21- message += f"Quote of the day: '{ quote ['quote' ]} ' - { quote ['author' ]} \n \n "
42+ message += "📜 Quote of the Day 📜\n "
43+ message += "-" * 25 + "\n "
44+ message += f"❝{ quote ['quote' ]} ❞\n "
45+ message += f" - { quote ['author' ]} \n \n "
2246
23- # Add the weather forecast to the message
47+ # Weather section with emoji and detailed formatting
2448 if self .content ['weather' ]['include' ]:
2549 weather = self .content ['weather' ]['content' ]
26- message += f"Today's weather forecast for { weather ['city' ]} , { weather ['country' ]} :\n "
50+ message += f"🌦️ Weather Forecast: { weather ['city' ]} , { weather ['country' ]} 🌦️\n "
51+ message += "-" * 50 + "\n "
2752 for period in weather ['forecast' ]:
28- message += f" - { period ['timestamp' ]} : { period ['description' ]} , { period ['temperature' ]} °C\n "
53+ message += f"• { period ['timestamp' ]} : { period ['description' ]} | { period ['temperature' ]} °C\n "
2954 message += "\n "
3055
31- # Add the Wikipedia article to the message
56+ # Wikipedia section with engaging header
3257 if self .content ['wikipedia' ]['include' ]:
3358 article = self .content ['wikipedia' ]['content' ]
34- message += f"Did you know? { article ['extract' ]} (source: { article ['url' ]} )\n \n "
59+ message += "🧠 Did You Know? 🧠\n "
60+ message += "-" * 25 + "\n "
61+ message += f"{ article ['extract' ]} \n "
62+ message += f"(Source: { article ['url' ]} )\n \n "
3563
36- # Add a closing message
37- message += "Have a great day!"
64+ message += "✨ Have a Fantastic Day! ✨"
3865
39- # Generate HTML content for the email
66+ # Enhanced HTML with more sophisticated styling and responsive design
4067 html = f"""
41- <html>
68+ <!DOCTYPE html>
69+ <html lang="en">
4270 <head>
71+ <meta charset="UTF-8">
72+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
73+ <title>Daily Digest - { datetime .datetime .now ().strftime ('%d %B %Y' )} </title>
74+ <link href="https://fonts.googleapis.com/css2?family=Roboto:wght@300;400;700&display=swap" rel="stylesheet">
4375 <style>
4476 body {{
45- font-family: Arial, sans-serif;
77+ font-family: 'Roboto', Arial, sans-serif;
78+ max-width: 600px;
79+ margin: 0 auto;
80+ padding: 20px;
4681 line-height: 1.6;
4782 color: #333;
83+ background-color: #f4f4f4;
84+ }}
85+ .container {{
86+ background-color: white;
87+ border-radius: 10px;
88+ box-shadow: 0 4px 6px rgba(0,0,0,0.1);
89+ padding: 30px;
4890 }}
4991 h1 {{
5092 color: #2c3e50;
93+ text-align: center;
94+ border-bottom: 2px solid #3498db;
95+ padding-bottom: 10px;
5196 }}
52- p {{
53- margin: 10px 0;
97+ .section {{
98+ margin-bottom: 20px;
99+ padding: 15px;
100+ border-radius: 5px;
54101 }}
55102 .quote {{
103+ background-color: #f9e79f;
56104 font-style: italic;
57- color: #e74c3c;
58105 }}
59106 .weather {{
60- background-color: #ecf0f1;
61- padding: 10px;
62- border-radius: 5px;
107+ background-color: #e6f2ff;
63108 }}
64109 .wikipedia {{
65- background-color: #f9e79f;
66- padding: 10px;
67- border-radius: 5px;
110+ background-color: #e8f6f3;
111+ }}
112+ .footer {{
113+ text-align: center;
114+ color: #7f8c8d;
115+ margin-top: 20px;
68116 }}
69117 </style>
70118 </head>
71119 <body>
72- <h1>Good Morning!</h1>
73- <p>Here's your daily email for { datetime .datetime .now ().strftime ('%d-%m-%Y' )} :</p>
74-
75- { f"<p class='quote'><b>Quote of the day:</b> '{ quote ['quote' ]} ' - { quote ['author' ]} </p>" if self .content ['quote' ]['include' ] else "" }
76-
77- { f"<div class='weather'><b>Today's weather forecast for { weather ['city' ]} , { weather ['country' ]} :</b><ul>" +
78- "" .join ([f"<li>{ period ['timestamp' ]} : { period ['description' ]} , { period ['temperature' ]} °C</li>" for period in weather ['forecast' ]]) +
79- "</ul></div>" if self .content ['weather' ]['include' ] else "" }
80-
81- { f"<div class='wikipedia'><b>Did you know?</b> { article ['extract' ]} <br>Source: <a href='{ article ['url' ]} '>{ article ['url' ]} </a></div>" if self .content ['wikipedia' ]['include' ] else "" }
82-
83- <p>Have a great day!</p>
120+ <div class="container">
121+ <h1>🌞 Daily Digest</h1>
122+ <p style="text-align: center; color: #7f8c8d;">
123+ { datetime .datetime .now ().strftime ('%d %B %Y' )}
124+ </p>
125+
126+ { f'''
127+ <div class="section quote">
128+ <h2>📜 Quote of the Day</h2>
129+ <blockquote>
130+ <i>{ quote ['quote' ]} </i>
131+ <footer>— { quote ['author' ]} </footer>
132+ </blockquote>
133+ </div>''' if self .content ['quote' ]['include' ] else '' }
134+
135+ { f'''
136+ <div class="section weather">
137+ <h2>🌦️ Weather Forecast</h2>
138+ <p><strong>Location:</strong> { weather ['city' ]} , { weather ['country' ]} </p>
139+ <ul>
140+ { '' .join ([f"<li>{ period ['timestamp' ]} : { period ['description' ]} , { period ['temperature' ]} °C</li>" for period in weather ['forecast' ]])}
141+ </ul>
142+ </div>''' if self .content ['weather' ]['include' ] else '' }
143+
144+ { f'''
145+ <div class="section wikipedia">
146+ <h2>🧠 Did You Know?</h2>
147+ <p>{ article ['extract' ]} </p>
148+ <p><em>Source: <a href="{ article ['url' ]} ">{ article ['url' ]} </a></em></p>
149+ </div>''' if self .content ['wikipedia' ]['include' ] else '' }
150+
151+ <div class="footer">
152+ <p>✨ Have a Fantastic Day! ✨</p>
153+ </div>
154+ </div>
84155 </body>
85156 </html>
86157 """
87158
88159 return message , html
89160
161+ """
162+ Send the formatted email to the recipient.
163+ """
90164 def send_email (self ):
91- pass
92-
93-
94-
95-
96-
165+ # Build the email message
166+ message = EmailMessage ()
167+ message ['Subject' ] = f"🌞 Daily Digest - { datetime .datetime .now ().strftime ('%d %B %Y' )} 🌞"
168+ message ['From' ] = self .sender_credentials ['email' ]
169+ message ['To' ] = self .recipient_list ['email' ]
170+
171+ # Add plain text and HTML content
172+ message_body_text , message_body_html = self .format_email ()
173+ message .set_content (message_body_text )
174+ message .add_alternative (message_body_html , subtype = 'html' )
175+
176+ # Send the email with the SMTP server
177+ with smtplib .SMTP_SSL ('smtp.gmail.com' , 465 ) as server :
178+ server .login (self .sender_credentials ['email' ], self .sender_credentials ['password' ])
179+ server .send_message (message )
180+ server .quit ()
181+ print ("Email sent successfully!" )
97182
98183if __name__ == '__main__' :
99184 # Create a DailyEmail instance
@@ -104,4 +189,14 @@ def send_email(self):
104189
105190 # Print the email content
106191 print (message )
107- print (html )
192+ print ("\n --- HTML Content ---" )
193+ print (html )
194+
195+ # Save the email content to files
196+ with open ('email.html' , 'w' , encoding = 'utf-8' ) as file :
197+ file .write (html )
198+ with open ('email.txt' , 'w' , encoding = 'utf-8' ) as file :
199+ file .write (message )
200+
201+ # Send the email
202+ email .send_email ()
0 commit comments