title: Sending multiple emails from dataframe values
body: <p>First-timer here!  My Python code is supposed to run an SQL query, write the results to a CSV, convert the CSV to a dataframe and use values in the dataframe to compose and send an email.  The email addresses are a list in the dataframe, and delimited by a comma.</p>
  <pre><code>first_name  middle_name  last_name    email                           pref_lang  exp_date      p_id
  Joe         A            Smith        jas@gmail.com                   eng        Aug 22, 2022  001
  NaN         NaN          Joe A Smith  jas@business.org                cht        Dec 03, 2030  002
  NaN         NaN          Joe A Smith  jas@gmail.com,jas@business.org  eng        Oct 16, 2024  003
  </code></pre>
  <p>Everything works ok but the emails are only being sent to the first address in the list.  The second address appears in the &quot;to&quot; when the email is received but mailtraces on the SMTP server fail to find any email sent to the second address.</p>
  <p>I've searched high and low on this site and others for a similar issue and can't find a solution.  What should I do differently to send the email to multiple addresses in the same row?</p>
  <pre><code>import psycopg2
  import csv
  import pandas
  import itertools
  import operator
  import smtplib
  from email.mime.multipart import MIMEMultipart
  from email.mime.base import MIMEBase
  from email.mime.text import MIMEText
  from email.mime.image import MIMEImage
  from email.utils import formatdate
  from email import encoders
  from datetime import date
  from dateutil.relativedelta import relativedelta
  
  try:
      conn = psycopg2.connect(&quot;dbname='' user='' host='' port='' password='' sslmode=''&quot;)
  except psycopg2.Error as e:
      print (&quot;Unable to connect to database: &quot; + str(e))
  
  cursor = conn.cursor()
  cursor.execute(open(&quot;query.sql&quot;,&quot;r&quot;).read())
  
  key_func = operator.itemgetter(0, 1)
  
  with open('file.csv', mode='w', newline='') as csv_file:
      writer = csv.writer(csv_file, delimiter=',')
      writer.writerow([ i[0] for i in cursor.description ])
      writer.writerows(cursor.fetchall())
  
  conn.close()
  
  df = pandas.read_csv('file.csv')
  
  def sendemail(html, to_email):
      emailhost = ''
      emailport = ''
      emailfrom= ''
      emailsubject = 'Subject'
      emailto = [str(row[3])]
      text = '''Dear {} {} {}, somethng happened and I want to tell you about it: {}. Thank You.'''.format(str(row[0]),str(row[1]),str(row[2]),str(row[5]))
      smtp = smtplib.SMTP(emailhost, emailport)
      msg = MIMEMultipart('alternative')
      part1 = MIMEText(text,'plain')
      msg['From'] = emailfrom
      if type(emailto) is list:
          msg['To'] = ', '.join(emailto)
      else:
          msg['To'] = emailto
      msg['Date'] = formatdate(localtime = True)
      msg['Subject'] = emailsubject
      msg.attach (part1)
      server = smtplib.SMTP(emailhost, emailport)
      server.sendmail(emailfrom, emailto, msg.as_string())
      server.quit()
  
  for row in df.itertuples(index=False):
      sendemail(df.to_html(index=False), df['email'])
  </code></pre>