/
automatedemail.py
68 lines (47 loc) · 1.81 KB
/
automatedemail.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
'''
Wriiten By : Prakriti Regmi
On : 8th Feb 2022
Description : This is a simple python script to send an automated email to the user.
'''
#Neceessary Libraries
import smtplib , ssl
from datetime import datetime
#Function to send the email
def send_email():
#Fetching the current month
currentMonth= datetime.now().strftime("%b")
#Setting Email Subject
subject= "Product Update Details."
#Setting Email Body
body = f"Greetings, \n \n I hope this email finds you well. \n This is to inform that the product updates for the month of {currentMonth} has been successfully completed in all our media.\n\n\nWith Best Regards,\nPrakriti Regmi."
#Compiling the entire email
message = f"Subject : {subject}\n\n {body}"
#Setting the email server
port = 465 # For SSL
smtp_server = "smtp.gmail.com"
#Setting the personal gmail credentials
password = "idskaefmzabjqlhk"
sender_email = "regmi.prakriti24@gmail.com"
#Setting the receiver emails
receiver_email = ["tbf110011@gmail.com", "regmi.prakriti24@gmail.com"]
#Creating the ssl object
context = ssl.create_default_context()
with smtplib.SMTP_SSL(smtp_server, port, context=context) as server:
try :
try:
#Logging into the email server
server.login(sender_email, password)
except smtplib.SMTPAuthenticationError:
print("Authentication Error")
return None
#Sending the email
server.sendmail(sender_email, receiver_email, str(message))
print("email sent")
return None
except Exception as e:
print(e)
print("email not sent")
return None
server.quit()
#Calling the function
send_email()