Skip to content

Commit 1207bf9

Browse files
committed
initial commit
1 parent 7428a1a commit 1207bf9

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

AUTOMATION/Email Automation/ReadMe.MD

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Automating email sending task
2+
3+
## Introduction
4+
utilizing the `smtplib` library
5+
6+
`smtp_server` and `smtp_port`: Set these variables to the appropriate SMTP server and port of your email provider.
7+
8+
`sender_email`: Specify the email address from which you want to send the email.
9+
10+
`sender_password`: Provide the password or an app-specific password for the sender's email account.
11+
12+
`recipient_email`: Specify the recipient's email address.
13+
14+
`subject`: Set the subject line of the email.
15+
16+
`message`: Provide the content or body of the email.
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import os
2+
import smtplib
3+
from email.mime.text import MIMEText
4+
from email.mime.multipart import MIMEMultipart
5+
6+
def send_email(sender_email, recipient_email, subject, message):
7+
# SMTP server configuration
8+
smtp_server = 'smtp.gmail.com'
9+
smtp_port = 587
10+
11+
# Get the sender password from an environment variable
12+
sender_password = os.environ.get('EMAIL_PASSWORD')
13+
14+
if not sender_password:
15+
print("Error: Email password not set in environment variable.")
16+
return
17+
18+
# Create the email message
19+
email = MIMEMultipart()
20+
email['From'] = sender_email
21+
email['To'] = recipient_email
22+
email['Subject'] = subject
23+
24+
# Create a MIMEText object with HTML content
25+
html_content = '''
26+
<html>
27+
<body>
28+
<h1>{}</h1>
29+
<p>{}</p>
30+
<p>This is a <strong>bold</strong> example.</p>
31+
<p>This is an <em>italic</em> example.</p>
32+
</body>
33+
</html>
34+
'''.format(subject, message)
35+
36+
email.attach(MIMEText(html_content, 'html'))
37+
38+
# Connect to the SMTP server
39+
server = smtplib.SMTP(smtp_server, smtp_port)
40+
server.starttls()
41+
server.login(sender_email, sender_password)
42+
43+
# Send the email
44+
server.sendmail(sender_email, recipient_email, email.as_string())
45+
46+
# Close the connection
47+
server.quit()
48+
49+
# Example usage
50+
sender_email = '[email protected]'
51+
recipient_email = '[email protected]'
52+
subject = 'Hello from Python Email Script'
53+
message = 'This is an automated email sent using Python.'
54+
55+
send_email(sender_email, recipient_email, subject, message)

0 commit comments

Comments
 (0)