Created
May 15, 2015 07:44
-
-
Save jongha/db98006e55ac05bfd6c8 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
import sys | |
import smtplib | |
import datetime | |
from optparse import OptionParser | |
from smtplib import SMTP as SMTP | |
from email.MIMEText import MIMEText | |
DEFAULT_FROM = 'no-reply@domain.com' | |
DEFAULT_DOMAIN = 'smtp.domain.com' | |
class SendMail(object): | |
username = None | |
password = None | |
host = None | |
port = None | |
def __init__(self, username, password, host, port=25): | |
self.username = username | |
self.password = password | |
self.host = host | |
self.port = port | |
def send(self, subject='', content='', recipient=None): | |
conn = SMTP(host=self.host, port=self.port) | |
conn.set_debuglevel(False) | |
if self.username and self.password: | |
conn.login(self.username, self.password) | |
msg = MIMEText(content) | |
msg['Subject'] = subject | |
msg['From'] = DEFAULT_FROM | |
msg['To'] = recipient | |
try: | |
conn.sendmail(self.username, recipient.split(','), msg.as_string()) | |
finally: | |
conn.close() | |
print 'Sending...' | |
if __name__ == '__main__': | |
parser = OptionParser() | |
parser.add_option('-s', '--subject', dest='subject', default='no subject', help='Subject of mail', metavar='SUBJECT') | |
parser.add_option('-r', '--recipients', dest='recipients', help='Recipients of mail') | |
parser.add_option('--host', dest='host', default=DEFAULT_DOMAIN, help='SMTP server host') | |
parser.add_option('--port', dest='port', default='25', help='SMTP server port') | |
parser.add_option('--username', dest='username', default=None, help='SMTP username') | |
parser.add_option('--password', dest='password', default=None, help='SMTP password') | |
(options, args) = parser.parse_args() | |
lines = [] | |
for line in sys.stdin: | |
stripped = line.strip() | |
#if not stripped: break | |
lines.append(stripped) | |
options.content = '\r\n'.join(lines) | |
sendmail = SendMail( | |
username=options.username, | |
password=options.password, | |
host=options.host, | |
port=options.port | |
) | |
sendmail.send(subject=options.subject, content=options.content, recipient=options.recipients) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment