Skip to content

Commit 50b115d

Browse files
authored
Update code.py
Instead of printing to the console, consider using the logging module for better logging management, try-except blocks to handle potential errors during the SMTP connection and email sending process, environment variables, context managers and improved handle attachments to avoid runtime errors
1 parent c643b6f commit 50b115d

File tree

1 file changed

+42
-34
lines changed

1 file changed

+42
-34
lines changed

AUTOMATION/Sending-Emails/code.py

+42-34
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,44 @@
1-
###### Notice ######
2-
3-
# before sending email you have to enable your less secure apps access in your sending mail ([email protected])
4-
51
import smtplib
2+
import os
63
from email.message import EmailMessage
7-
8-
EMAIL_ADDRESS = "[email protected]" # your email-id goes here
9-
EMAIL_PASSWORD = "xyz" # your password goes here
10-
11-
msg = EmailMessage()
12-
msg['Subject'] = 'this is subject'
13-
msg['From'] = EMAIL_ADDRESS
14-
msg['To'] = EMAIL_ADDRESS
15-
16-
msg.set_content('Content area')
17-
18-
msg.add_alternative("""\
19-
<html>
20-
<body>
21-
<h1>Your HTML CONTENT GOES HERE </h1>
22-
</body>
23-
</html>
24-
""", subtype='html')
25-
26-
with open('testing.txt', 'rb') as f: # your filename will go here
27-
file_data = f.read()
28-
file_name = f.name
29-
30-
msg.add_attachment(file_data, maintype='application', subtype='octet-stream', filename=file_name)
31-
32-
with smtplib.SMTP_SSL('smtp.gmail.com', 465) as smtp:
33-
smtp.login(EMAIL_ADDRESS, EMAIL_PASSWORD)
34-
35-
smtp.send_message(msg)
36-
print("Email Sent Successfully ..")
4+
import logging
5+
6+
# Set up logging
7+
logging.basicConfig(level=logging.INFO)
8+
9+
# Use environment variables for credentials
10+
EMAIL_ADDRESS = os.getenv('EMAIL_ADDRESS')
11+
EMAIL_PASSWORD = os.getenv('EMAIL_PASSWORD')
12+
13+
def send_email(subject, recipient, body, html_content=None, attachment_path=None):
14+
msg = EmailMessage()
15+
msg['Subject'] = subject
16+
msg['From'] = EMAIL_ADDRESS
17+
msg['To'] = recipient
18+
19+
msg.set_content(body)
20+
21+
if html_content:
22+
msg.add_alternative(html_content, subtype='html')
23+
24+
if attachment_path:
25+
try:
26+
with open(attachment_path, 'rb') as f:
27+
file_data = f.read()
28+
file_name = f.name
29+
msg.add_attachment(file_data, maintype='application', subtype='octet-stream', filename=file_name)
30+
except FileNotFoundError:
31+
logging.error(f"Attachment file {attachment_path} not found.")
32+
return
33+
34+
try:
35+
with smtplib.SMTP_SSL('smtp.gmail.com', 465) as smtp:
36+
smtp.login(EMAIL_ADDRESS, EMAIL_PASSWORD)
37+
smtp.send_message(msg)
38+
logging.info("Email Sent Successfully")
39+
except Exception as e:
40+
logging.error(f"An error occurred: {e}")
41+
42+
# Usage Example
43+
send_email('Test Subject', '[email protected]', 'This is the email body',
44+
'<html><body><h1>HTML Content</h1></body></html>', 'testing.txt')

0 commit comments

Comments
 (0)