Skip to content

Instantly share code, notes, and snippets.

@codeflitting
Last active August 29, 2015 14:08
Show Gist options
  • Save codeflitting/3a0cc8ccb80c1d9cdae8 to your computer and use it in GitHub Desktop.
Save codeflitting/3a0cc8ccb80c1d9cdae8 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
#coding: utf-8
import smtplib, argparse, os
from email.mime.text import MIMEText
from email.header import Header
class Config:
def __init__(self):
fn = os.path.join(os.path.abspath(os.path.split(__file__)[0]), "mail.conf")
print "Reading configuration from %s" % fn
for line in open(fn).readlines():
line = line.strip()
if not line or line.find('#') != -1:
continue
key, value = [x.strip() for x in line.split("=", 2)]
setattr(self, key, value)
def get_args(self):
parser = argparse.ArgumentParser()
parser.add_argument("-s", "--subject", help="what subject do you want ?", default='')
parser.add_argument("-r", "--receiver", help="Who do you want to send the message?", default='')
parser.add_argument("-m", "--message", help="what the message is ?", default='')
return parser.parse_args()
def main():
conf = Config()
args = conf.get_args()
msg = MIMEText(args.message,'plain','utf-8') #中文需参数‘utf-8’,单字节字符不需要
msg['Subject'] = Header(args.subject, 'utf-8')
smtp = smtplib.SMTP()
smtp.connect(conf.smtpserver)
smtp.login(conf.username, conf.password)
smtp.sendmail(conf.sender, args.receiver, msg.as_string())
smtp.quit()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment