This repository has been archived by the owner on Aug 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[APIv5.8] Updated Timeout to Handle IP Quota, Introduced Gateways Wor…
…kflow. - IP Quota Timeout is updated to 259sec. - Introduced gateways Config Attr. - Abstracted Gateway.send_email method unto function. - Refactored Gateway class to single static method send to select gateway method.
- Loading branch information
1 parent
c2bb993
commit 2c300cb
Showing
3 changed files
with
39 additions
and
44 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
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 |
---|---|---|
@@ -1,51 +1,44 @@ | ||
from config import Config | ||
|
||
from twilio.rest import Client | ||
|
||
from email.mime.application import MIMEApplication | ||
from email.mime.multipart import MIMEMultipart | ||
from email.mime.text import MIMEText | ||
from email.utils import COMMASPACE, formatdate | ||
|
||
import smtplib | ||
|
||
def email_gateway(subject, addr_to, content, content_format='html', files=[]): | ||
if type(addr_to) == str: | ||
addr_to = [addr_to] | ||
addr_to = COMMASPACE.join(addr_to) | ||
|
||
msg = MIMEMultipart() | ||
msg['From'] = Config.email_auth['username'] | ||
msg['To'] = addr_to | ||
msg['Date'] = formatdate(localtime=True) | ||
msg['Subject'] = subject | ||
|
||
msg.attach(MIMEText(content, content_format)) | ||
|
||
for file in files: | ||
part = MIMEApplication(file['content'], Name=file['name']) | ||
part['Content-Disposition'] = 'attachment; filename="{}"'.format(file['name']) | ||
msg.attach(part) | ||
|
||
email_server = Config.email_auth['server'] | ||
email_username = Config.email_auth['username'] | ||
email_password = Config.email_auth['password'] | ||
|
||
smtp = smtplib.SMTP_SSL(email_server) | ||
smtp.login(email_username, email_password) | ||
smtp.sendmail(Config.email_auth['username'], addr_to, msg.as_string()) | ||
smtp.close() | ||
|
||
class Gateway: | ||
|
||
@classmethod | ||
def send_sms(cls, to, body): | ||
account_sid = Config.sms_auth['sid'] | ||
auth_token = Config.sms_auth['token'] | ||
client = Client(account_sid, auth_token) | ||
message = client.messages.create( | ||
body=body, | ||
from_=Config.sms_auth['number'], | ||
to=to | ||
) | ||
return message | ||
|
||
@classmethod | ||
def send_email(cls, subject, addr_to, content, content_format='html', files=[]): | ||
if type(addr_to) == str: | ||
addr_to = [addr_to] | ||
addr_to = COMMASPACE.join(addr_to) | ||
|
||
msg = MIMEMultipart() | ||
msg['From'] = Config.email_auth['username'] | ||
msg['To'] = addr_to | ||
msg['Date'] = formatdate(localtime=True) | ||
msg['Subject'] = subject | ||
|
||
msg.attach(MIMEText(content, content_format)) | ||
|
||
for file in files: | ||
part = MIMEApplication(file['content'], Name=file['name']) | ||
part['Content-Disposition'] = 'attachment; filename="{}"'.format(file['name']) | ||
msg.attach(part) | ||
|
||
email_server = Config.email_auth['server'] | ||
email_username = Config.email_auth['username'] | ||
email_password = Config.email_auth['password'] | ||
|
||
smtp = smtplib.SMTP_SSL(email_server) | ||
smtp.login(email_username, email_password) | ||
smtp.sendmail(Config.email_auth['username'], addr_to, msg.as_string()) | ||
smtp.close() | ||
@staticmethod | ||
def send(gateway, **kwargs): | ||
if gateway == 'email': | ||
return email_gateway(**kwargs) | ||
else: | ||
return Config.gateways[gateway](**kwargs) |