Home  >  Q&A  >  body text

There is a little problem with sending emails to multiple recipients in python.

I wanted to send emails to more than one user, so I wrote a sending method, and then read the file in a loop. I wanted the recipient to see that the only recipient in the email was myself, but the recipients still showed multiple recipients in the list. The first recipient in the file sees one, and the second recipient sees the first two as if they were appended to the list, but when to_addr is printed out in the call, there is only one address, not Multiple addresses.
I sent it later, and commented msg['To'] to avoid displaying the recipients, but I wanted to understand why multiple recipients were displayed.
Please help me analyze it.

#!/usr/bin/python
# -*- coding: UTF-8 -*-
import smtplib
import os
import socket
import commands
from email.mime.text import MIMEText
from email.header import Header
def send(a,b):
    user = 'xxxx'
    from_addr = 'xxxx@xxxx.com'
    password = '***********;'
    smtp_server = 'smtp.xxxx.com'
    to_addr = b
    print "##############"+to_addr
    msg['To'] = Header(to_addr, 'utf-8').encode()
    server = smtplib.SMTP(smtp_server, 25)
    server.set_debuglevel(1)
    server.login(user, password)
    print '**************'+to_addr
    server.sendmail(from_addr, to_addr, a.as_string())
    server.close()
if __name__ == '__main__':
            msg = MIMEText('内容文字内容文字', 'plain', 'utf-8')
            msg['Subject'] = Header(u'我是标题', 'utf-8').encode()
            file = open("sample.txt")
            #lines=file.readlines()
            for i in file:
                print i 
                print "##"
                q=send(msg,i)

高洛峰高洛峰2711 days ago829

reply all(2)I'll reply

  • 滿天的星座

    滿天的星座2017-05-18 10:52:26

    This should be merged and displayed by the mailbox itself. You should log in to the recipient's mailbox to see if the recipient list is the same, instead of looking at the sender

    reply
    0
  • 给我你的怀抱

    给我你的怀抱2017-05-18 10:52:26

    Try to take a nap? In each loop time.sleep(60)?

    reply
    0
  • Cancelreply