Skip to content

Instantly share code, notes, and snippets.

@cocodrips
Created March 16, 2017 04:05
Show Gist options
  • Save cocodrips/2faf1b425b100cdf826e64d9a7edd901 to your computer and use it in GitHub Desktop.
Save cocodrips/2faf1b425b100cdf826e64d9a7edd901 to your computer and use it in GitHub Desktop.
Send mail
# -*- coding: utf-8 -*-
import smtplib
from email.mime.text import MIMEText
from email.header import Header
server = 'localhost'
def send(send_from, send_tos, subject, body):
"""
:type send_from: string
:type send_tos: list of string
:type subject: string
:type body: string or file
:return:
"""
_body = ""
if isinstance(body, file):
with open(body, 'r') as f:
_body = f.read()
if isinstance(body, basestring):
_body = body
# with smtplib.SMTP(server, port=port) as s:
s = smtplib.SMTP(server)
s.set_debuglevel(True)
cset = 'utf-8'
message = MIMEText(_body, _subtype='plain', _charset=cset)
message['Subject'] = Header(subject, charset=cset)
message['From'] = send_from
message['To'] = send_tos[0]
s.sendmail(send_from, send_tos, message.as_string())
s.close()
if __name__ == '__main__':
send('[email protected]', ['[email protected]'], 'test', 'test mail')
# Reference
# http://qiita.com/yasunori/items/265d8db746742bb967c4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment