vithal n
vithal n

Reputation: 11

how to send outlook mail to multiple users with different content using python automation

I enter image description here need to automate the outlook mail to multiple recipients with different content for each and every user, I am attaching the dataset and code which i have written.For better understanding I have attached the dataset , in that I have to send mail to respective person with their information only as a email body. In my code the for loop is not working and only executing one time and sending mail for the first recipient. Please find the code and data as attached and suggest what to do.

'''

import win32com.client as win32
#import os,datetime,schedule,time,pathlib
import numpy as np
import pandas as pd
import smtplib
import csv
from string import Template
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
dtest = pd.read_csv('details.csv')
dt1  = dtest
de = pd.read_csv('email_name.csv')

de_l = dt1['Email_id']
list_c = list(de_l)
#print(list_c)


for i in list_c:
    dt2 = dtest[dtest['Email_id']==i]

    outlook=win32.Dispatch('outlook.application')
    body='''\
        <html>
        <head></head>
        <body>
        <p>Hello All,<br>
        <p> Please submit your timesheet by eod.<br>
        <body> '''+ dt2.to_html()+''' </br></body>
        <font color ='blue'>
       <br>Thanks</>
        </font>
       <br></br>
       <font color ='red'>
        please note this is an automated mail do not reply<t1>
       </font>
         </p>
        </body>
    
        </style>
        </html>
        '''

    

    mail=outlook.CreateItem(0)

    mail.Subject = ' -testing email notification'
    mail.To = i

    mail.HTMLBody = body
    From= '[email protected]'

    mail.send()
   '''

Upvotes: 0

Views: 694

Answers (1)

Xenobiologist
Xenobiologist

Reputation: 2151

this should give you an idea how to solve the problem.

# Outlook-Mailing
# https://stackoverflow.com/questions/69987277/how-to-send-outlook-mail-to-multiple-users-with-different-content-using-python-a

import pandas as pd
import win32com.client
import time

path = r'd:\Python\mailing.csv'
df = pd.read_csv(path, sep=';', encoding='cp1252')
print(df)


for i in df.index:
    outlook = win32com.client.Dispatch('outlook.application')
    mail = outlook.CreateItem(0)
    mail.Subject = 'Python Mail'
    #mail.HTMLBody = '<h3>This is HTML Body</h3>'
    print(df['Name'][i], df['Email_id'][i])
    mail.To = df['Email_id'][i]

    mail.HTMLBody = '''\
        <html>
        <head></head>
        <body>
        <p>Hello ''' + df['Name'][i] + ''',<br>
        <p> Please submit your timesheet by eod.<br>
        <body> ''' + str(df['Math'][i]) + ''' </br></body>
        <font color ='blue'>
       <br>Thanks</>
        </font>
       <br></br>
       <font color ='red'>
        please note this is an automated mail do not reply<t1>
       </font>
         </p>
        </body>
        </style>
        </html>
        '''
#mail.Body = "This is the normal body"
#mail.Attachments.Add('c:\\sample.xlsx')
#mail.CC = '[email protected]'

    mail.Send()

Upvotes: 1

Related Questions