Send all returned data in the same email

Asked

Viewed 46 times

2

I have the following query:

cursor.execute("SELECT raddb.StockMinimo.Id, raddb.StockMinimo.Produto, Minimo, Quantidade FROM raddb.StockMinimo LEFT OUTER JOIN raddb.StockProdutos ON raddb.StockProdutos.Id = raddb.StockMinimo.Id WHERE raddb.StockMinimo.Identificacao = '3' AND raddb.StockMinimo.Ativo = '1' AND Quantidade < Minimo AND Minimo > '0'")
myresult = cursor.fetchall()

Which returns the following data;

Product id Minimum Quantity

93 Transparent 15mm Adhesive Tape (Units) 6 3

112 Carbon Pencil nº2 (Box C/ 12 Units) 10 6

160 Desk Staple Bag (Pcs) 3 1

Then I do the for to return the results:

for linha in myresult:
 Produto = linha[1]
 Minimo = linha[2]
 Quantidade = linha[3]

Send the email as follows:

texto        = 'Os seguinte produtos encontram-se com quantidade de stock igual ou inferior ao stock minimo. {} ({}) ({})'.format(
        Produto.encode("utf-8"), Quantidade, Minimo)

The problem is that it sends email for each line that returns from the database. As it returns 3 lines sends 3 emails. I wanted to send all the lines in the same email.

Complete code:

myresult = cursor.fetchall()

for linha in myresult:
 Produto = linha[1]
 Minimo = linha[2]
 Quantidade = linha[3]

 if Quantidade <= Minimo:
   remetente    = '[email protected]'
   senha        = 'xxxxxxxxx'

   destinatario = ['[email protected]']
   assunto      = 'Stock Papelaria'
   texto        = 'Os seguinte produtos encontram-se com quantidade de stock igual ou inferior ao stock minimo. Produto: {} Quantidade: {} Minimo: {}'.format(
        Produto.encode("utf-8"), Quantidade, Minimo)

   msg = '\r\n'.join([
      'From: %s' % remetente,
      'To: %s' % destinatario,
      'Subject: %s' % assunto,
      '',
      '%s' % texto
   ])

   server = smtplib.SMTP('smtp.gmail.com:587')
   server.starttls()
   server.login(remetente,senha)
   server.sendmail(remetente, destinatario, msg)
   server.quit()
  • see how to concatenate strings

  • @Elton Nunes saw how to cancatenate the strings and I did it this way: teste = [Produto.encode("utf-8") + str(Quantidade) + str(Minimo)], but still send separate emails

  • @Elton Nunes and intended to send all the rows returned in the query in one email. I think the problem is not in the concatenation

  • concatenate with n between lines, will turn into a string but will keep the line break, a doubt in which moment you are sending the email? the construction of your script is not clear to me

  • @Elton Nunes added the full script to the question.

  • the code responsible for sending the email is inside the loop, it should be outside

  • @Elton Nunes put out, now just sends an email, but only sends the last line of the returned. I tried with this concatenation teste = [Produto.encode("utf-8") + str(Quantidade) + str(Minimo)], but only send in the email the last line

  • You have to work on the logic of concatenization

  • @Elton Nunes can set an example for me to understand?

Show 4 more comments

1 answer

2


super simplified

you’re doing it

for linha in banco
    if linha
        enviaEmail

you must do

variavel
for linha in banco
    if linha
        variavel concatena linha

enviaEmail

Browser other questions tagged

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