Home >Backend Development >Python Tutorial >How to Send Emails to Multiple Recipients Using Python\'s smtplib?

How to Send Emails to Multiple Recipients Using Python\'s smtplib?

DDD
DDDOriginal
2024-10-27 08:37:311013browse

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&#160;protected]'
recipients = ['[email&#160;protected]', '[email&#160;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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn