Skip to content

Instantly share code, notes, and snippets.

@b1
Created April 19, 2013 10:34
Show Gist options
  • Save b1/5419532 to your computer and use it in GitHub Desktop.
Save b1/5419532 to your computer and use it in GitHub Desktop.
Basic script for sending excel over smtp
import smtplib, os
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
from email.Utils import COMMASPACE, formatdate
from email import Encoders
def send_mail(send_from, send_to, subject="", text="", files=None, server='smtp.google.ru'):
if files == None:
files = []
assert type(send_to)==list
assert type(files)==list
msg = MIMEMultipart('mixed')
msg['From'] = send_from
msg['To'] = COMMASPACE.join(send_to)
msg['Date'] = formatdate(localtime=True)
msg['Subject'] = subject
msg.attach( MIMEText(text) )
for f in files:
part = MIMEBase('application','msexcel')
content = None
with open(f, 'rb') as fi:
content = fi.read()
fi.close()
part.set_payload(content)
Encoders.encode_base64(part)
part.add_header('Content-Disposition', 'attachment', filename=os.path.basename(f))
msg.attach(part)
smtp = smtplib.SMTP(server)
smtp.sendmail(send_from, send_to, msg.as_string())
smtp.close()
@sergshabal
Copy link

wawawa

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment