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
Showing
7 changed files
with
115 additions
and
0 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
Empty file.
Empty file.
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,18 @@ | ||
"""Mailtrap (SMTP) email backend class.""" | ||
from django.conf import settings | ||
from django.core.mail.backends.smtp import EmailBackend as SMTPBaseEmailBackend | ||
|
||
|
||
class EmailBackend(SMTPBaseEmailBackend): | ||
""" | ||
A wrapper that manages the Mailtrap SMTP network connection. | ||
""" | ||
def __init__(self, host=None, port=None, username=None, password=None, fail_silently=False, **kwargs): | ||
super().__init__( | ||
host="mailtrap.io", | ||
port=465, | ||
username=settings.MAILTRAP_EMAIL_HOST_USER if username is None else username, | ||
password=settings.MAILTRAP_EMAIL_HOST_PASSWORD if password is None else password, | ||
use_tls=True, | ||
fail_silently=fail_silently, | ||
) |
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,2 @@ | ||
def emailify_phone_number(phoneNumber): | ||
return "{}@localhost".format(phoneNumber) |
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,47 @@ | ||
from django.conf import settings | ||
|
||
from .base import BaseSMSBackend | ||
|
||
DEFAULT_SMS_OVER_EMAIL_BACKEND = "smsish.mail.backends.mailtrap.EmailBackend" | ||
|
||
|
||
class SMSBackend(BaseSMSBackend): | ||
def __init__(self, *args, **kwargs): | ||
super(SMSBackend, self).__init__(*args, **kwargs) | ||
|
||
def send_messages(self, sms_messages): | ||
results = [] | ||
for message in sms_messages: | ||
t = self.get_transform_function() | ||
email_message = t(message) | ||
result = email_message.send() | ||
results.append(result) | ||
|
||
return results | ||
|
||
def get_transform_function(self): | ||
return transform_sms_to_email_message | ||
|
||
|
||
def transform_sms_to_email_message(sms_message): | ||
from django.core.mail import get_connection | ||
from django.core.mail.message import EmailMessage | ||
from smsish.mail.utils import emailify_phone_number | ||
|
||
backend = getattr(settings, "SMSISH_MAILTRAP_SMS_BACKEND_EMAIL_BACKEND", DEFAULT_SMS_OVER_EMAIL_BACKEND) | ||
conn = get_connection(backend=backend) | ||
email = EmailMessage( | ||
subject="SMS over Email", | ||
body=sms_message.body, | ||
from_email=emailify_phone_number(sms_message.from_email), | ||
to=[emailify_phone_number(r) for r in sms_message.to], | ||
bcc=[emailify_phone_number(r) for r in sms_message.bcc] if sms_message.bcc else None, | ||
connection=conn, | ||
attachments=None, | ||
headers=None, | ||
cc=[emailify_phone_number(r) for r in sms_message.cc] if sms_message.cc else None, | ||
reply_to=[emailify_phone_number(sms_message.reply_to) for r in sms_message.reply_to] if sms_message.reply_to else None, | ||
) | ||
email.attach("metadata.txt", "Content-Length: {}".format(len(sms_message.body)), "text/plain") | ||
|
||
return email |
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