0

I wanted to send an email via outlook through a python script. I surfed through the internet for python code samples.

I stumbled upon the following question:

Send Outlook Email Via Python?

I tried putting the codes in the answers to the question to test but probably python3 ,which is what I am using, does not have the package win32com.client.

So I have a windows10 system and what I tried to do is to initiate a click on the bottom left corner of the screen and then initiated a keypress of 'o', which would put "outllook" in focus in the start menu and an initiation of keypress "enter" would open "outlook" Then I wanted to initiate a keypress of 'ctrl' and 'N' to create a new message and like that was planning to use outlook keyboard shortcuts to my advantage to send an email.

The code looked something like the following:

import pyautogui

j = pyautogui.size()
pyautogui.click(0,j[1])
pyautogui.keyDown('o')
pyautogui.keyDown('enter')
pyautogui.keyDown('ctrlleft')
pyautogui.keyDown('n')
pyautogui.keyUp('ctrlleft')
pyautogui.keyUp('n')
pyautogui.keyDown('tab')
pyautogui.typewrite('some mail id')

This approach worked fine... when written upto

pyautogui.keyDown('enter')

It could open up the outllook mailbox but when the rest of the part were added, the application took sometime to open and the code did not wait for it and executed the rest of the code so "ctrl + N " did not work.

I looked for ways to make the code pause for the application to open got to know about subprocess module.

And wrote the following code:

import pyautogui 
import subprocess
import os
print("yep it's on")

p = subprocess.Popen(('C:\\Program Files (x86)\Microsoft Office\\root\Office16\\outlook.exe'))
p.wait()

But still that did not work .

How can I make my code pause till the application gets opened ??

AustinWBryan
  • 3,081
  • 3
  • 18
  • 38

0 Answers0