简体   繁体   中英

Python Email Send to Multiple Recipients

My code doesn't have a problem. In my list of recipients, I have 10 emails adress. Do you know to send an email to every singles address email of this list?

Message error:

pywintypes.com_error: (-2147352567, 'Exception occurred.', (4096, 'Microsoft Out look', 'Outlook does not recognize one or more names. ', None, 0, -2147467259), None)

import win32com.client as win32
import pandas as pd

xls = pd.ExcelFile(path)
dfs = xls.parse(sheet_name="details", header = None)
recipients = dfs[21]

outlook = win32.Dispatch('outlook.application')
mail = outlook.CreateItem(0)
mail.To = recipients
mail.Subject = 'Automatic Email '
mail.Body = 'body'
mail.HTMLBody = 'Hello, this is a test </h2>'
mail.Send()

I would make a function to send a message, and then just put a for loop that says for email in email_list: send_email(your_subject, your_message, email)

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

def send_email(subject, message, recipient):
    smtp_obj = smtplib.SMTP('smtp.mail.yahoo.com', 587)
    smtp_obj.starttls()
    smtp_obj.ehlo()
    smtp_obj.login('your_username_here', 'your_password_here')
    msg = MIMEMultipart()
    msg['From'] = 'your_email_here'
    msg['To'] = recipient
    msg['Subject'] = subject
    msg.attach(MIMEText(message, 'plain'))
    smtp_obj.sendmail(msg['From'], msg['To'], msg.as_string())
    smtp_obj.quit()

this is my function for sending e_mail through yahoo, but it can be adapted by logging into the appropriate smtp server for your provider (I would guess outlook from your code)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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