Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/346.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 自动化电子邮件传递_Python_Excel_Smtp - Fatal编程技术网

Python 自动化电子邮件传递

Python 自动化电子邮件传递,python,excel,smtp,Python,Excel,Smtp,我从一本书(用Python自动化任务)中举了一个自动化的例子,其中包括打开和阅读电子表格,检查费用是否已支付,如果未支付,则向客户发送电子邮件通知他。但是当我运行代码时,它不会显示任何错误,但也不会发生任何事情。如果你能帮助我,我将不胜感激,如果必要的话,我仍然会推荐一个图书馆来执行这个过程 请遵循以下代码: import openpyxl, smtplib, sys wb = openpyxl.load_workbook('C:/temp/cobranca.xlsx') sheet = wb

我从一本书(用Python自动化任务)中举了一个自动化的例子,其中包括打开和阅读电子表格,检查费用是否已支付,如果未支付,则向客户发送电子邮件通知他。但是当我运行代码时,它不会显示任何错误,但也不会发生任何事情。如果你能帮助我,我将不胜感激,如果必要的话,我仍然会推荐一个图书馆来执行这个过程

请遵循以下代码:

import openpyxl, smtplib, sys

wb = openpyxl.load_workbook('C:/temp/cobranca.xlsx')
sheet = wb['Sheet1']

lastCol = sheet.max_column
latestMonth = sheet.cell(row=1, column=lastCol).value

unpaidMembers = {}
for r in range(2, sheet.max_row + 1):
    payment = sheet.cell(row=r, column=lastCol).value
    if payment != 'ok':
        name = sheet.cell(row=r, column=1).value
        email = sheet.cell(row=r, column=2).value
        unpaidMembers[name] = email


smtpObj = smtplib.SMTP('mail.omnia.net.br', 465)
smtpObj.ehlo()
smtpObj.starttls()
smtpObj.login('dp.contabil@omnia.net.br', sys.argv[1])

for name, email in unpaidMembers.items():
    body = "Subject: %s dues unpaid. \n Dear %s, \n Records show that you have not paid dues for %s. Please make this payment as soon as possible. Thank you!'" % (latestMonth, name, latestMonth)
    print('Sending email to %s...' % email)
    sendmailStatus = smtpObj.sendmail('dp.contabil@omnia.net.br', email, body)

    if sendmailStatus != {}:
        print('There was a problem sendind email to %s: %s' % (email, sendmailStatus))
        smtpObj.quit()

下面是我使用的一个示例:

import smtplib

from string import Template

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

MY_ADDRESS = 'XYZ@gmail.com'
PASSWORD = 'YourPassword'


def get_contacts(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):
    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('C:/Users/xyz/Desktop/mycontacts.txt')  # read contacts
    message_template = read_template('C:/Users/xyz/Desktop/message.txt')
    s = smtplib.SMTP(host='smtp.gmail.com', port=587)
    s.starttls()
    s.login(MY_ADDRESS, PASSWORD)
    for name, email in zip(names, emails):
        msg = MIMEMultipart()  # create a message
        message = message_template.substitute(PERSON_NAME=name.title())
        print(message)
        msg['From'] = MY_ADDRESS
        msg['To'] = email
        msg['Subject'] = "Sending mail to all"
        msg.attach(MIMEText(message, 'plain'))
        s.send_message(msg)
        del msg
    s.quit()
if __name__ == '__main__':
    main()


您的代码是否正确填写了
unpaidMembers
字典?首先使用
print(unpaidMembers)
查看dictionaryYes@CDJB中的内容。我使用了
打印(unpaid成员)
,谢谢@furas@gdn我可以给你发一封我喜欢的电子邮件吗did@Vaibhav我想要。