1

I am trying to send a hyperlink to a shared directory using win32com in the body text of an Outlook email. I am not sure what is happening to my path when the program is ran that is making the directory appear as it is below.

import win32com.client as win32
outlook = win32.Dispatch('outlook.application')
mail = outlook.CreateItem(0)
mail.To = 'EMAIL ADDRESSES'
mail.Subject = 'Subject'
mail.HTMLbody = ("Hello All -<br><br>"
         "Please find the following files in the shared drive:<br>"
         "<a href='\\servername1\apps\folder'>"
         "\\servername1\apps\folder</a><br><br>"
         "The file names are:<br>"
         "FILENAMES")
mail.send

The file path is appearing in the email as: \servername1pps\folder

Michael David
  • 93
  • 2
  • 2
  • 7

2 Answers2

2

Guy at my work was able to answer the question.

import win32com.client as win32
outlook = win32.Dispatch('outlook.application')
mail = outlook.CreateItem(0)
mail.To = 'EMAIL ADDRESSES'
mail.Subject = 'Subject'
mail.HTMLbody = (r"""Hello All -<br><br>
     Please find the following files in the shared drive:<br>
     <a href='\\servername1\apps\folder'>
     \\servername1\apps\folder</a><br><br>
     The file names are:<br>
     FILENAMES""")
mail.send

We added the "r" followed by a triple quote and it worked.

Not sure what the r means, but it worked. Maybe someone can explain what the r is to me. Probably a dumb question, but I honestly don't know.

Michael David
  • 93
  • 2
  • 2
  • 7
1

To answer your second question, "what does r in front of a string mean", it means a raw string. To quote from another stackoverflow page:

"The r means that the string is to be treated as a raw string, which means that all escape codes will be ignored"

So if you for example have a file path where folder names have spaces between words you would have to use the r prefix. For example: r"C:\Users\A010101\Desktop\Space between\file.txt"

Please see the question page regarding the r prefix: What does preceding a string literal with "r" mean?

Community
  • 1
  • 1
Crebit
  • 367
  • 1
  • 4
  • 14