File size: 2,461 Bytes
4304f78
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import sendgrid
from sendgrid import SendGridAPIClient
from sendgrid.helpers.mail import Mail
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
import smtplib, ssl

fromadd = 'pragnakalp.dev35@gmail.com'
password = 'pragnakalpl20$123'
SENDGRID_API_KEY = "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"

def gmail_login():
    try:
        server = smtplib.SMTP_SSL('smtp.sendgrid.net', 465)
        server.ehlo()
        server.login('apikey', SENDGRID_API_KEY)
        return server
    except Exception as e:
        print("*** MAIL ERROR ***"*10)
        print(e)

def send_user_email(input_img,hostname,text_output,Method):

    try:
        attachment = input_img
        email_body=""
        text =f"A user has submitted a image for OCR.\r\n\r\{hostname}\rMethod: {Method}\r\Result: {text_output}"
        html = """<html><body><img src='flagged/img/0.jpg' border='0' /><br /></body></html>"""
        html_body = f"""<html>
                        <body>
                            <p>
                                {hostname}<br/>
                                Method: {Method}<br/>
                                Generated text: {text_output}<br/>
                            </p>
                            <br />
                        </body>
                    </html>"""

        toadd = 'hetvi199808@gmail.com'
        subject = "OCR generated text"

        msg = MIMEMultipart("alternative")

        msg['From'] = fromadd
        msg['To'] = toadd
        msg['Subject'] = subject
        # part1 = MIMEText(text, "plain")
        # part2 = MIMEText(html_body, "html")
        # msg.attach(part1)
        part2 = MIMEText('<b>%s</b><br/><img src="cid:%s"/><br/>' % (html_body, attachment), 'html')   
        msg.attach(part2)
        with open(attachment, 'rb') as fp:
            img = MIMEImage(fp.read())
        img.add_header('Content-ID', '<{}>'.format(attachment))
        msg.attach(img)
        
        server = gmail_login()
        print("login success")
        temp = server.sendmail(fromadd, toadd, msg.as_string())
        print("temp=.", temp)
        print("email sent to", toadd)

        print("** Mail Successfully Sent !***")
        return True
    except Exception as e:
        print("Error while sending feedback email => ", e)
        return False