向多个收件人发送单独的电子邮件[英] Send individual emails to multiple recipients

本文是小编为大家收集整理的关于向多个收件人发送单独的电子邮件的处理方法,想解了向多个收件人发送单独的电子邮件的问题怎么解决?向多个收件人发送单独的电子邮件问题的解决办法?那么可以参考本文帮助大家快速定位并解决问题。

问题描述

我已经整理了一个代码将电子邮件发送给多个收件人.但是,每个收件人都会收到所有邮件而不是自己的邮件.

dataframe:

email content mark@gmail.com Hi Mark, bla bla eve@gmail.com Hi Eve, bla bla john@gmail.com Hi, John bla bla

for content in df['content']:
    for email in df['email']:
       message = MIMEMultipart()
       message['Subject'] = "Subject"
       message['From'] = 'my email'
       message['Reply-to'] = 'my email'
       message['To'] = '{}'.format(email)

       text = MIMEText(mail)
       message.attach(text)

       server = smtplib.SMTP ('smtp.gmail.com',587)
       server.ehlo()
       server.starttls()
       server.login(login, password) 
       server.sendmail(message['From'], message['To'], message.as_string())
       server.quit()

推荐答案

尝试以下方法:

df = {
      'content': ['test1', 'test2', 'test3'], 
        'email': ['mail1@mail.com', 'mail2@mail.com', 'mail3@mail.com']
     }

x = 0
for email in df['email']:
        print(email)
        print(df["content"][x]+"\n")
        x+=1

其他推荐答案

Just zip列在一起并省略一个循环:

for email, content in zip(df['email'], df['content']):
       message = MIMEMultipart()
       message['Subject'] = "Subject"
       message['From'] = 'my email'
       message['Reply-to'] = 'my email'
       message['To'] = '{}'.format(email)

       text = MIMEText(mail)
       message.attach(text)

       server = smtplib.SMTP ('smtp.gmail.com',587)
       server.ehlo()
       server.starttls()
       server.login(login, password) 
       server.sendmail(message['From'], message['To'], message.as_string())
       server.quit()

其他推荐答案

我迭代了两次.而不是使用2用于循环,而是使用out dataframe的正确方法是使用

而不是:

for content in df['content']:
    for email in df['email']:

使用此:

for c in df.itertuples():
   message = MIMEMultipart()
   message['To'] = '{}'.format(c[0])

   text = MIMEText(c[1])
   message.attach(text)

本文地址:https://www.itbaoku.cn/post/1727784.html

相关标签/搜索