forked from 790hanu/Annex-qr-code-simulator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EmailSender.py
126 lines (114 loc) · 4.53 KB
/
EmailSender.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.mime.application import MIMEApplication
import smtplib
import ssl
from datetime import date
from email_validator import validate_email
import jwt
class Email:
def __init__(self):
self.__smtp_server = "smtp.gmail.com"
self.__port = 587
self.__context = ssl.create_default_context()
self.__sender_email = "sender email"
self.__sender_password = "sender email password"
self.__today_date = date.today().strftime("%d-%m-%y")
self.__token = {"some": "payload"}
def EmailValidator(self,email):
try:
validate_email(email)
return True
except:
return False
def send(self,**kwargs):
print(kwargs)
msg = MIMEMultipart()
receiver_email = kwargs["receiver_email"]
verificationLink = kwargs["verify_link"]
email = kwargs["email"]
number = kwargs["number"]
image_url = kwargs["image_url"]
print("Receiver Email :",receiver_email)
if(self.EmailValidator(receiver_email[0])):
msg["Subject"] = "Techyshivang Blog : Email Verify on {}".format(self.__today_date)
msg["From"] = self.__sender_email
msg['To'] = ", ".join(receiver_email)
text = """Welcome,this is a Verification Email for your complete Process."""
body_text = MIMEText(text, 'plain')
msg.attach(body_text)
# html = """\
# <html>
# <body>
# <br>
# Just Click om this Link {} \nor paste it over the browser to complete your sign up process in case of any
# difficulty please contact over this email {} or our customer support Number {} <br>
# Thank you. <br>
# <br>
# <br>
# Thanks & Regards<br>
# Shivang Saxena
# </p>
# </body>
# </html>
# """
html = open("verify.html")
# body_html = MIMEText(html.format(verificationLink,email,number), 'html')
body_html = MIMEText(html.format(verificationLink,verificationLink,verificationLink), 'html')
msg.attach(body_html)
# img_name = image_url.split("/")[6] # TODO: replace your image filepath/name
img_name = "index.webp"
with open(image_url, 'rb') as fp:
img = MIMEImage(fp.read())
img.add_header('Content-Disposition', 'attachment', filename=img_name)
msg.attach(img)
try:
server = smtplib.SMTP(self.__smtp_server, self.__port)
server.ehlo()
server.starttls(context=self.__context)
server.ehlo()
server.login(self.__sender_email, self.__sender_password)
# Send email here
server.sendmail(self.__sender_email,receiver_email,msg.as_string())
except Exception as e:
return {
"statusCode":500,
"message":"Email Not sent",
"error":e
}
finally:
server.quit()
return {
"statusCode":200,
"message":"verification email send successfully !!"
}
else:
return {
"statusCode":403,
"message":"Email is Not Valid !!"
}
def validateEmail(self,verify_link):
try:
token = verify_link
res = jwt.decode(token,"secret", algorithms=["HS256"])
return {
"statusCode":200,
"message":"Signature Valid !!",
"data":res
}
except Exception as e:
return {
"statusCode":403,
"message":"Signature InValid !!",
"error":e
}
email = Email()
response = email.send(
receiver_email=["receiver_email"],
email="[email protected]",
number="+91 XXXXXXXXXX",
verify_link="http://localhost:3000/verify/{}".format(jwt.encode({"some": "payload"}, "secret", algorithm="HS256").decode()),
image_url="image-url or save image name in the current working directory"
)
print(response)