Home >Backend Development >Python Tutorial >How to Send Emails to Multiple Recipients Using Python\'s smtplib?
Sending Emails to Multiple Recipients with Python's smtplib
When attempting to utilize smtplib.sendmail to deliver emails to several recipients, developers often face challenges. While the mail headers may indicate that multiple addresses are included, the message is only received by the first recipient.
This issue arises from the disparity between the format required by the email.Message module and the smtplib.sendmail() function. To successfully send emails to multiple recipients, it is crucial to set the header as a string comprised of comma-separated email addresses. However, the to_addrs parameter in sendmail() expects a list of email addresses.
An example that illustrates this approach is provided below:
<code class="python">import smtplib from email.mime.text import MIMEText s = smtplib.SMTP('smtp.uk.xensource.com') s.set_debuglevel(1) msg = MIMEText("""body""") sender = '[email protected]' recipients = ['[email protected]', '[email protected]'] msg['Subject'] = "subject line" msg['From'] = sender msg['To'] = ", ".join(recipients) s.sendmail(sender, recipients, msg.as_string())</code>
By following this approach, emails can be successfully sent to multiple recipients using the smtplib library in Python.
The above is the detailed content of How to Send Emails to Multiple Recipients Using Python\'s smtplib?. For more information, please follow other related articles on the PHP Chinese website!