title: Unable To Stop Loop In Project Source Code
body: <p>I am building a custom email sender in Python, using sample csv data as input. I have finished most of the script, however the only issue I am having is in the last few lines of code. If you run the code (make sure to include your email/pwd) you will notice that custom emails get sent to the right recipients, however they receive multiple copies due to the loop. </p>
  
  <p>I have tried a simple break statement at the end and that just sends out the first email then stops. Also haven't been able to find a solution on google/youtube.</p>
  
  <pre><code>import datetime
  import smtplib
  import os
  from email.mime.multipart import MIMEMultipart
  from email.mime.text import MIMEText
  import pandas as pd
  
  host = "smtp.gmail.com"
  port = 587
  usr = os.environ.get("EMAIL_USER")
  pwd = os.environ.get("EMAIL_PASS")
  from_email = usr
  
  
  class MessageUser():
      user_details = []
      messages = []
      email_messages = []
      base_message = "Hi {name}!\n\nThank you for the purchase on {date}. We hope you are exited about using it. Just as a reminder the purchase total was ${total}. Have a great day!"
      def add_user(self, name, amount, email=None):
          name = name[0].upper() + name[1:].lower()
          amount = f"{amount}"
          detail = {
              "name": name,
              "amount": amount,
          }
          today = datetime.date.today()
          date_text = '{today.month}/{today.day}/{today.year}'.format(today=today)
          detail['date'] = date_text
          if email is not None:   # if email != None
              detail["email"] = email
          self.user_details.append(detail)
      def get_details(self):
          return self.user_details
      def make_messages(self):
          if len(self.user_details) &gt; 0:
              for detail in self.get_details():
                  name = detail["name"]
                  amount = detail["amount"]
                  date = detail["date"]
                  message = self.base_message
                  new_msg = message.format(
                      name=name,
                      date=date,
                      total=amount
                  )
                  user_email = detail.get("email")
                  if user_email:
                      user_data = {
                          "email": user_email,
                          "message": new_msg
                      }
                      self.email_messages.append(user_data)
                  else:
                      self.messages.append(new_msg)
              return self.messages
          return []
      def send_email(self):
          self.make_messages()
          if len(self.email_messages) &gt; 0:
              for detail in self.email_messages:
                  user_email = detail['email']
                  user_message = detail['message']
                  try:
                      email_conn = smtplib.SMTP(host, port)
                      email_conn.ehlo()
                      email_conn.starttls()
                      email_conn.login(usr, pwd)
                      the_msg = MIMEMultipart("alternative")
                      the_msg["Subject"] = "Billing Update"
                      the_msg["From"] = from_email
                      the_msg["To"] = user_email
                      part_1 = MIMEText(user_message, "plain")
                      the_msg.attach(part_1)
                      email_conn.sendmail(from_email, [user_email], the_msg.as_string())
                  except smtplib.SMTPException:
                      print("Error sending message.")
              return True
          return False
  
  df = pd.read_csv('test_data.csv')
  
  Name = df.Name
  Amount = df.Price
  Email = df.Email
  
  for name, amount, email in zip(Name, Amount, Email):
      obj = MessageUser()
      obj.add_user(f'{name}', f'{amount}', email=f'{email}')
      obj.get_details()
      obj.send_email()
  
  # Works but need loop break suggestion.
  </code></pre>
  
  <pre><code># Previous attempt with error. email.errors.HeaderParseError: header value appears to contain an embedded header.
  
  name = df.Name[1:]
  amount = df.Price[1:]
  email = df.Email[1:]
  
  obj = MessageUser()
  obj.add_user(f'{name}', f'{amount}', email=f'{email}')
  obj.get_details()
  obj.send_email()
  </code></pre>
  
  <p>Expected: Send custom emails to all recipients once.
  Actual: Custom emails are received multiple times due to ending loop.</p>