Sending email with Python and Mysql

Asked

Viewed 163 times

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?

  • it enters the except line and prints Error

  • Remove Try .. except to identify the exception being generated. And all records returned by SQL are valid?

  • Remove Try/catch and see the error at the right sff

  • 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!!

1 answer

0

After the orientation of Andrey and Miguel I put the code and it was like this:

    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'"

    # executa o SQL
    cursor.execute(sql)
    # lista a base.
    results = cursor.fetchall()

    #loga no SMTP
    smtp.login(email,senha)

    for row in results:
        #id = row[0]
        email_destino = row[1]
        titulo = row[2]
        mensagem = row[3]
        #email_respondepara = row[4]

        #aguarda 20 segundo para enviar o proximo email
        time.sleep(20)

        de = email
        para = [email_destino]

        msg = '''Subject: %s \n
        %s''' % (titulo, ' ' + mensagem)
        #envia o e-mail
        smtp.sendmail(de, para, msg)
    #desconecta do SMTP
    smtp.quit()

    # desconecta do servidor
    db.close()

Browser other questions tagged

You are not signed in. Login or sign up in order to post.