-
Notifications
You must be signed in to change notification settings - Fork 6
/
mail.py
37 lines (30 loc) · 1.04 KB
/
mail.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
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.image import MIMEImage
import smtplib
def sendmail(to_email, message, image_path, payment_status):
from_email = ""
msg = MIMEMultipart('alternative')
msg['Subject'] = "Python Automation Workstop"
msg['From'] = from_email
msg['To'] = to_email
content = MIMEText(message, 'plain')
msg.attach(content)
# This example assumes the image is in the given directory
if payment_status == 'paid':
fp = open(image_path, 'rb')
msgImage = MIMEImage(fp.read())
fp.close()
msg.attach(msgImage)
response = {}
try:
with smtplib.SMTP("smtp.gmail.com", 587) as s:
s.starttls()
s.login(from_email, "")
print("Sending Mail:", to_email)
s.sendmail(from_email, to_email, msg.as_string())
response['email_status'] = "Success"
except Exception as err:
print(err)
response['email_status'] = "Failed"
return response