Created
April 19, 2013 10:34
-
-
Save b1/5419532 to your computer and use it in GitHub Desktop.
Basic script for sending excel over smtp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
wawawa