Skip to content

Instantly share code, notes, and snippets.

@choro3
Created January 14, 2013 06:35
Show Gist options
  • Save choro3/4528174 to your computer and use it in GitHub Desktop.
Save choro3/4528174 to your computer and use it in GitHub Desktop.
メール送信スクリプト
#!/usr/bin/env python
# coding: utf-8
import sys
import argparse
import smtplib
import email
import os
import os.path
def send_mail(from_addr, to_addr, text,
subject='Email from MyNotifier',
charset='utf8',
user=None,
password=None,
need_login=False,
server='smtp.gmail.com:587'):
msg = email.MIMEText.MIMEText(text.encode(charset), 'plain', charset)
msg['Subject'] = email.Header.Header(subject, charset)
msg['From'] = from_addr
msg['To'] = to_addr
msg['Date'] = email.Utils.formatdate(localtime=True)
smtp = smtplib.SMTP(server)
if need_login:
smtp.ehlo()
smtp.starttls()
smtp.ehlo()
smtp.login(user, password)
smtp.sendmail(from_addr, to_addr, msg.as_string())
smtp.close()
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument('--from-addr')
parser.add_argument('--to-addr')
parser.add_argument('--subject')
parser.add_argument('--message')
parser.add_argument('--user')
parser.add_argument('--password')
return parser.parse_args()
def parse_conf():
pass
def main():
if os.path.exists('$HOME/.mailnotify'):
conf = parse_conf('$HOME/.mailnotify')
args = parse_args()
send_mail(args.from_addr or '[email protected]',
args.to_addr or '',
args.message or 'Hello, This message was came from MyNotifier.',
subject=args.subject or 'E-mail Notification',
user=args.user or '[email protected]',
password=args.password or '',
need_login=True)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment