Skip to content

Commit

Permalink
Add support for verification codes
Browse files Browse the repository at this point in the history
  • Loading branch information
jm-mailosaur committed Mar 8, 2022
1 parent fa99ac0 commit 941ac73
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 14 deletions.
2 changes: 2 additions & 0 deletions mailosaur/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from .mailosaur_exception import MailosaurException
from .message_address import MessageAddress
from .link import Link
from .code import Code
from .image import Image
from .message_content import MessageContent
from .attachment import Attachment
Expand Down Expand Up @@ -31,6 +32,7 @@
'MailosaurException',
'MessageAddress',
'Link',
'Code',
'Image',
'MessageContent',
'Attachment',
Expand Down
12 changes: 12 additions & 0 deletions mailosaur/models/code.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class Code(object):
"""Code.
:param value:
:type value: str
"""

def __init__(self, data=None):
if data is None:
data = {}

self.value = data.get('value', None)
5 changes: 5 additions & 0 deletions mailosaur/models/message_content.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
from .link import Link
from .code import Code
from .image import Image


class MessageContent(object):
"""MessageContent.
:param links:
:type links: list[~mailosaur.models.Link]
:param codes:
:type codes: list[~mailosaur.models.Code]
:param images:
:type images: list[~mailosaur.models.Image]
:param body:
Expand All @@ -17,6 +21,7 @@ def __init__(self, data=None):
data = {}

self.links = [Link(i) for i in data.get('links', [])]
self.codes = [Code(i) for i in data.get('codes', [])]

images = data.get('images', None)
if isinstance(images, list):
Expand Down
10 changes: 10 additions & 0 deletions tests/emails_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,11 @@ def validate_html(self, email):
self.assertEqual("http://invalid/", email.html.links[2].href)
self.assertEqual("invalid", email.html.links[2].text)

# Html.Codes
self.assertEqual(2, len(email.html.codes))
self.assertEqual("123456", email.html.codes[0].value)
self.assertEqual("G3H1Y2", email.html.codes[1].value)

# Html.Images
self.assertTrue(email.html.images[1].src.startswith('cid:'))
self.assertEqual("Inline image 1", email.html.images[1].alt)
Expand All @@ -328,6 +333,11 @@ def validate_text(self, email):
self.assertEqual("https://mailosaur.com/", email.text.links[1].href)
self.assertEqual(email.text.links[1].href, email.text.links[1].text)

# Text.Codes
self.assertEqual(2, len(email.text.codes))
self.assertEqual("654321", email.text.codes[0].value)
self.assertEqual("5H0Y2", email.text.codes[1].value)

def validate_headers(self, email):
expected_from_header = "%s <%s>" % (
email.sender[0].name, email.sender[0].email)
Expand Down
34 changes: 22 additions & 12 deletions tests/mailer.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@
from email.mime.multipart import MIMEMultipart
from email.mime.image import MIMEImage


class Mailer(object):
html = open(os.path.join(os.path.dirname(__file__), 'resources', 'testEmail.html'), 'r').read()
text = open(os.path.join(os.path.dirname(__file__), 'resources', 'testEmail.txt'), 'r').read()
html = open(os.path.join(os.path.dirname(__file__),
'resources', 'testEmail.html'), 'r').read()
text = open(os.path.join(os.path.dirname(__file__),
'resources', 'testEmail.txt'), 'r').read()
verified_domain = os.getenv('MAILOSAUR_VERIFIED_DOMAIN') or "mailosaur.net"

@staticmethod
Expand All @@ -18,40 +21,47 @@ def send_emails(client, server, quantity):
Mailer.send_email(client, server)

@staticmethod
def send_email(client, server, send_to_address = None):
def send_email(client, server, send_to_address=None):
host = os.getenv('MAILOSAUR_SMTP_HOST', 'mailosaur.net')
port = os.getenv('MAILOSAUR_SMTP_PORT', '25')

message = MIMEMultipart('related')

randomString = ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(10))
randomString = ''.join(random.choice(
string.ascii_uppercase + string.ascii_lowercase) for _ in range(10))

message['Subject'] = "%s subject" % (randomString)

random_to_address = send_to_address
if (random_to_address == None):
random_to_address = client.servers.generate_email_address(server)

message['From'] = "%s %s <%s@%s>" % (randomString, randomString, randomString, Mailer.verified_domain)
message['To'] = "%s %s <%s>" % (randomString, randomString, random_to_address)

message['From'] = "%s %s <%s@%s>" % (randomString,
randomString, randomString, Mailer.verified_domain)
message['To'] = "%s %s <%s>" % (
randomString, randomString, random_to_address)

alt = MIMEMultipart('alternative')
message.attach(alt)

# Text body
alt.attach(MIMEText(Mailer.text.replace("REPLACED_DURING_TEST", randomString)))
alt.attach(MIMEText(Mailer.text.replace(
"REPLACED_DURING_TEST", randomString)))

# Html body
alt.attach(MIMEText(Mailer.html.replace("REPLACED_DURING_TEST", randomString), 'html'))
alt.attach(MIMEText(Mailer.html.replace(
"REPLACED_DURING_TEST", randomString), 'html'))

fp = open(os.path.join(os.path.dirname(__file__), 'resources', 'cat.png'), 'rb')
fp = open(os.path.join(os.path.dirname(
__file__), 'resources', 'cat.png'), 'rb')
img = MIMEImage(fp.read())
img.add_header('Content-ID', 'ii_1435fadb31d523f6')
img.add_header('Content-Disposition', 'inline', filename='cat.png')
message.attach(img)
fp.close()

fp = open(os.path.join(os.path.dirname(__file__), 'resources', 'dog.png'), 'rb')
fp = open(os.path.join(os.path.dirname(
__file__), 'resources', 'dog.png'), 'rb')
img = MIMEImage(fp.read())
img.add_header('Content-Disposition', 'attachment', filename='dog.png')
message.attach(img)
Expand Down
10 changes: 9 additions & 1 deletion tests/resources/testEmail.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@
<img src="cid:ii_1435fadb31d523f6" alt="Inline image 1">
</a>
</div>
<div>
Your verification code is 123456
<br>
</div>
<div>
Your special ID is G3H1Y2
<br>
</div>
<div>
<br>
</div>
Expand All @@ -25,4 +33,4 @@
</div>
</div>
</div>
</div>
</div>
6 changes: 5 additions & 1 deletion tests/resources/testEmail.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,8 @@ REPLACED_DURING_TEST text

this is an image:[image: Inline image 1] <https://mailosaur.com/>

this is an invalid link: invalid
Your verification code is 654321

Your special ID is 5H0Y2

this is an invalid link: invalid

0 comments on commit 941ac73

Please sign in to comment.