0
I am trying to send emails by Python with data from a database, but there is an error that I am not able to understand.
The system makes the connection to the bank, sends the first email and when will send the second email an error.
Could someone help me with this code please?
Follow the code I have:
#!/usr/bin/python
import MySQLdb
import smtplib
import time
smtp = smtplib.SMTP_SSL('email-ssl.com.br', 465)
email = '[email protected]'
senha = '1234'
# Abre o banco de dados
db = MySQLdb.connect("localhost","root","1234","intranet" )
# prepare a cursor object using cursor() method
cursor = db.cursor()
sql = " SELECT e.id,e.email_destino,e.titulo,e.mensagem,e.email_respondepara FROM envia_email e WHERE e.enviado = 'F'"
try:
# executa o SQL
cursor.execute(sql)
# lista a base.
results = cursor.fetchall()
for row in results:
#id = row[0]
email_destino = row[1]
titulo = row[2]
mensagem = row[3]
#email_respondepara = row[4]
time.sleep(40)
smtp.login(email,senha)
de = '[email protected]'
para = [email_destino]
msg = '''Subject: %s \n
%s''' % (titulo, ' ' + mensagem)
smtp.sendmail(de, para, msg)
smtp.quit()
except:
print "Error"
# desconecta do servidor
db.close()
Which error occurs?
– Andrey
it enters the except line and prints Error
– Giuliano
Remove Try .. except to identify the exception being generated. And all records returned by SQL are valid?
– Andrey
Remove Try/catch and see the error at the right sff
– Miguel
After removing Try, as directed by Andrey and Miguel, I understood the error better. The system was losing connection with SMTP. Thanks for the help!!
– Giuliano