简体   繁体   中英

Using Python smtplib to send to distribution lists

I'm using SMTPlib to automatically send out an email:

emailto = ['distro@email.com','me@email.com']
emailfrom = "me@email.com"

msg = MIMEMultipart('related')
msg['Subject'] = currentdate + " Subject"
msg['From'] = emailfrom
msg['To'] = ", ".join(emailto)  

msgAlternative = MIMEMultipart('alternative')
msg.attach(msgAlternative)
msgAlternative.attach(msgText)

smtpObj = smtplib.SMTP('mail.email.com')
smtpObj.ehlo()
smtpObj.starttls()
smtpObj.sendmail(emailfrom, emailto, msg.as_string())
smtpObj.quit()

When I use this code I get the email, with distro@email.com in the "To:" line as well, but no one in distro@email.com gets it. I've sent to distribution lists before with no problem, but this specific one will not work. It is a fairly large list (~100 recipients)

Errors may pass unnoticed, because you are not checking the result of .sendmail() when sending to multiple addresses.

This method will not raise an exception if it succeeds to send the email to at least one recipient.

The important part of the docs is:

This method will return normally if the mail is accepted for at least one recipient. Otherwise it will raise an exception. If this method does not raise an exception, it returns a dictionary, with one entry for each recipient that was refused. Each entry contains a tuple of the SMTP error code and the accompanying error message sent by the server.

Something like this should help to find the problem:

errors = smtpObj.sendmail(emailfrom, emailto, msg.as_string())
for recipient, (code, errmsg) in errors.items():
    # ... print or log the error ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM