Sending Email to Multiple Recipients using Python smtplib
When attempting to utilize Python's smtplib.sendmail to email multiple recipients, users often encounter issues. Despite specifying multiple addresses in the email header, only the first recipient receives the message.
This arises from a disparity in formatting expectations between the email.Message module and the smtplib.sendmail() function. The email.Message module accepts comma-delimited email addresses in the header, while sendmail() requires a list of addresses.
To effectively send email to multiple recipients using smtplib.sendmail, follow these steps:
Example code to send an email to multiple recipients using smtplib.sendmail:
<code class="python">from email.MIMEMultipart import MIMEMultipart from email.MIMEText import MIMEText import smtplib msg = MIMEMultipart() msg["Subject"] = "Example" msg["From"] = "sender@example.com" msg["To"] = "recipient1@example.com,recipient2@example.com,recipient3@example.com" msg["Cc"] = "cc1@example.com,cc2@example.com" body = MIMEText("example email body") msg.attach(body) smtp = smtplib.SMTP("mailhost.example.com", 25) smtp.sendmail(msg["From"], msg["To"].split(",") + msg["Cc"].split(","), msg.as_string()) smtp.quit()</code>
The above is the detailed content of How to Send Emails to Multiple Recipients Using Python\'s smtplib.sendmail()?. For more information, please follow other related articles on the PHP Chinese website!