Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
colin
committed
Jul 24, 2016
1 parent
f327c01
commit ef94bdf
Showing
11 changed files
with
97 additions
and
10 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
from .broadcaster import Broadcaster, SQLiteBroadcaster, BasicSQLiteBroadcaster # noqa | ||
from .broadcaster import Broadcaster, SQLiteBroadcaster, BasicSQLiteBroadcaster, EmailBroadcaster # noqa |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from .petemail import EmailMixin # noqa |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
from abc import abstractproperty | ||
from email.mime.multipart import MIMEMultipart | ||
from email.mime.text import MIMEText | ||
import json | ||
import smtplib | ||
|
||
|
||
def sample_config(): | ||
|
||
return json.dumps() | ||
|
||
|
||
class EmailMixin(object): | ||
""" | ||
Helps sending emails. Requires a config file json file that looks something like this: | ||
{ | ||
"username": "myuser@gmail.com", | ||
"from": "myuser@gmail.com", | ||
"password": "mypassword", | ||
"smtp": "smtp.gmail.com:587", | ||
"to": [ | ||
"myuser@gmail.com", | ||
"youruser@gmail.com" | ||
] | ||
} | ||
""" | ||
required_keys = ('from', 'to', 'smtp', 'username', 'password') | ||
|
||
@abstractproperty | ||
def email_config_filename(self): | ||
return None | ||
|
||
@abstractproperty | ||
def subject_formatter(self): | ||
return None | ||
|
||
@abstractproperty | ||
def message_formatter(self): | ||
return None | ||
|
||
def _get_config(self): | ||
with open(self.email_config_filename, 'r') as buff: | ||
config = json.load(buff) | ||
for required_key in EmailMixin.required_keys: | ||
if required_key not in config: | ||
raise KeyError('Config must include {}'.format(required_key)) | ||
|
||
def send_message(self, message_dict): | ||
"""Email a message given a dictionary. | ||
Formatting is set using EmailMixin.subject_formatter and | ||
EmailMixin.message_formatter, and configuration is loaded from | ||
EmailMixin.email_config_filename. | ||
Args: | ||
message_json: A json string defining a message. | ||
""" | ||
config = self._get_config() | ||
from_email = config['from'] | ||
to_emails = config['to'] | ||
|
||
message = json.loads(message_json) | ||
|
||
msg = MIMEMultipart('alternative') | ||
msg['Subject'] = self.subject_formatter.format(**message) | ||
msg['From'] = from_email | ||
msg['To'] = ",".join(to_emails) | ||
|
||
msg.attach(MIMEText(self.message_formatter.format(**message), 'plain')) | ||
|
||
server = smtplib.SMTP(config['smtp']) | ||
server.ehlo() | ||
server.starttls() | ||
server.login(config['username'], config['password']) | ||
server.sendmail(from_email, to_emails, msg.as_string()) | ||
server.quit() |
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
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
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
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
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
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