您好我一直在尝试使用python发送电子邮件,但错误不断出现。这是我的剧本。
import smtplib
from string import Template
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
MY_ADDRESS = '*****@gmail.com'
PASSWORD = '*****'
def get_contacts(filename):
"""
Return two lists names, emails containing names and email addresses
read from a file specified by filename.
"""
names = []
emails = []
with open(filename, mode='r', encoding='utf-8') as contacts_file:
for a_contact in contacts_file:
names.append(a_contact.split()[0])
emails.append(a_contact.split()[1])
return names, emails
def read_template(filename):
"""
Returns a Template object comprising the contents of the
file specified by filename.
"""
with open(filename, 'r', encoding='utf-8') as template_file:
template_file_content = template_file.read()
return Template(template_file_content)
def main():
names, emails = get_contacts('mycontacts.txt') # read contacts
message_template = read_template('message.txt')
# set up the SMTP server
s = smtplib.SMTP('smtp.gmail.com', 587)
s.starttls()
s.login(MY_ADDRESS, PASSWORD)
# For each contact, send the email:
for name, email in zip(names, emails):
msg = MIMEMultipart() # create a message
# add in the actual person name to the message template
message = message_template.substitute(PERSON_NAME=name.title())
# Prints out the message body for our sake
print(message)
# setup the parameters of the message
msg['From'] = MY_ADDRESS
msg['To'] = email
msg['Subject'] = "This is TEST"
# add in the message body
msg.attach(MIMEText(message, 'plain'))
# send the message via the server set up earlier.
s.send_message(msg)
del msg
# Terminate the SMTP session and close the connection
s.quit()
if __name__ == '__main__':
main()
Traceback (most recent call last):
File "C:/Users/tyger_000/Desktop/Object Oriented Programming/Project/Private-Group-Work-OOPP/SmartLifestyle(Combined)/Email Sender/email_sender.py", line 75, in <module>
main()
File "C:/Users/tyger_000/Desktop/Object Oriented Programming/Project/Private-Group-Work-OOPP/SmartLifestyle(Combined)/Email Sender/email_sender.py", line 53, in main
message = message_template.substitute(PERSON_NAME=name.title())
File "C:\Program Files\Python35\lib\string.py", line 125, in substitute
return self.pattern.sub(convert, self.template)
File "C:\Program Files\Python35\lib\string.py", line 122, in convert
self._invalid(mo)
File "C:\Program Files\Python35\lib\string.py", line 95, in _invalid
(lineno, colno))
ValueError: Invalid placeholder in string: line 1, col 17
这是mycontacts.txt文件
user@computer ~ $ cat mycontacts.txt
john john@gmail.com
这是我的message.txt
user@computer ~ $ cat message.txt
Dear ${PERSON_NAME},
Brace Yourselves,
Haze is coming!
Yours Truly
我不知道出了什么问题,我是新用户。请帮忙。谢谢你。最初的错误是目标机器拒绝但在解决之后,这发生了。我是否需要安装电子邮件包或类似的东西?如果是,我该怎么办?
答案 0 :(得分:0)
从tempate中取出$
符号
Dear {PERSON_NAME},
答案 1 :(得分:0)
从tempate中取出$
符号
Dear {PERSON_NAME},
或者如果模块较新,可以使用双花括号{{}}:
Dear {{PERSON_NAME}},
答案 2 :(得分:0)
您的脚本和 txt 文件中没有任何问题,它在我的系统上正常运行!! 请尝试在不同的文件或其他系统中再次运行。或者你的python lib文件可能有问题。
您无需从@johnashu
回答的txt文件中删除$