Skip to content

Instantly share code, notes, and snippets.

@exemplum100
Created July 16, 2023 09:11
Show Gist options
  • Save exemplum100/99dda849798d7a225ddc01ccf1136c1d to your computer and use it in GitHub Desktop.
Save exemplum100/99dda849798d7a225ddc01ccf1136c1d to your computer and use it in GitHub Desktop.
Send file by outlook
import os
import shutil
import pprint
from datetime import datetime
import win32com.client as win32
#upd stamp
with open("lastupd.txt", "a") as f:
x = datetime.today().strftime('%Y-%m-%d %H:%M:%S')
f.write(x + '\n')
f.close
#del old dir from copy stage
try:
shutil.rmtree('./DescArc')
except:
pass
#Copy dir (from updir/to current folder)
shutil.copytree('../PyDir', './DescArc')
print('\nAFTER:')
pprint.pprint(os.listdir('./DescArc'))
#zip it!
shutil.make_archive('DescSendArc', 'zip', 'DescArc')
# send it!
olApp = win32.Dispatch('Outlook.Application')
olNS = olApp.GetNameSpace('MAPI') # NameSpaces need for attchm
subj1='DescArc [' + datetime.today().strftime('%Y-%m-%d %H:%M:%S')+']'
body1 = str(os.listdir('./DescArc'))
mailItem = olApp.CreateItem(0)
mailItem.Subject = subj1
mailItem.BodyFormat = 1
mailItem.Body = body1
mailItem.To = '@gmail.com' #may blocked some attachments or mark as spam
mailItem.Attachments.Add(os.path.join(os.getcwd(), 'DescSendArc.zip'))
mailItem.Send()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment