【问题标题】:Send gmail with multiple recipients与多个收件人一起发送 gmail
【发布时间】:2020-12-09 08:08:57
【问题描述】:

我正在尝试创建批量电子邮件发件人。我正在努力添加多个收件人。我希望脚本打开包含多个电子邮件地址的 contacts.csv,并将电子邮件发送给他们。

简单地说,我需要帮助创建一个打开 csv 文件、读取电子邮件并将电子邮件发送到这些地址的脚本。

我试过了,但是没用:

with smtplib.SMTP("smtp.gmail.com:587") as server:
    with open("contacts.csv") as file:
        reader = csv.reader(file)
        next(reader)  # Skip header row
        for name, email in reader:
            server.sendmail(
                sender_email,
                email,text
                #message.format(name=name),
            )

我的 contacts.csv 文件:

name, email
john, test1@gmail.com
jake, test2@gmail.com

我的 emailsender.py 文件:

import smtplib
from email import encoders 
from email.mime.text import MIMEText 
from email.mime.multipart import MIMEMultipart 

fromaddr = "myemail@gmail.com"
toaddr = "emails@gmail.com"
msg = MIMEMultipart() 
      
msg['From'] = fromaddr 
msg['To'] = toaddr 
msg['Subject'] = "test"
body = "testing"
msg.attach(MIMEText(body, 'plain')) 
      
s = smtplib.SMTP('smtp.gmail.com', 587) 
s.starttls() 
s.login(fromaddr, "password") 
text = msg.as_string() 
s.sendmail(fromaddr, toaddr, text)
print("Email sent!")
s.quit() 
send()

【问题讨论】:

    标签: python email


    【解决方案1】:

    我已经测试了这段代码,它工作正常:

    import smtplib
    from email.mime.text import MIMEText 
    from email.mime.multipart import MIMEMultipart 
    import csv
    
    emails = []
    with open('test.csv', 'r') as file: #open csv file
      reader = csv.reader(file) #init the csv reader
      for row in reader: #iterate through all rows in the csv file
        emails.append(row[1]) #add the second element of every row (the email column) to the list "emails"
      emails.pop(0) #remove the first email which is just the text "email" in the csv file that we don't want
    print(emails) #remove if you wish
    
    def sender(recipients, from_email, password, body, subj): #create a sender function. partially adapted from https://stackoverflow.com/a/51931160/8402369
      msg = MIMEMultipart()
    
      msg['Subject'] = subj
      msg['From'] = from_email
      msg['To'] = (', ').join(recipients)
    
      msg.attach(MIMEText(body,'plain'))
    
      server = smtplib.SMTP('smtp.gmail.com', 587)
      server.starttls()
      server.login(from_email, password)
      server.send_message(msg)
      server.quit()
    
    
    sender(emails, 'youremail@gmail.com', 'your_password', 'Message body', 'Subject') #send all emails
    

    Run and edit the code online

    编辑:

    这是一个能够根据 csv 数据更改电子邮件内容的版本:

    import smtplib
    from email.mime.text import MIMEText
    from email.mime.multipart import MIMEMultipart
    import csv
    
    csvdata = []
    with open('test.csv', 'r') as file:  #open csv file
        reader = csv.reader(file)  #init the csv reader
        for row in reader:  #iterate through all rows in the csv file
            csvdata.append(
                row #instead of adding the email to the array, add the whole row to the array
            )  #add the second element of every row (the email column) to the list "emails"
        csvdata.pop(0)
    
    
    def sender(
            recipient, name_to_display, from_email, password, body, subj
    ):  #create a sender function. partially adapted from https://stackoverflow.com/a/51931160/8402369
        msg = MIMEMultipart()
    
        msg['Subject'] = subj
        msg['From'] = f'{name_to_display} <{from_email}>'
        msg['To'] = recipient
    
        msg.attach(MIMEText(body, 'plain'))
    
        server = smtplib.SMTP('smtp.gmail.com', 587)
        server.starttls()
        server.login(from_email, password)
        server.send_message(msg)
        server.quit()
    
    your_email = "email@gmail.com"
    your_password =  "password"
    name_to_display = "The name of the sender that displays in the inbox (your name or whatever you want)"
    
    for item in csvdata:
        name = item[0] #the first column of the csv file is the name
        email = item[1] #the second is the email. if you have more columns get them here like so: item[2] (change the index accordingly)
        print(f'Sending email to {email}')
        body = "Hi " + name + ", The rest of the content goes here"
        subject = "Subject"
        sender(email, name_to_display, your_email, your_password, body, subject)
    
    

    Run and edit this code online

    注意事项:

    sender函数部分改编自here

    【讨论】:

    • 感谢您的提交。如果我想更进一步,将他们的姓名从 contacts.csv 包含到电子邮件正文中,我该怎么做?
    • 我不确定您是否可以这样做,因为您将同一封电子邮件发送给多个人。您可以向每个人发送单独的电子邮件,但对于很多人来说,这很可能需要更长的时间来处理
    • 所有邮件都可以相同,但唯一需要更改的变量是名称。那么这是不可能的吗? @MarsNebulaSoup
    • 这绝对不是不可能的。让我打一个简单的例子来说明我正在做什么。
    • 抱歉拖了这么久。我试图让发件人的姓名出现在电子邮件收件箱中。无论如何,this 应该可以工作。它也不是很慢。我对其进行了测试,并收到了两封不同名称的电子邮件,就像您需要的那样
    猜你喜欢
    • 1970-01-01
    • 2015-10-17
    • 2021-01-18
    • 2019-03-08
    • 2019-03-08
    • 1970-01-01
    • 2016-12-11
    • 1970-01-01
    • 2013-11-02
    相关资源
    最近更新 更多