3

I've gone through many SO posts and smtplib documentation, everything seems to be correct, but mail is sent to only first recipient in the list

Note: I'm using Python3.7, I've tried from Python 2.6 also, In below case mail is getting delivered only to very first recipient in receiver

Below is my code:

import smtplib
from email.mime.text import MIMEText


sender='from@domain.com'
receiver=['email1@domain.com', 'email2@domain.com', 'email3@domain.com']
msg = MIMEText("message")
msg['Subject'] = "Test Email"
msg['From'] = sender
msg['To'] = ",".join(receiver)

server = smtplib.SMTP("smtp.domain", 25)
sever.sendmail(sender, receiver, msg.as_string())
server.quit()
Rohit Nimmala
  • 1,459
  • 10
  • 28
  • 1
    Do `print(sever.sendmail(sender, receiver, msg.as_string()))` to see any recipients rejected by the SMTP host. –  Jul 27 '20 at 17:38
  • @JustinEzequiel it prints -> {} – Rohit Nimmala Jul 27 '20 at 17:46
  • Have you checked the SPAM folders of the recipients? –  Jul 27 '20 at 18:19
  • And have you tried adding a space after the comma? I.e., `msg['To'] = ", ".join(receiver)` –  Jul 27 '20 at 18:21
  • Yeah, no luck. Interesting part is, I wont get an email too if my email address is not placed in first position in the list. – Rohit Nimmala Jul 27 '20 at 18:21
  • @JustinEzequiel Yes, I tried putting a space after comma, that didnt work too – Rohit Nimmala Jul 27 '20 at 18:24
  • Perhaps a `server.set_debuglevel(2)` before the sendmail or send_message will shed some light. Ideally, you'd see a line like `send: 'rcpt TO:<...>` for each recipient followed by the server's response. –  Jul 27 '20 at 18:25
  • @JustinEzequiel I really appreciate your help, Let me try this. – Rohit Nimmala Jul 27 '20 at 18:38
  • @JustinEzequiel it shows that it is sending to all the recipients, surprisingly IamFr00st's answer seems to work – Rohit Nimmala Jul 27 '20 at 18:50
  • 1
    Then perhaps you've reassigned receiver to a string before your call to sever.sendmail. I'd do a `print(repr(receiver))` just above the call to sever.sendmail. –  Jul 27 '20 at 19:45

1 Answers1

4

Instead of

sever.sendmail(sender, receiver, msg.as_string())

use

server.send_message(msg)

SMTP.send_message() is a method for sending email.message.Message objects which will use the sender and receiver specified in the Message object. In your case that would be the variable msg (MIMEText is a subclass of Message).

I don't know why, I had a similar issue when using it the way you did. Probably because to_addrs are specified twice as as_string() adds it to the message body, what happens later I don't know.

to_addrs in SMTP.sendmail() is described as: "A list of addresses to send this mail to. A bare string will be treated as a list with 1 address.", so that was fine.

IamFr0ssT
  • 729
  • 5
  • 11
  • 4
    Why? Code-only responses are rarely helpful to the community in general. Remember your answers should be targeted not only the OP, but also towards others who may find it in the far future. You should add an explanation for _why_ your code fixes the problem. – Pranav Hosangadi Jul 27 '20 at 18:05
  • Thanks, this works. Surprisingly I still have no idea why sendmail doesn't work! – Rohit Nimmala Jul 27 '20 at 18:57
  • Did it work out for anybody? I still get only the first recipent using this answer – Alon212 Jan 01 '23 at 19:07