1

I tried this email sending sample code:

# -*- 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()

but keep hitting the following error when I run the code on command prompt:

C:\<>\Desktop\Exp>python sendemail.py
Traceback (most recent call last):
  File "sendemail.py", line 40, in <module>
    import win32com.client
  File "C:\Python27\lib\site-packages\win32com\__init__.py", line 5, in <module>
    import win32api, sys, os
ImportError: DLL load failed: %1 is not a valid Win32 application.

Can someone please guide me on how to fix this error? Or is their a better way to do this?

Thanks.

Taku
  • 31,927
  • 11
  • 74
  • 85
Khan
  • 11
  • 1
  • 3
  • please tell your python version and bit? and OS ? – Harsha Biyani Oct 09 '17 at 07:24
  • Python 2.7.13 (v2.7.13:a06454b1afa1, Dec 17 2016, 20:42:59) [MSC v.1500 32 bit (Intel)] on win32. I am using Windows 10 64 bit OS – Khan Oct 09 '17 at 23:49
  • I re-installed pywin32 (32-bit version) as pointed out by Trapli below and also installed psutil module and restarted my system. Issue is resolved now and I am able to send email. Thank you all for helping me out. – Khan Oct 10 '17 at 01:56
  • More details: [\[SO\]: Python Ctypes - loading dll throws OSError: \[WinError 193\] %1 is not a valid Win32 application (@CristiFati's answer)](https://stackoverflow.com/a/57297745/4788546). – CristiFati Dec 20 '22 at 22:35

3 Answers3

1

This is likely a x86 vs x64 issue. If you use 64 bit python, import a 64 bit dll, if you use 32 bit python, import the 32 bit dll.

Edit2: I think this is what you are looking for. See this for details.

Trapli
  • 1,517
  • 2
  • 13
  • 19
  • 1
    Your answer is pretty self-explanatory, a more formal and better answer should address the question *”how you can do that?”*, instead of just saying “you need to do this”, but without telling them how to do it. – Taku Oct 09 '17 at 07:30
0

Did you install the Win32 Extensions for Python? Make sure you pick the correct installer for your system and version of Python otherwise it may not work correctly.

AzizStark
  • 1,390
  • 1
  • 17
  • 27
0

I re-installed pywin32 (32-bit version) as pointed out by Trapli and also installed psutil module and restarted my system. Issue is resolved now and I am able to send email. Thank you all for helping me out.

Khan
  • 11
  • 1
  • 3