There is a little problem with sending emails to multiple recipients in python.
高洛峰
高洛峰 2017-05-18 10:50:26
0
2
879

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)

高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

reply all(2)
滿天的星座

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

给我你的怀抱

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

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template