簡體   English   中英

從 excel 文件自動發送批量電子郵件

[英]Automating sending bulk emails from an excel file

Email 通知列表

我想自動向我列表中的學校家長發送自定義/個性化電子郵件,如上圖所示,提供學生課程的詳細信息和其他相關信息。為此,我正在使用openpyxlsmtplib模塊。

但是,我在包含學生信息和家長信息的字典中的行和項目的循環運行不佳。 每次我運行腳本時,我都會收到這種類型的錯誤

Traceback (most recent call last):
  File "class_email_notification.py", line 42, in <module>
    for course, account, link, clasS_day, session in class_info.items():
ValueError: not enough values to unpack (expected 5, got 2)

我的 xlsx 和 .py 文件都位於同一目錄中。 .py 文件的片段是這樣的:

import openpyxl, smtplib, sys, datetime
from time import sleep

wb = openpyxl.load_workbook('course_notification.xlsx')
sheet = wb['student info']

parents = {}
class_info = {}
students = {}
day = datetime.datetime(2020, 6, 1, 8, 0 ,0).strftime('%A, %B, %d, %Y %I:%M:%S %p')

for r in range(2, sheet.max_row + 1):
    parent_name = sheet.cell(row = r, column = 8).value
    parent_email = sheet.cell(row = r, column = 9).value
    parents[parent_name] = parent_email

    course = sheet.cell(row = r, column = 3).value
    account = sheet.cell(row = r, column = 4).value
    link = sheet.cell(row = r, column = 5).value
    clasS_day = sheet.cell(row = r, column = 6).value
    session = sheet.cell(row = r, column = 7).value
    class_info[course] = account, link, clasS_day, session

    gender = sheet.cell(row = r, column = 2).value
    student_name = sheet.cell(row = r, column = 1).value

smtpObj = smtplib.SMTP('smtp.gmail.com')
smtpObj.ehlo()
smtpObj.starttls()

for parent_name, parent_email in parents.items():
    for course, account, link, clasS_day, session in class_info.items():
        smtpObj.login('example@gmail.com', sys.argv[1])
        if gender.lower() == 'male':
            gender = 'his'
            body = 'Subject: Tinker Education Online Classes\n\nDear %s,\n\nHope you and your family are doing well and keeping safe while at home. Tinker Education would like to bring to your attention that our online classes will commence  on ', day,' .\n\nOur teachers would like to have an insanely great start of the term with our students. Hence, you are requested to notify your child of ',gender, 'class time. Class details are as follows:\n\n%s\n\nDay: %s\nSession: %s\nCLP:\n%s\nVideo link: %s\n\n%s is requested to keep time of ', gender, ' %s %s\n\nKind regards,\nTinker Desk'%(parent_name, course, clasS_day, session, account, link, student_name, course, session)
        else:
            gender = 'her'
            body = 'Subject: Tinker Education Online Classes\n\nDear %s,\n\nHope you and your family are doing well and keeping safe while at home. Tinker Education would like to bring to your attention that our online classes will commence  on ', day,' .\n\nOur teachers would like to have an insanely great start of the term with our students. Hence, you are requested to notify your child of ',gender, 'class time. Class details are as follows:\n\n%s\n\nDay: %s\nSession: %s\nCLP:\n%s\nVideo link: %s\n\n%s is requested to keep time of ', gender, ' %s %s\n\nKind regards,\nTinker Desk'%(parent_name, course, clasS_day, account, session, link, student_name, course, session)
        print('Sending emails to %s through %s'%(parent_name, parent_email))
        send_email_status = smtpObj.sendmail('my_email@gmail.com', parent_email, body)

if send_email_status != {}:
    print('There was an error sending an email to %s through %s'(parent_name, parent_email))
smtpObj.quit()

for循環在遍歷字典中的鍵和值時沒有提供足夠的值來解包。 我錯過了什么?

我試圖理解為什么無法獲取字典中的所有項目。 我的假設是我的邏輯不是那么正確。

我已經參考了自動化無聊的東西:發送電子郵件,我可以理解如何 go 關於它。 但真正了解為什么我的腳本上的 dicts 沒有完全解包的確切邏輯是我的問題。

通過class_info.items()迭代返回的結構在這里是一個嵌套元組,其中第一個元素是鍵,第二個元素是關聯的值。 在這種情況下,它是(account, link, clasS_day, session)

您可以通過for course, (account, link, clasS_day, session) in class_info.items():

復制由class_info.items()返回的元組結構

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM