简体   繁体   English

遍历数据帧行以发送 email

[英]Iterate through data frame rows to send email

I have a data frame that looks like this:我有一个看起来像这样的数据框:

|num|data |     email       |
|-- | --- | --------------- |
|1  | x   |user1@example.com|
|2  | y   |user1@example.com|
|3  | z   |user2@example.com|

I need to send the 'num', 'data', fields to the email in the 'email' column.我需要将“num”、“data”字段发送到“email”列中的 email。

So for this dataframe, rows 1 and 2 would be sent to user1@example.com and row 3 would be sent to user2@example.com所以对于这个 dataframe,第 1 行和第 2 行将被发送到 user1@example.com,第 3 行将被发送到 user2@example.com

My Idea is to create an 'sendemail' function with the following parameters: data, to_email我的想法是使用以下参数创建一个'sendemail' function:数据,to_email

Then iterate through the data frame rows passing the parameters to the for-loop.然后遍历将参数传递给for循环的数据框行。

Here's where I am:这就是我所在的位置:

import pandas as pd
from tabulate import tabulate
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

# import data into df
df1 = pd.read_excel("data.xlsx")

# define email function       
def sendemail(html, to_email):
   
    fromaddr = "from@email.com"
    toaddr = to_email
    msg = MIMEMultipart("alternative")
    msg["From"] = fromaddr
    msg["To"] = to_email
    msg["Subject"] = "new email"
    msg.add_header("reply-to", "user@email.com")

    body = MIMEText(html, "html")
    msg.attach(body)

    server = smtplib.SMTP("smtp.emailserver.com", 587)

    server.starttls()
      
    server.login(fromaddr, "pw")
    text = msg.as_string()
    server.sendmail(fromaddr, toaddr, text)

    server.quit()

#iterate trough rows and send email
for row in df1.itertuples(index=False):
    sendemail(df1.to_html(index=False), df1["email"])

I believe my error is passing the data into the for-loop.我相信我的错误是将数据传递到 for 循环中。 I am getting an error:我收到一个错误:

AttributeError: 'Series' object has no attribute 'encode'

Any assistance would be greatly appreciated!任何帮助将不胜感激!

Try to change your for loop to:尝试将您的 for 循环更改为:

for row in df1.itertuples(index=False):
    sendemail(df1.to_html(index=False), row.email)

Here, instead of passing df1['email'] as a pandas Series to the sendmail() function where it requires a string for the 2nd argument, you access the row value under column 'email' by the syntax row.email , which is now an element (a string, in particular) and can be used directly in your sendmail() function. Here, instead of passing df1['email'] as a pandas Series to the sendmail() function where it requires a string for the 2nd argument, you access the row value under column 'email' by the syntax row.email , which is现在是一个元素(特别是一个字符串),可以直接在您的sendmail() function 中使用。

An alternative way to access row.email is getattr(row, "email")访问row.email的另一种方法是getattr(row, "email")

I used a different approach iterating through the email process to send it to each row level recipient.我使用了一种不同的方法遍历 email 进程,将其发送给每个行级别的收件人。 You can use a for loop and the zip() function to grab all necessary data.您可以使用 for 循环和 zip() function 来获取所有必要的数据。 I used this on a relatively small data set and cannot speak to whether it performs well on large data sets.我在一个相对较小的数据集上使用了它,无法说出它在大型数据集上是否表现良好。

for x,y,z,a in zip(fulldf['Employee Name'], fulldf['Store E-mail'], fulldf['E-mail'], fulldf['Date Hired']):
outlook = win32com.client.Dispatch('outlook.application')
mail = outlook.CreateItem(0)
mail.To = y
mail.CC = z
mail.Subject = 'Career Growth Conversation'
mail.HTMLBody = """\
    <html>
        <head></head>
        <body>
            <p>Congratulations – """+ x +" hire date: "+ a +""" is ready for a Career Growth Conversations!<br>
                
            </p>
        </body>
    </html>
    """
mail.Attachments.Add(r"")
mail.Send()

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

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