Spaces:
Running
Running
pragnakalp
commited on
Commit
•
4304f78
1
Parent(s):
ed4d37e
Upload send_email_user.py
Browse files- send_email_user.py +71 -0
send_email_user.py
ADDED
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import sendgrid
|
3 |
+
from sendgrid import SendGridAPIClient
|
4 |
+
from sendgrid.helpers.mail import Mail
|
5 |
+
from email.mime.application import MIMEApplication
|
6 |
+
from email.mime.multipart import MIMEMultipart
|
7 |
+
from email.mime.text import MIMEText
|
8 |
+
from email.mime.image import MIMEImage
|
9 |
+
import smtplib, ssl
|
10 |
+
|
11 |
+
fromadd = 'pragnakalp.dev35@gmail.com'
|
12 |
+
password = 'pragnakalpl20$123'
|
13 |
+
SENDGRID_API_KEY = "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"
|
14 |
+
|
15 |
+
def gmail_login():
|
16 |
+
try:
|
17 |
+
server = smtplib.SMTP_SSL('smtp.sendgrid.net', 465)
|
18 |
+
server.ehlo()
|
19 |
+
server.login('apikey', SENDGRID_API_KEY)
|
20 |
+
return server
|
21 |
+
except Exception as e:
|
22 |
+
print("*** MAIL ERROR ***"*10)
|
23 |
+
print(e)
|
24 |
+
|
25 |
+
def send_user_email(input_img,hostname,text_output,Method):
|
26 |
+
|
27 |
+
try:
|
28 |
+
attachment = input_img
|
29 |
+
email_body=""
|
30 |
+
text =f"A user has submitted a image for OCR.\r\n\r\{hostname}\rMethod: {Method}\r\Result: {text_output}"
|
31 |
+
html = """<html><body><img src='flagged/img/0.jpg' border='0' /><br /></body></html>"""
|
32 |
+
html_body = f"""<html>
|
33 |
+
<body>
|
34 |
+
<p>
|
35 |
+
{hostname}<br/>
|
36 |
+
Method: {Method}<br/>
|
37 |
+
Generated text: {text_output}<br/>
|
38 |
+
</p>
|
39 |
+
<br />
|
40 |
+
</body>
|
41 |
+
</html>"""
|
42 |
+
|
43 |
+
toadd = 'hetvi199808@gmail.com'
|
44 |
+
subject = "OCR generated text"
|
45 |
+
|
46 |
+
msg = MIMEMultipart("alternative")
|
47 |
+
|
48 |
+
msg['From'] = fromadd
|
49 |
+
msg['To'] = toadd
|
50 |
+
msg['Subject'] = subject
|
51 |
+
# part1 = MIMEText(text, "plain")
|
52 |
+
# part2 = MIMEText(html_body, "html")
|
53 |
+
# msg.attach(part1)
|
54 |
+
part2 = MIMEText('<b>%s</b><br/><img src="cid:%s"/><br/>' % (html_body, attachment), 'html')
|
55 |
+
msg.attach(part2)
|
56 |
+
with open(attachment, 'rb') as fp:
|
57 |
+
img = MIMEImage(fp.read())
|
58 |
+
img.add_header('Content-ID', '<{}>'.format(attachment))
|
59 |
+
msg.attach(img)
|
60 |
+
|
61 |
+
server = gmail_login()
|
62 |
+
print("login success")
|
63 |
+
temp = server.sendmail(fromadd, toadd, msg.as_string())
|
64 |
+
print("temp=.", temp)
|
65 |
+
print("email sent to", toadd)
|
66 |
+
|
67 |
+
print("** Mail Successfully Sent !***")
|
68 |
+
return True
|
69 |
+
except Exception as e:
|
70 |
+
print("Error while sending feedback email => ", e)
|
71 |
+
return False
|