简体   繁体   English

在python 3.3中从.txt发送电子邮件给收件人

[英]send email to recipient from .txt in python 3.3

I am trying to send an email with smtp in python 3.3 to a recipient listed in a text file. 我正在尝试将python 3.3中带有smtp的电子邮件发送给文本文件中列出的收件人。 The error I receive is: 我收到的错误是:
session.sendmail(sender, recipient, msg.as_string()) smtplib.SMTPRecipientsRefused: {} session.sendmail(sender, recipient, msg.as_string()) smtplib.SMTPRecipientsRefused: {}

Where is the error in sendmail? sendmail中的错误在哪里? Thanks! 谢谢!

Full code below: 完整代码如下:

#!/usr/bin/python
import os, re
import sys
import smtplib

from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

mails = open('/path/emails.txt','r+')
mailList = mails.read()
mailList = [i.strip() for i in mails.readlines()] 
directory = "/path/Desktop/"

SMTP_SERVER = 'smtp.gmail.com'
SMTP_PORT = 587

sender = 'Sender@gmail.com'
password = "Sender'sPassword"
recipient = [i.strip() for i in mails.readlines()] 
subject = 'Python (-++-) Test'
message = 'Images attached.'

def main():
    msg = MIMEMultipart()
    msg['Subject'] = 'Python (-++-) Test'
    msg['To'] = recipient
    msg['From'] = sender

    files = os.listdir(directory)
    pngsearch = re.compile(".png", re.IGNORECASE)
    files = filter(pngsearch.search, files)
    for filename in files:
        path = os.path.join(directory, filename)
        if not os.path.isfile(path):
            continue

         img = MIMEImage(open(path, 'rb').read(), _subtype="png")
         img.add_header('Content-Disposition', 'attachment', filename=filename)
         msg.attach(img)

    part = MIMEText('text', "plain")
    part.set_payload(message)
    msg.attach(part)

    session = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)

    session.ehlo()
    session.starttls()
    session.ehlo
    session.login(sender, password)

    session.sendmail(sender, recipient, msg.as_string())
    session.quit()

if __name__ == '__main__':
    main()

You want to double-check your recipients code. 您要仔细检查您的收件人代码。 It looks like you're trying to consume the contents of the file more than once, which won't work--the file objects should be understood as a stream, not a block of data, so once you've done f.read() or [i.strip() for i in mails.readlines()] one time, that stream is empty, so doing it a second time is going to produce an empty list. 看来您尝试不止一次使用文件的内容,这是行不通的-文件对象应被理解为流,而不是数据块,因此一旦完成f.read()[i.strip() for i in mails.readlines()]一次,该流为空,因此第二次执行将产生一个空列表。 You should check this yourself by printing recipient 您应该通过打印recipient自己检查一下

then try this: 然后试试这个:

mails = open('/path/emails.txt','r+')
#mailList = mails.read()
#mailList = [i.strip() for i in mails.readlines()] 
directory = "/path/Desktop/"

SMTP_SERVER = 'smtp.gmail.com'
SMTP_PORT = 587

sender = 'Sender@gmail.com'
password = "Sender'sPassword"
recipient = [i.strip() for i in mails.readlines()] 
print(recipient)
subject = 'Python (-++-) Test'
message = 'Images attached.'

Now you should have the a populated recipient list, and on to the next issue! 现在,您应该拥有一个已填充的收件人列表,然后继续下一期!

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

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