• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
    • Questions
    • Teachers
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

134
Views
Send all data returned in the query in the same email

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:

Id Produto Minimo Quantidade

93 Fita Adesiva 15mm Transparente (Unid.) 6 3

112 Lápis De Carvão Nº2 (Caixa C/ 12 Unidades) 10 6

160 Saca Agrafes Para Secretária (Unid) 3 1

Then I do the for to return the results:

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

I 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 an email for each line that returns from the database. As it returns 3 lines it sends 3 emails. I intended to send all the lines returned in the query in the same email.

full code:

myresult = cursor.fetchall()

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

 if Quantidade <= Minimo:
   remetente    = 'xxxxxxxxxxx@gmail.com'
   senha        = 'xxxxxxxxx'

   destinatario = ['xxxxxxxx@123.pt']
   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()
about 3 years ago · Santiago Trujillo
1 answers
Answer question

0

First I was sending the email inside the for wrong.

After correcting this problem, I created the variable before the for and then within that variable I concatenated the variables returned in the for:

resultado = []

for linha in myresult:
 Produto = linha[1]
 Minimo = linha[2]
 Quantidade = linha[3]
 if Quantidade <= Minimo:
  resultado.append('Produto: ' + Produto.encode("utf-8") + '          Quantidade: ' + str(Quantidade) + '          Minimo: ' + str(Minimo))
  result = ''.join(resultado)
remetente    = 'xxxxxxxxxxx@gmail.com'
senha        = 'xxxxxxxxx'

destinatario = ['xxxxxxxx@123.pt']
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()
about 3 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error