How to Add File as Attachment to Outlook Item in Python - python

I have just created a few files and zipped them up, then begun an email to send it. I'ts probably simple, but I haven't been able to figure out how to specify a file by path to attach. Feeding the filepath alone doesn't seem to work?
ZipName = 'Order'+OrderNumber+'.zip'
zip = zipfile.ZipFile(ZipName, 'a', 8)
for file in os.listdir(filepath_out):
if file.endswith(".epw"):
zip.write(file)
zip.close()
outlook = win32.Dispatch('outlook.application')
mail = outlook.CreateItem(0)
#mail.From = 'sales#c1.com'
mail.To = 'support#c2.com'
mail.Subject = 'Files for Order ' + OrderNumber
mail.HtmlBody = ""
mail.Attachments.Add(ZipName)
mail.Display(True)
It's off topic but related; is there an easy way to specify a non-default "from" email address? "From" doesn't seem to be a property and "Sender" doesn't change anything.

Attachments.Add takes a fully qualified file name (e.g. c:\temp\order1.zip), not just a file name.

Related

How to read in Excel email attachments received in Outlook?

So I am trying to read in an Excel file from an attachment in Microsoft outlook. The code below works, but only while the email I am trying to read the attachment from is at the top of my inbox. How can I adjust my code so it looks at all emails in my inbox folder looking for the attachment or finding the emailed based on the subject provided. Also I would eventually like to have this work with a shared mailbox, but that is a secondary issue right now.
from win32com.client import Dispatch
import email
import datetime as date
import os
outlook = Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox = outlook.GetDefaultFolder("6")
all_inbox = inbox.Items
val_date = date.date.today()
sub_today = 'subject of email'
att_today = 'Filename'
for msg in all_inbox:
yourstring = msg.Subject.encode('ascii', 'ignore').decode('ascii')
if(yourstring.find('"Filename"') != -1):
break
for att in msg.Attachments:
if att.FileName == att_today:
attachments = msg.Attachments
break
attachment = attachments.Item(1)
fn = os.getcwd() + '\\' + att_today
attachment.SaveASFILE(fn)
df = pd.read_excel(fn)
First of all, to iterate over all items you need to skip items, not break the loop. Use the continue keyword instead of break in the loop.
The Outlook object model provides the Find/FindNext and Restrict methods for getting items that correspond to your conditions. The store provider does the job more efficient than just iterating over all items in the folder. Read more about these methods in the following articles:
How To: Use Find and FindNext methods to retrieve Outlook mail items from a folder (C#, VB.NET)
How To: Use Restrict method to retrieve Outlook mail items from a folder
You can use the following search criteria where the query performs a phrase match query for hello in the message subject (VBA syntax):
filter = "#SQL=" & Chr(34) & "https://schemas.microsoft.com/mapi/proptag/0x0037001E" _
& Chr(34) & " ci_phrasematch " & "'hello'"
To get a shared folder using the NameSpace.GetSharedDefaultFolder method which returns a Folder object that represents the specified default folder for the specified user. This method is used in a delegation scenario, where one user has delegated access to another user for one or more of their default folders (for example, their shared Calendar folder).

How to Rename Attachments from outlook msg (.msg files) with the msg file name it self (string before .msg) with Python

I have a folder that has nearly 12k (.msg) files each has a csv attachment.
I managed to get a code to extract the attachment from each .msg file. but due to attachment and subjects are similar the attachment keeps getting over written! I tried to rename with msg.subject but the subject of the msg is similar
import win32com.client
import os
inputFolder = r'directory with my msg' ## Change here the input folder
outputFolder = r'directiry for attachments' ## Change here the attachments output folder
for file in os.listdir(inputFolder):
if file.endswith(".msg"):
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
filePath = inputFolder + '\\' + file
msg = outlook.OpenSharedItem(filePath)
att = msg.Attachments
for i in att:
i.SaveAsFile(os.path.join(outputFolder, str(msg.subject + ".csv")))
#Saves the file with the attachment name
You need to find an algorithm which allows identifying attachments uniquely - try to combine the attachment file name with email data such as ReceivedTime and etc.
Don't forget to exclude forbidden symbols from the result file name before trying to save the attachment.

Attaching files files to line spacing in email (RTF) with win32com

I'm making a tool for automatic email with Python and win32com.
I would like to attach files in line spacing due to make mail in RTF.
Accomplishment
Hello, world!
{attached file}
Thank you.
Code
outlook = = win32com.client.Dispatch("Outlook.Application")
mail = outlook.createItemFromTemplate(template_item)
mail.to = ""
mail.cc = ""
mail.bcc = ""
mail.bodyformat = 3 # 3: RTF
mail.Attachment.Add(attachment)
mail.Display(True)
With this code, All files were attached to bottom.
I handled with using 'format' method in mail.Body, but the name of file was inserted.
Could you teach me about the approach to attach files to line spacing in email (RTF)?
mail.to = ""
mail.cc = ""
mail.bcc = ""
mail.bodyformat = 3
mail.Display(True) # before attachment
mail.Attachment.Add(Source=attachment,Position=position)
Changing the "Display" layout, I can attach files in line spacing.

python forward email to specific outlook folder win32com

I want to forward an Outlook email to a specific folder (not Inbox) using python. I'm using win32com.
When I Forward then Move, my email does appear in the specific folder, but when I look in my Outlook the Datetime and From are not showing.
How do I get the Datetime and From onto the forward/moved email?
import win32com.client
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
root_folder = outlook.Folders.Item(1)
sub_folder1 = root.Folders['Project1']
next_level = sub_folder1.Folders['ProjectA']
msg = next_level.Items
msgs = msg.GetLast()
dest = sub_folder1.Folders['ProjectB']
NewMsg = msgs.Forward()
NewMsg.Move(dest)
This is because you're making a draft of the original received email.
To simply move the email remove the NewMsg = msgs.Forward() line and replace NewMsg.Move(dest) with msgs.Move(dest).
import win32com.client
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
root_folder = outlook.Folders.Item(1)
sub_folder1 = root_folder.Folders['Project1']
next_level = sub_folder1.Folders['ProjectA']
msg = next_level.Items
msgs = msg.GetLast()
dest = sub_folder1.Folders['ProjectB']
msgs.Move(dest)
If you wish to make an actual copy and move the new item to the second folder, replace your ending code with
msgcopy = msgs.Copy()
dest = sub_folder1.Folders['ProjectB']
msgcopy.Move(dest)

Add signature to outlook email with python using win32com

Does anyone know how to add an email signature to an email using win32com?
import win32com.client as win32
outlook = win32.Dispatch('outlook.application')
mail = outlook.CreateItem(0)
mail.To = 'TO'
mail.Subject = 'SUBJECT'
mail.HTMLbody = 'BODY'
mail.send
A fully functional e-mailer function with signature included, using code from the answer above:
def Emailer(message, subject, recipient):
import win32com.client as win32
outlook = win32.Dispatch('outlook.application')
mail = outlook.CreateItem(0)
mail.To = recipient
mail.Subject = subject
mail.GetInspector
index = mail.HTMLbody.find('>', mail.HTMLbody.find('<body'))
mail.HTMLbody = mail.HTMLbody[:index + 1] + message + mail.HTMLbody[index + 1:]
mail.Display(True)
#mail.send #uncomment if you want to send instead of displaying
then call
Emailer("Hi there, how are you?", "Subject", "david#bisnode.com")
Outlook signatures are not exposed through the Outlook Object Model. The best you can do is read the signature from the file system and add its contents to the HTML body appropriately. Keep in mind that two HTML strings must be merged, not just concatenated. You would also need to merge the styles from two HTML documents and take care of the embedded images used by the signature.
Note that Outlook adds a signature when an unmodified message is displayed or its inspector is touched
import win32com.client as win32
outlook = win32.Dispatch('outlook.application')
mail = outlook.CreateItem(0)
mail.To = 'TO'
mail.Subject = 'SUBJECT'
mail.GetInspector
mail.HTMLBody now contains the message signature that you will need to merger (not just concatenate!) with your own HTML
UPDATE: as of the latest (Summer 2016) builds of Outlook, GetInspector trick no longer works. Now Only MailItem.Display adds the signature to an unmodified message.
If you want to programmatically insert a signature, Redemption (I am its author) exposes RDOSignature object which implements ApplyTo method (it handles the signature image files and merges HTML styles appropriately).
You can find the signature in Outlook stored as an HTML file in %APPDATA%\Microsoft\Signatures and I used the following code to copy the file contents and add them to my email body
import win32com.client
import os
signature_path = os.path.join((os.environ['USERPROFILE']),'AppData\Roaming\Microsoft\Signatures\Work_files\\') # Finds the path to Outlook signature files with signature name "Work"
html_doc = os.path.join((os.environ['USERPROFILE']),'AppData\Roaming\Microsoft\Signatures\Work.htm') #Specifies the name of the HTML version of the stored signature
html_doc = html_doc.replace('\\\\', '\\') #Removes escape backslashes from path string
html_file = codecs.open(html_doc, 'r', 'utf-8', errors='ignore') #Opens HTML file and ignores errors
signature_code = html_file.read() #Writes contents of HTML signature file to a string
signature_code = signature_code.replace('Work_files/', signature_path) #Replaces local directory with full directory path
html_file.close()
olMailItem = 0x0
outlook = win32com.client.Dispatch("Outlook.Application")
newMail = outlook.CreateItem(olMailItem)
newMail.CC = "my#email.com"
newMail.Subject = subject
newMail.BodyFormat = 2 # olFormatHTML https://msdn.microsoft.com/en-us/library/office/aa219371(v=office.11).aspx
newMail.HTMLBody = "Email Message" + signature_code
newMail.display()
It seems I needed to replace the local path to the Signature files, with the absolute path in order to use images,etc.
sig_files_path = 'AppData\Roaming\Microsoft\Signatures\\' + signature_name + '_files\\'
sig_html_path = 'AppData\Roaming\Microsoft\Signatures\\' + signature_name + '.htm'
signature_path = os.path.join((os.environ['USERPROFILE']), sig_files_path) # Finds the path to Outlook signature files with signature name "Work"
html_doc = os.path.join((os.environ['USERPROFILE']),sig_html_path) #Specifies the name of the HTML version of the stored signature
html_doc = html_doc.replace('\\\\', '\\')
html_file = codecs.open(html_doc, 'r', 'utf-8', errors='ignore') #Opens HTML file and converts to UTF-8, ignoring errors
signature_code = html_file.read() #Writes contents of HTML signature file to a string
signature_code = signature_code.replace((signature_name + '_files/'), signature_path) #Replaces local directory with full directory path
html_file.close()
you should be able to do this if your signature is set as default.
>>> signature = message.body
>>> message.body = "ahoj\n" + signature
You acan also use message.HTMLbody if your signature contains picture.
Your signature should always apear in the message if you set it as default. You will save current content of the body to signature variable and then add it to the end of the message. Works for me at least.
I started to apply the code that the good samaritan linktotherescue posted us above.
After doing research on the inspector feature I could make it by doing another signature at Outlook and changing the current image to a text called {IMAGE} then with "Find" I used to search the text and replaced with the image from my original signature.
import win32com.client as win32
import os
import codecs
sig_files_path "C://Users//RenterSa//AppData//Roaming//Microsoft//Signatures//Technical Support Engineer_archivos"
sig_html_path = "C://Users//RenterSa//AppData//Roaming//Microsoft//Signatures//TSE (Python).htm"
img_path = r'C:\Users\RenterSa\AppData\Roaming\Microsoft\Signatures\Technical Support Engineer_archivos\image001.jpg'
signature_path = os.path.join((os.environ['USERPROFILE']), sig_files_path) # Finds the path to Outlook signature files with signature name "Work"
html_doc = os.path.join((os.environ['USERPROFILE']),sig_html_path) #Specifies the name of the HTML version of the stored signature
html_doc = html_doc.replace('\\\\', '\\')
html_file = codecs.open(html_doc, 'r', 'utf-8', errors='ignore') #Opens HTML file and converts to UTF-8, ignoring errors
signature_code = html_file.read() #Writes contents of HTML signature file to a string
signature_code = signature_code.replace((sig_html_path + sig_files_path),
signature_path) #Replaces local directory with full directory path
html_file.close()
olMailItem = 0x0
outlook = win32.Dispatch("Outlook.Application")
newMail = outlook.CreateItem(olMailItem)
newMail.CC = "my#email.com"
newMail.Subject = "subject"
newMail.Importance = 2
newMail.BodyFormat = 3 # olFormatHTML https://msdn.microsoft.com/en-us/library/office/aa219371(v=office.11).aspx
newMail.HTMLBody = "Email Message" + signature_code
inspector = newMail.GetInspector
newMail.display()
doc = inspector.WordEditor
selection = doc.Content
selection.Find.Text = r"{IMAGE}"
selection.Find.Execute()
selection.Text = ""
img = selection.InlineShapes.AddPicture(img_path, 0 , 1)

Categories