Auto Send Email Through Outlook via Python Code – Topbullets.com

Topbullets.comI wrote an article on How to send email through ASP.NET and it helped a lot of people. Automating such processes always save lot of time and manual intervention. Currently in my project one process takes 45 minutes to run and one needs to look out at screen continuously for any error or if code stops in between. For that I wrote a python code which track the process and send email notification instantly if code stops or runs completely. The article can also be useful for someone who is looking for a python code to know which windows applications are running or open any Windows application using Python.
First of all there are lots of SMTP servers which can be used to send email. Google, Yahoo and Hotmail everyone provided SMTP details. You can easily find python code on the same but in my case I am using Outlook app which is Windows default email application.
The code is written is 3 major segments. 1st segment would raise flag when our process stops or runs successfully, 2nd will check if outlook is running or not and then 3rd will send email notification. I won’t paste 1st segment as it depends on your requirement. You can pass one variable and alter its value based on the outcome and then call any functions. FYI, you can add if/else before line 19 and change ’email body’ accordingly. The below code is well commented. One can easily understand the logic behind and use it. If you have any doubt or suggestion please feel free to comment below.

# -*- coding: utf-8 -*-
"""
Created on Wed Sep 21 15:36:00 2016

@author: Deepesh.Singh
"""

import win32com.client as win32
import psutil
import os
import subprocess

# Drafting and sending email notification to senders. You can add other senders' email in the list
def send_notification():
    outlook = win32.Dispatch('outlook.application')
    mail = outlook.CreateItem(0)
    mail.To = 'abc@xzs.com; bhm@ert.com', 
    mail.Subject = 'Sent through Python'
    mail.body = 'This email alert is auto generated. Please do not respond.'
    mail.send
    
# Open Outlook.exe. Path may vary according to system config
# Please check the path to .exe file and update below
    
def open_outlook():
    try:
        subprocess.call(['C:\Program Files\Microsoft Office\Office15\Outlook.exe'])
        os.system("C:\Program Files\Microsoft Office\Office15\Outlook.exe");
    except:
        print("Outlook didn't open successfully")

# Checking if outlook is already opened. If not, open Outlook.exe and send email
for item in psutil.pids():
    p = psutil.Process(item)
    if p.name() == "OUTLOOK.EXE":
        flag = 1
        break
    else:
        flag = 0

if (flag == 1):
    send_notification()
else:
    open_outlook()
    send_notification()

The article can be helpful for:
1. Send email through Python code
2. How to check if windows application is running or not in Python
3. How to open windows application through Python code
4. Find all running programs in Python

Signature

Deepesh Singh
logo

Please leave your valuable comment.

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s