Last active
October 25, 2016 07:38
-
-
Save xiaotangyuan/e44a89f3a628c6a023c5f98a33b5c62c to your computer and use it in GitHub Desktop.
smtp发送邮件附件
This file contains hidden or 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 os | |
import smtplib | |
from email.mime.application import MIMEApplication | |
from email.mime.multipart import MIMEMultipart | |
from email.mime.text import MIMEText | |
from email.utils import COMMASPACE, formatdate | |
def send_mail(send_from, send_to, subject, text, files, | |
smtpserver, sender_username, sender_password): | |
assert isinstance(send_to, list) | |
msg = MIMEMultipart() | |
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 or []: | |
with open(f, "rb") as fil: | |
part = MIMEApplication( | |
fil.read(), | |
Name=os.path.basename(f) | |
) | |
part['Content-Disposition'] = 'attachment; filename="%s"' % os.path.basename(f) | |
msg.attach(part) | |
smtp = smtplib.SMTP_SSL(smtpserver, 465) | |
smtp.ehlo() | |
smtp.login(sender_username, sender_password) | |
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