class SMTPPlugin(object): def __init__(self, config): self.outbox = Outbox(server=config.host, username=config.user, password=config.password, port=config.get('port', 25), mode=config.get('mode', None)) def send(self, subject, message, recipients): self.outbox.send(Email(subject=subject, html_body=message, recipients=recipients))
def _send_email(config_file, subject, body): with open(config_file, 'r') as f: config = json.load(f) port = config["port"] server = config["server"] fromaddr = config["fromaddr"] toaddrs = config["toaddrs"] username = config["username"] password = config["password"] outbox = Outbox(username=username, password=password, server=server, port=port) email = Email(subject=subject, body=body, recipients=toaddrs) outbox.send(email)
def test_outbox_send(): message = Email(['*****@*****.**'], 'subject', 'body') o = Outbox('username', 'password', 'server', 1234, debug=True) import email.mime.multipart with mock.patch.object(email.mime.multipart.MIMEMultipart, 'as_string', lambda self: 'foo'): with mock.patch('smtplib.SMTP') as SMTP: smtp = SMTP.return_value o.send(message, [Attachment('foo', fileobj=StringIO('foo'))]) smtp.set_debuglevel.assert_any_call(True) smtp.starttls.assert_any_call() smtp.login.assert_any_call('username', 'password') smtp.sendmail.assert_any_call('username', message.recipients, 'foo') smtp.quit.assert_any_call()
def send_email(): outbox = Outbox(username=config.username, password=config.password,server=config.server, port=config.port) conn = sqlite3.connect(config.database_directory+'article_db') cursor = conn.cursor() to_send = cursor.execute('SELECT headline,rubric,body,rowid FROM article_tbl WHERE delivered = "False"').fetchall() if len(to_send) > 0: html_email = PyH('Economist Articles') html_email << a(img(src="http://www.economist.com/rights/images/logo+economist.gif"),name="top") contents = html_email << div() for i, thing in enumerate(to_send): content_object = contents << div() content_object << a(thing[0],href="#%i" % i,style="font-family:Verdana;color:blue") content_object << br() content_object << span(" %s" % thing[1],style="font-family:Verdana;") content_object << p('') html_email << h2(thing[0],style="font-family:Verdana;") << a(name="%i" % i) html_email << h3(thing[1],style="font-family:Verdana;") html_email << p(thing[2],style="font-family:Verdana;") html_email << p('') << a("Top",href="#top",style="font-family:Verdana;") html_email << p('') html_email << p('Made with <3 by oneschirm',style="font-family:Verdana;") outbox.send(Email(subject='Economist Articles - %s' % datetime.strftime(datetime.today(),'%m/%d/%Y, %H:%M %Z'), html_body=html_email.render(),recipients=config.recipients)) for thing in to_send: cursor.execute('UPDATE article_tbl SET delivered="True" WHERE rowid = ?', (thing[3],)) conn.commit() conn.close()