title: Retrieving data from a DB and send it via mail
body: <p>I am creating a script that takes some data from a database and then it sends it via mail. Unfortunately, I am stuck with something that seems very easy to do.</p>
  
  <p>If there is only one entry from the DB it works perfectly (ex: 18/10/18 - Do math exercises) but if there are multiple entries they don't show up, I thought about using a while loop but as I have to keep all the entries in the same variable I can't get it working. I share the code below:</p>
  
  <pre><code># Enviar correu Python 3.6 (NO ANOMENAR EMAIL.PY)
  import smtplib, pymysql.cursors, secrets, re, datetime
  from email.mime.multipart import MIMEMultipart
  from email.mime.text import MIMEText
  from time import strftime
  
  def dbb():
      connection = pymysql.connect(host='X',
                                   user='root',
                                   password='X',
                                   db='deuresc',
                                   charset='utf8mb4',
                                   cursorclass=pymysql.cursors.DictCursor)
      try:
          with connection.cursor() as cursor:
              # Llegim tots els recordatoris
              dataactual = strftime('%Y-%m-%d')
              data = datetime.datetime.strptime(dataactual, "%Y-%m-%d")
              datafinal = data + datetime.timedelta(days=1)
              sql = "SELECT * FROM `deures` WHERE `data`=%s"
              cursor.execute(sql, datafinal)
              resultat = cursor.fetchall()
  
              for row in resultat:
                  data = str(row['data'])
                  print(data)
                  tasca = str(row['tasca'])
                  print(tasca)
                  # It prints all the values but it doesn't send them below.
              fromaddr = "X"
              PASSWORD = "X"
  
              welcome = ['Jefe', 'Gery', 'Boss', 'Gary']
  
              # Misstage del correu
              SUBJECT = "Recordatori deures " + data
              body = "Bon dia " + secrets.choice(welcome) + ',' +  '\n' + 'Tens les següents tasques programades per demà: ' + '\n' + data + ' ' + tasca
              toaddr = "X"
  
              try:
                  msg = MIMEMultipart()
                  msg['From'] = fromaddr
                  msg['To'] = toaddr
                  msg['Subject'] = SUBJECT
  
                  msg.attach(MIMEText(body, 'plain'))
  
                  # Detalles del servidor de correu (Gmail, SMTP, Port: 587, TLS)
                  server = smtplib.SMTP('smtp.gmail.com', 587)
                  server.starttls()
                  server.login(fromaddr, PASSWORD)
                  text = msg.as_string()
                  server.sendmail(fromaddr, toaddr, text)
                  server.quit()
                  print("S'ha enviat el recordatori a '" + toaddr + "' satisfactoriament!")
              except:
                  print("S'ha produït un error!")
      finally:
          connection.close()
  </code></pre>
  
  <p>Thanks in advance for your help,</p>