Skip to content

Commit d70a1f2

Browse files
committed
Merge remote-tracking branch 'origin/main' into dino-game
2 parents 692c546 + 9354d09 commit d70a1f2

File tree

1 file changed

+42
-34
lines changed

1 file changed

+42
-34
lines changed

AUTOMATION/Sending-Emails/code.py

Lines changed: 42 additions & 34 deletions
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 (xyz@gmail.com)
4-
51
import smtplib
2+
import os
63
from email.message import EmailMessage
7-
8-
EMAIL_ADDRESS = "xyz@gmail.com" # 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', 'recipient@example.com', 'This is the email body',
44+
'<html><body><h1>HTML Content</h1></body></html>', 'testing.txt')

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy