Home > Back-end >  Issue with the .join command from smtplib
Issue with the .join command from smtplib

Time:06-26

I am trying to send emails to multiple recipients using the .join command from the smtp library. When I run the python code, the email sends and works however it only sends an email to the first address under the RECIPIENT_ADDRESS rather than all of the ones listed. How can I fix this? Thank you so much. I will add the code below.

Python Code

        MY_ADDRESS = "*****@gmail.com"         # Replace with yours
        MY_PASSWORD = "*****"      # Replace with yours
        RECIPIENT_ADDRESS = ['*****@gmail.com', '*****@gmail.com']  # Replace with yours

        HOST_ADDRESS = 'smtp.gmail.com'   # Replace with yours
        HOST_PORT = 587                          # Replace with yours


        import smtplib
        from email.utils import formataddr
        from email.mime.multipart import MIMEMultipart
        from email.mime.application import MIMEApplication
        from email.mime.text import MIMEText


        # Connection with the server
        server = smtplib.SMTP(host=HOST_ADDRESS, port=HOST_PORT)
        server.starttls()
        server.login(MY_ADDRESS, MY_PASSWORD)

        # Creation of the MIMEMultipart Object
        message = MIMEMultipart()

        # Setup of MIMEMultipart Object Header
        message['From'] = formataddr(('Management', '*****@gmail.com'))
        message['To'] = ", ".join(RECIPIENT_ADDRESS)
        message['Subject'] = "Test Bill - July"

        # Creation of a MIMEText Part
        textPart = MIMEText("Hello John & Johnny,\n\nAttached below is your rent bill for the month of July 2022. To receive your rent bill with a physical copy please email back requesting for us to do so. Otherwise, you will receive your rent bill through email. If you pay online and have scheduled a payment then just double check that the amount paid covers the amount due in the bill you receive.\n\nBest,\ Management", 'plain')

        # Creation of a MIMEApplication Part
        filename = "Test Bill - John.pdf"
        filePart = MIMEApplication(open(filename,"rb").read(),Name=filename)
        filePart["Content-Disposition"] = 'attachment; filename="%s' % filename

        # Parts attachment
        message.attach(textPart)
        message.attach(filePart)

        # Send Email and close connection
        smtp_obj = smtplib.SMTP_SSL("smtp.gmail.com", 465)
        smtp_obj.login(MY_ADDRESS, MY_PASSWORD)
        smtp_obj.sendmail(message['From'], message['To'], message.as_string())
        smtp_obj.quit()

CodePudding user response:

Try using a for loop:

MY_ADDRESS = "*****@gmail.com"         # Replace with yours
MY_PASSWORD = "*****"      # Replace with yours
RECIPIENT_ADDRESS = ['*****@gmail.com', '*****@gmail.com']  # Replace with yours

HOST_ADDRESS = 'smtp.gmail.com'   # Replace with yours
HOST_PORT = 587                          # Replace with yours


import smtplib
from email.utils import formataddr
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication
from email.mime.text import MIMEText


        # Connection with the server
server = smtplib.SMTP(host=HOST_ADDRESS, port=HOST_PORT)
server.starttls()
server.login(MY_ADDRESS, MY_PASSWORD)

        # Creation of the MIMEMultipart Object
message = MIMEMultipart()

        # Setup of MIMEMultipart Object Header
message['From'] = formataddr(('Management', '*****@gmail.com'))

message['Subject'] = "Test Bill - July"

        # Creation of a MIMEText Part
textPart = MIMEText("Hello John & Johnny,\n\nAttached below is your rent bill for the month of July 2022. To receive your rent bill with a physical copy please email back requesting for us to do so. Otherwise, you will receive your rent bill through email. If you pay online and have scheduled a payment then just double check that the amount paid covers the amount due in the bill you receive.\n\nBest,\ Management", 'plain')

        # Creation of a MIMEApplication Part
filename = "Test Bill - John.pdf"
filePart = MIMEApplication(open(filename,"rb").read(),Name=filename)
filePart["Content-Disposition"] = 'attachment; filename="%s' % filename

        # Parts attachment
message.attach(textPart)
message.attach(filePart)

        # Send Email and close connection
smtp_obj = smtplib.SMTP_SSL("smtp.gmail.com", 465)
smtp_obj.login(MY_ADDRESS, MY_PASSWORD)
        
for email in RECIPIENT_ADDRESS:
        message['To'] = ", ".join(email)
        smtp_obj.sendmail(message['From'], message['To'], message.as_string())
smtp_obj.quit()

CodePudding user response:

The solution to this issue is to replace the message['To'] section with the recipient emails Ex. message['To'] = '[email protected], [email protected]' and then replace the sendmail(message['To']) with sendmail(message['To'].split(',')). So the full line of code for the second section would be smtp_obj.sendmail(message['From'], message['To'].split(','), message.as_string()).

  • Related