简体   繁体   中英

Monitor E-mail sent via Python win32.Dispatch('Outlook.Application')?

I'm having a bit of trouble getting to a solution with the following, I created a function that sends emails via Outlook, using the win32 package, cause the smtblib in blocked where I work. The function itself works like a charm:

def sendEmail(emailTo, emailCC, emailSubject, emailAttach, emailBody):
    for item in psutil.pids():
        if psutil.pid_exists(item):
            p = psutil.Process(item)
            if p.name() == 'OUTLOOK.EXE':
                os.system('taskkill /f /im OUTLOOK.EXE')
                break
            
    outlook = win32.Dispatch('outlook.application')
    mail = outlook.CreateItem(0)
    mail.Subject = emailSubject
    mail.To = emailTo
    mail.CC = emailCC
    #mail.Body = emailBody
    mail.HTMLBody = emailBody
    if not emailAttach:
        print('No Attachements...\n')
    else:
        print('Attaching files...\n')
        for i in range(0, len(emailAttach)):
            mail.Attachments.Add(emailAttach[i])
    mail.Send()
    time.sleep(30)

The thing here is, sometimes we need to send multiple different e-mails to different people inside a for loop, however since the code goes faster than the e-mail creation, sometimes it fails, I was wondering if there was a way for me to monitor if the e-mail finished, cause from what I could tell, on the task bar it open a outlook with a gear while it's creating and sending the e-mail. The main point here is if there is a way for me to monitor when the e-mail has been created and indeed sent for me to move on to the next item.

I forced a wait with the 30 seconds sleep but I'm sure there is a better and more pythonic way to do this.

Email submission is inherently asynchronous. Even if a message is successfully sent by Outlook, it is possible the target mailbox no longer exists and you will get back an NDR, sometimes hours later.

If MailItem.Send did not error out, this is the best you can do.

To be sure the email is processed and sent out you can handle the ItemAdd event of the Items class, it is fired when one or more items are added to the specified collection. By default, Outlook puts copies of sent items to the Sent Items folder. The MailItem.SaveSentMessageFolder property specifies a Folder object that represents the folder in which a copy of the email message will be saved after being sent. So, you can check out when the message was sent out.

Another aspect of using the Send method from external application is security. You may get errors on the Send line if the Outlook object model raise a security issue. For example, in Microsoft Outlook 2019, Outlook for Office 365, Outlook 2016, and Outlook 2013, when you send an email message from another program such as Microsoft Excel, you receive the following warning message:

A program is trying to send an e-mail message on your behalf. If this is unexpected, click Deny and verify your antivirus software is up-to-date.

But also there is a chance to get an exception at runtime on the Send line instead. Most probably that is the case!

Read more about that in the A program is trying to send an e-mail message on your behalf warning in Outlook article.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM