1

I'm interested in automating Outlook to check/read mails. I'm using the package win32com with Python to control the app, but I can't find how to open an hyperlink which is in the mail body.

Is there an if or for statement that can open the hyperlink automatically?

Thank you in advance for your help!

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45

2 Answers2

2

I found a way to open the hyperlink with the BeautifulSoup and webbrowser libraries thanks to the HTMLBody property:

from bs4 import BeautifulSoup
import webbrowser

    html = mail.HTMLBody #html source code of the mail
    soup = BeautifulSoup(html, 'html.parser')
    for link in soup.find_all('a'): # will find every <a> tag in the html source code
        webbrowser.open(link.get('href')) # will open the url(s)
chronowix
  • 36
  • 8
1

It seems you need to iterate over each item and check the HTMLBody property which returns a string representing the HTML body of the specified item. In the returned string you may find all hyperlinks which you can check.

Note, iterating over all items is not really a good idea. I'd suggest using the Find/FindNext or Restrict methods of the Items class from the Outlook object model instead. They allow dealing only with items that correspond to your conditions / search criteria. Read more about these methods in the article I wrote for the technical blog:

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45