在Python中拆分的麻烦

时间:2017-12-11 03:55:14

标签: python email

我正在尝试在python 2.7中创建一个脚本,向一些数据存储在文本文件中的人发送电子邮件。

import smtplib
email = raw_input("Your Gmail: ")
password = raw_input("Your Gmail Password: ")
txtlist = raw_input(".txt file of receiver emails: ")
content = raw_input("Content of your email: ")
txt = open(txtlist, 'r')
read = txt.read()
read.split(",")
txt.close()
server = smtplib.SMTP('smtp.gmail.com',587)
server.ehlo()
server.starttls()
server.login(email, password)
server.sendmail(email, read, content)
server.close()

当我运行脚本时,我希望它向文本文件中列出的每个人发送电子邮件。 当我运行脚本时,只会将电子邮件发送给列表中的第一个人。请帮忙,谢谢!

2 个答案:

答案 0 :(得分:1)

替换

read.split(",")

read = read.split(",")

这是示例(来自文档)

 >>> import smtplib
 >>> s=smtplib.SMTP("localhost")
 >>> tolist=    ["one@one.org","two@two.org","three@three.org","four@four.org"]
 >>> msg = '''\
 ... From: Me@my.org
 ... Subject: testin'...
 ...
 ... This is a test '''
 >>> s.sendmail("me@my.org",tolist,msg)
 { "three@three.org" : ( 550 ,"User unknown" ) }
 >>> s.quit()

答案 1 :(得分:0)

正如ShpielMeister所说,你必须跟踪分裂的结果。 例如:

dummyString = "I like spllitting strings in Python"
dummyStringResults = dummyString.split(" ")

将生成['我','喜欢',' spllitting','字符串','' ,' Python']将存储在dummyStringResults中。