Скрипт не отправляет электронное письмо для каждой строки таблицы Excel - PullRequest
0 голосов
/ 15 апреля 2020

Первый пост и первый кодовый проект здесь.

Я пытаюсь отправить электронное письмо, используя Python для каждой строки в таблице Excel. У меня есть 5 строк ... строка заголовка и четыре строки данных.

По какой-то причине отправляется только последняя строка. Как заставить каждую строку отправлять?

Я часами пытался выяснить это, но мне не повезло. Любая помощь будет оценена.

Заранее спасибо!

import pandas as pd
import smtplib

# From https://medium.com/swlh/automate-sending-emails-with-python-using-a-spreadsheet-e9763b3c9559

'''
Change these to your credentials and name
'''
your_name = "John Smith"
your_email = "XXXXX@gmail.com"
gmail_password = input("Type your password and hit enter:")

# If you are using something other than gmail
# then change the 'smtp.gmail.com' and 465 in the line below

server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
server.ehlo()
server.login(your_email, gmail_password)

# Read the file

email_list = pd.read_excel("20200415-xxxx-Test-Spreadsheet.xlsx")

# Get all the Names, Email Addresses, Subjects and Messages
all_clients = email_list['Client']
all_staff = email_list['Staff']
all_types = email_list['Form Type']
all_due_dates = email_list['Due In X Days']
all_emails = email_list['Email Address']


# Loop through the emails
for idx in range(len(all_emails)):

    # Get each records name, email, subject and message
    client = all_clients[idx]
    staff = all_staff[idx]
    form_type = all_types[idx]
    due_in_x_days = all_due_dates[idx]
    email_address = all_emails[idx]


# Creating the Subject Heading and Message
subject = f"The {form_type} Is Due In {due_in_x_days} Days For {client.upper()}" 
message = f"Hi {staff.title()}, \n\nThe {form_type} is due in {due_in_x_days} days for {client.upper()}.  Please turn it in before the due date. \n\nThanks, \n\nJudy"

# Create the email to send
full_email = ("From: {0} <{1}>\n"
                  "To: {2} <{3}>\n"
                  "Subject: {4}\n\n"
                  "{5}"
                  .format(your_name, your_email, staff, email_address, subject, message))

# In the email field, you can add multiple other emails if you want
# all of them to receive the same text
try:
        server.sendmail(your_email, [email_address], full_email)
        print('Email to {} successfully sent!\n\n'.format(email_address))
except Exception as e:
        print('Email to {} could not be sent :( because {}\n\n'.format(email_address, str(e)))

# Close the smtp server
server.close()
...