Python: Send e-mail to multiple recipients using Gmail SMTP

Below is the method I used.

def SendEmail(content):
    gmailUser = ‘me’
    password = ‘password’
    recipients = [‘you1’,
                ‘you2’,
                ‘you3’
                ]
    msg = MIMEMultipart(‘alternative’)
    
    msg[‘Subject’] = ‘Subject’
    msg[‘From’] = gmailUser
    msg[‘To’] = ‘, ‘.join(recipients)
    
    content = MIMEText(html, ‘html’)
    msg.attach(content)
    
    mailServer = smtplib.SMTP(‘smtp.gmail.com’, 587)
    mailServer.ehlo()
    mailServer.starttls()
    mailServer.ehlo()
    mailServer.login(gmailUser, password)
    mailServer.sendmail(sender, recipients, msg.as_string())
    mailServer.close()

Leave a Reply

Your email address will not be published. Required fields are marked *