Posts: 6
Threads: 1
Joined: Jul 2020
Hi,
I'm a newbie here. I'm looking for help to automatically send an email to an address assoicated with a string found in an excel spreadsheet.
I've a camera that takes an image of a number plate, then using pytesseract it reads the number plate characters from the image, searches an excel sheet for the characters and if found, send an email to an address associated with that number plate. So the code successfully takes the image, identifies the characters however I cannot seem to get it send the email. Below is the code I'm using, any help would be great :) Thanks
#Read the number plate
text = pytesseract.image_to_string(Cropped, config='--psm 11')
print("Detected Number is:", text)
cv2.imshow('Number Plate',img)
cv2.imshow('Zoomed Image',Cropped)
cv2.waitKey(5000)
cv2.destroyAllWindows()
loc = ("NumberPlateList.xlsx")
wb = xlrd.open_workbook(loc)
sheet = wb.sheet_by_index(0)
sheet.cell_value(0, 0)
scannedNumberPlateNumber = (text)
for i in range(sheet.nrows):
if sheet.cell_value(i, 0) == scannedNumberPlateNumber:
print('Found in column A - ignore')
if sheet.cell_value(i, 2) == scannedNumberPlateNumber:
print('Found in column C - send email to ' + sheet.cell_value(i, 3) )
def send_an_email():
toaddr = (+ sheet.cell_value(i, 3)) # To id
me = (+ sheet.cell_value(i, 3)) # your id
subject = "Your car is illegaly parked. PLEASE MOVE IMMEDIATELY" # Subject
msg = MIMEMultipart()
msg['Subject'] = subject
msg['From'] = me
msg['To'] = toaddr
msg.preamble = "test "
#msg.attach(MIMEText(text))
part = MIMEBase('application', "octet-stream")
part.set_payload(open("image.jpg", "rb").read())
encoders.encode_base64(part)
part.add_header('Content-Disposition', 'attachment; filename="image.jpg"') # File name and format name
msg.attach(part)
try:
s = smtplib.SMTP('smtp.gmail.com', 587) # Protocol
s.ehlo()
s.starttls()
s.ehlo()
s.login(user = 'XXXXXXXX', password = 'XXXXXX') # User id & password
#s.send_message(msg)
s.sendmail(me, toaddr, msg.as_string())
s.quit()
#except:
# print ("Error: unable to send email")
except SMTPException as error:
print ("Error") # Exception
Posts: 24
Threads: 0
Joined: Jun 2020
Does it throw an error? - If not then have you changed your gmail settings to allow less secure apps to access your account?
Posts: 6
Threads: 1
Joined: Jul 2020
Hi HarleyQuin,
No it doesn't throw an error however I had already changed the gmail settings to allow less secure apps to access my account.
Posts: 24
Threads: 0
Joined: Jun 2020
Jul-12-2020, 02:03 PM
(This post was last modified: Jul-12-2020, 02:04 PM by HarleyQuin.)
Hey,
Okay hmm. I use a module called email.message with smtp to send my emails so i don't think i can offer you any further advice on your current mailing method, sorry! That being said. here is the email module i use maybe you'd consider incorporating that if you don't get an answer back soon.
from email.message import EmailMessage
import smtplib
EMAIL_ADDRESS = "SENDER_EMAIL"
EMAIL_PASSWORD = "EMAIL_PASSWORD"
msg = EmailMessage()
msg['Subject'] = 'Email Subject'
msg['From'] = EMAIL_ADDRESS
msg['To'] = "Receiver Email"
msg.set_content("BODY OF EMAIL HERE")
with smtplib.SMTP_SSL('smtp.gmail.com', 465) as smtp:
smtp.login(EMAIL_ADDRESS, EMAIL_PASSWORD)
smtp.send_message(msg)
Posts: 6
Threads: 1
Joined: Jul 2020
I appreciate you taking the time to have a look so thank you!!
Just as an fyi.....before I added in the section below, it would successfuly email a given address. The section below seems to have an adverse affect however I do require the email address to be sourced from an excel file.
loc = ("NumberPlateList.xlsx")
wb = xlrd.open_workbook(loc)
sheet = wb.sheet_by_index(0)
sheet.cell_value(0, 0)
scannedNumberPlateNumber = ('202-G-5679')
for i in range(sheet.nrows):
if sheet.cell_value(i, 0) == scannedNumberPlateNumber:
print('Found in column A - ignore')
if sheet.cell_value(i, 2) == scannedNumberPlateNumber:
print('Found in column C - send email to ' + sheet.cell_value(i, 3) )
Posts: 24
Threads: 0
Joined: Jun 2020
(Jul-12-2020, 02:41 PM)aidanh26 Wrote: I appreciate you taking the time to have a look so thank you!!
Just as an fyi.....before I added in the section below, it would successfuly email a given address. The section below seems to have an adverse affect however I do require the email address to be sourced from an excel file.
loc = ("NumberPlateList.xlsx")
wb = xlrd.open_workbook(loc)
sheet = wb.sheet_by_index(0)
sheet.cell_value(0, 0)
scannedNumberPlateNumber = ('202-G-5679')
for i in range(sheet.nrows):
if sheet.cell_value(i, 0) == scannedNumberPlateNumber:
print('Found in column A - ignore')
if sheet.cell_value(i, 2) == scannedNumberPlateNumber:
print('Found in column C - send email to ' + sheet.cell_value(i, 3) )
Hmm that is interesting. Can you send me the entire code similar to the first submission (obvs. not including credentials) but including calls to functions etc?
I'll see if i can debug,
Harley
Posts: 6
Threads: 1
Joined: Jul 2020
(Jul-12-2020, 03:48 PM)HarleyQuin Wrote: Hmm that is interesting. Can you send me the entire code similar to the first submission (obvs. not including credentials) but including calls to functions etc?
Thank you. Email sent.
Posts: 24
Threads: 0
Joined: Jun 2020
Hey,
So just a quick one before we get started:
EMAIL_ADDRESS = "*********@gmail.com"
EMAIL_PASSWORD = "XXXX"
msg = EmailMessage()
msg['Subject'] = 'Email Subject'
msg['From'] = '"*********@gmail.com'
msg['To'] = sheet.cell_value(i, 3) # I replaced the 'To' email address here as I need the code to send email to the address from excel file msg.set_content("BODY OF EMAIL HERE")
with smtplib.SMTP_SSL('smtp.gmail.com', 465) as smtp:
smtp.login(EMAIL_ADDRESS, EMAIL_PASSWORD)
smtp.send_message(msg) Is this ^ your exact code. Because as below:
EMAIL_ADDRESS = ""*********@gmail.com"
EMAIL_PASSWORD = "XXXX"
msg = EmailMessage()
msg['Subject'] = 'Email Subject'
msg['From'] = '"*********@gmail.com'
msg['To'] = sheet.cell_value(i, 3) # I replaced the 'To' email address here as I need the code to send email to the address from excel file
msg.set_content("BODY OF EMAIL HERE")
with smtplib.SMTP_SSL('smtp.gmail.com', 465) as smtp:
smtp.login(EMAIL_ADDRESS, EMAIL_PASSWORD)
smtp.send_message(msg) "msg.set_content("BODY OF EMAIL HERE")" needs to be on its own line. ^
Also: Comment out everything other than the email function to make sure the emailing works, then introduce the rest of the code and then we can go from there :-)
Regards,
Harley
Posts: 6
Threads: 1
Joined: Jul 2020
(Jul-12-2020, 04:44 PM)HarleyQuin Wrote: Hey,
So just a quick one before we get started:
EMAIL_ADDRESS = "*********@gmail.com"
EMAIL_PASSWORD = "XXXX"
msg = EmailMessage()
msg['Subject'] = 'Email Subject'
msg['From'] = '"*********@gmail.com'
msg['To'] = sheet.cell_value(i, 3) # I replaced the 'To' email address here as I need the code to send email to the address from excel file msg.set_content("BODY OF EMAIL HERE")
with smtplib.SMTP_SSL('smtp.gmail.com', 465) as smtp:
smtp.login(EMAIL_ADDRESS, EMAIL_PASSWORD)
smtp.send_message(msg) Is this ^ your exact code. Because as below:
EMAIL_ADDRESS = ""*********@gmail.com"
EMAIL_PASSWORD = "XXXX"
msg = EmailMessage()
msg['Subject'] = 'Email Subject'
msg['From'] = '"*********@gmail.com'
msg['To'] = sheet.cell_value(i, 3) # I replaced the 'To' email address here as I need the code to send email to the address from excel file
msg.set_content("BODY OF EMAIL HERE")
with smtplib.SMTP_SSL('smtp.gmail.com', 465) as smtp:
smtp.login(EMAIL_ADDRESS, EMAIL_PASSWORD)
smtp.send_message(msg) "msg.set_content("BODY OF EMAIL HERE")" needs to be on its own line. ^
Also: Comment out everything other than the email function to make sure the emailing works, then introduce the rest of the code and then we can go from there :-)
Regards,
Harley (Jul-12-2020, 04:44 PM)HarleyQuin Wrote: "msg.set_content("BODY OF EMAIL HERE")" needs to be on its own line. ^
(Jul-12-2020, 04:44 PM)HarleyQuin Wrote: "msg.set_content("BODY OF EMAIL HERE")" needs to be on its own line. ^
Hi,
That was just a copy and paste error from copying it into here. Its correct in my code.
Posts: 24
Threads: 0
Joined: Jun 2020
Jul-13-2020, 09:00 AM
(This post was last modified: Jul-13-2020, 09:01 AM by HarleyQuin.)
That makes sense, i did think that might be the case.
Will the email send if you remove the excess code? if so..
try setting:
sheet.cell_value(i, 3) as:
some_variable = str(sheet.cell_value(i, 3))
msg.set_content(some_variable) Then pass it to the email program because your code seems fine to be honest.
|