繁体   English   中英

如何将电子邮件发送到电子表格上的电子邮件列表?

[英]How to send e-mails to a list of e-mails on a spreadsheet?

我需要使用 python 向一些客户发送电子邮件。 他们所有的电子邮件都在电子表格上。

我拥有的代码仅将电子邮件发送到电子表格的最后一行。 但我需要它单独发送到所有电子邮件。

我可以在我的代码中进行哪些更改,以便它向所有人发送电子邮件?

import win32com.client as win32
import pandas as pd

# integrating outlook
outlook = win32.Dispatch('outlook.application')

# creating an email
email = outlook.CreateItem(0)

ler = pd.read_excel ('D:\Projeto Zurich/email_list.xlsx')
# setting the e-mail
for index, linha in ler.iterrows ():
    email.to = (linha["EMAIL"])
    email.Subject = "Hello," +  (linha ["NAME"])
    email.HTMLBody = """
    <p>Hi, there!</p>
    """

    email.Send()
    print("Email Enviado")

我的电子表格就是这样:

我认为您需要分两步完成:

  1. 您需要从工作表中获取 email
  2. 然后,您在电子邮件列表上创建一个循环并发送 email

例如,email 位于第一列,主题位于第二列:

# Reading an excel file using Python
import xlrd
# Give the location of the file
loc = ("path of file.xlsx")
# To open Workbook
wb = xlrd.open_workbook(loc)
sheet = wb.sheet_by_index(0)

for i in range(0, 100): # "100" allowing the number of email you have
  # For row 0 and column 0
  email = sheet.cell_value(0, i)
  subject = sheet.cell_value(1, i)
  send_email(email, subject)

def send_email(mail_address, subject):
  # ADD HERE YOUR CODE FOR SENDING YOUR EMAIL
  email.to = mail_address
  email.Subject = "Hello," +  subject
  email.HTMLBody = """
  <p>Hi, there!</p>
  """
  email.Send()
  print("Email Enviado")

暂无
暂无

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

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