0

I am trying to send multiple emails with click of button through tkinter, i have an excel file where i have written all my information (To-List,CC-List,Email-Subject,Email-Body) and saved in the folder.

Now i want to open the file through Browse button where it will look out for a file for that i created a function def input()

Then created another function def Email() for iterating the rows one by one and it will save the email in outlook.

I have created a below code where it has created three buttons:-

  • Browse Button - It will look out for file in which i have information for multiple emails
  • Begin Button - Email trigger
  • Quit Button - Close the application

import tkinter as tk
from tkinter import ttk
from tkinter import filedialog
import win32com.client as win32
import pandas as pd

def input():
    filename= filedialog.askopenfile()

def Email():
    outlook = win32.Dispatch('outlook.application')
    filterlist = pd.read_excel (input)
    for index, row in filterlist.iterrows():
         x1= row['ToEmail']
         x2= row['CCEmail']
         x3= row['Subject']
         x4= row['Body']
                      
         mail = outlook.CreateItem(0)
         mail.To = x1
         mail.CC = x2
         mail.Subject = x3
         mail.Body = x4
         
         mail.save()

master = tk.TK()
master.title('Email')

top_frame = tk.Frame(master)
bottom_frame= tk.Frame(master)
line = tk.Frame(master, height = 1, width = 400, relief = 'groove')

input_path = tk.Label(top_frame,text='Input File Path')
input_entry = tk. Entry (top_frame,text='', width = 40)
browse = tk.Button (top_frame, text="Browse", command = lambda : input())

begin_button = tk.Button (bottom_frame, text="Email Me!", command = Email)
quit_button = tk.Button (bottom_frame, text="Quit", command = master.destroy)

top_frame.pack(side=tk.TOP)
line.pack(pady=10)
bottom_frame.pack(side=tk.BOTTOM)

input_path.pack(pady=5)
input_entry.pack(pady=5)
browse.pack(pady=5)

begin_button.pack(pady=20,fill=tk.X)
quit_button.pack(pady=20,fill=tk.X)

master.mainloop()

When i am running this code i am getting the below error, its not reading the file correctly.

Can you please let me know what i am doing wrong.

    Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\xxxx\anaconda3\lib\site-packages\win32com\client\dynamic.py", line 89, in _GetGoodDispatch
    IDispatch = pythoncom.connect(IDispatch)
pywintypes.com_error: (-2147221005, 'Invalid class string', None, None)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\xxxx\anaconda3\lib\tkinter\__init__.py", line 1705, in __call__
    return self.func(*args)
  File "<ipython-input-5-044b278bdd66>", line 12, in Email
    outlook = win32.Dispatch('outlook.application')
  File "C:\Users\xxxx\anaconda3\lib\site-packages\win32com\client\__init__.py", line 95, in Dispatch
    dispatch, userName = dynamic._GetGoodDispatchAndUserName(dispatch,userName,clsctx)
  File "C:\Users\xxxx\anaconda3\lib\site-packages\win32com\client\dynamic.py", line 114, in _GetGoodDispatchAndUserName
    return (_GetGoodDispatch(IDispatch, clsctx), userName)
  File "C:\Users\xxxx\anaconda3\lib\site-packages\win32com\client\dynamic.py", line 91, in _GetGoodDispatch
    IDispatch = pythoncom.CoCreateInstance(IDispatch, None, clsctx, pythoncom.IID_IDispatch)
pywintypes.com_error: (-2147221005, 'Invalid class string', None, None)
  • It looks like `Outlook` cannot be found. – acw1668 Jul 07 '20 at 01:24
  • When i am running this command without tkinter it is running fine with same coding but when i am trying to do with tkinter it is not reading the content of the file. – Jatin Kapoor Jul 07 '20 at 01:31
  • But the error is on the line `outlook = win32.Dispatch('outlook.application')` which is not related to tkinter. Also you passed `input` (which is a built-in function) to `pd.read_excel(input)`. – acw1668 Jul 07 '20 at 02:39

0 Answers0