繁体   English   中英

尝试使用python发送电子邮件但出现错误

[英]Trying to send an email with python but getting error

嗨,我一直在尝试使用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

我不知道怎么了,我是新用户。 请帮忙。 谢谢。 最初的错误是目标机器拒绝,但解决之后,这种情况发生了。 我需要安装电子邮件软件包或类似的软件包吗?如果要怎么办?

从模板中删除$符号

 Dear {PERSON_NAME},

从模板中删除$符号

 Dear {PERSON_NAME},

或者,如果模块较新,则使用双花括号{{}}:

 Dear {{PERSON_NAME}},

您的脚本或txt文件都没有错,在我的系统上运行正常! 请尝试在其他文件或其他系统中再次运行。 或者您的python lib文件可能存在一些问题。

您不需要像@johnashu回答的那样从$ txt文件中删除$

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM