Skip to content

Commit fa4b70f

Browse files
author
Luciano Vitetti
committed
Added support for multiple recipients
1 parent 5cb2f16 commit fa4b70f

File tree

5 files changed

+74
-7
lines changed

5 files changed

+74
-7
lines changed

README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,29 @@ params = MailParams(
3535

3636
estr.mail.send(params)
3737
```
38+
39+
## Multiple Recipients Usage
40+
41+
```python
42+
from envialosimple.transaccional import Transaccional
43+
from envialosimple.transaccional.mail import MailParams
44+
45+
estr = Transaccional(your_api_key)
46+
47+
params = MailParams(
48+
from_email='no-reply@mycompany.com',
49+
from_name='MyCompany Notifications',
50+
to_email=[
51+
{"email": 'jane.doe@example.com', "name": 'Jane Doe'},
52+
{"email": 'sean.smith@example.com', "name": 'Sean Smith'},
53+
{"email": 'john.doe@example.com'}
54+
],
55+
reply_to='reply@here.com',
56+
subject='This is a test for {{name}}',
57+
preview_text='A glimpse of what comes next...',
58+
html='<h1>HTML emails are cool, {{name}}</h1>',
59+
text='Text emails are also cool, {{name}}',
60+
context={'name': 'John'})
61+
62+
estr.mail.send(params)
63+
```

src/envialosimple/transaccional/http.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ def post(self, url: str, data: dict) -> tuple:
2121

2222
if http_code == 500:
2323
raise ValueError(f'The server responded with code {http_code}')
24-
25-
if type(json_response) is not dict:
24+
25+
if type(json_response) is not dict and type(json_response) is not list:
2626
raise ValueError('The server did not respond with JSON')
2727

2828
return (http_code, json_response)

src/envialosimple/transaccional/mail.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import base64
22
from warnings import warn
3-
from typing import TypeVar, TypedDict, Union
3+
from typing import TypeVar, TypedDict, Union, List
44
from typing_extensions import Unpack
55

66
SelfMailParams = TypeVar("SelfMailParams", bound="MailParams")
@@ -13,10 +13,15 @@ class AttachmentParams(TypedDict):
1313
filename: Union[str, None]
1414

1515

16+
class Recipient(TypedDict):
17+
email: str
18+
name: Union[str, None]
19+
20+
1621
class MailParamsParams(TypedDict):
1722
from_email: Union[str, None]
1823
from_name: Union[str, None]
19-
to_email: Union[str, None]
24+
to_email: Union[str, None, List[Recipient]]
2025
to_name: Union[str, None]
2126
subject: Union[str, None]
2227
reply_to: Union[str, None]
@@ -93,7 +98,7 @@ def __init__(self, **kwargs: Unpack[MailParamsParams]):
9398
self.variables = kwargs.get('variables', {})
9499

95100
def setTo(self,
96-
to_email: str,
101+
to_email: Union[str, List[Recipient]],
97102
to_name: Union[str, None]) -> SelfMailParams:
98103
self.to_email = to_email
99104
self.to_name = to_name
@@ -197,8 +202,8 @@ def to_dict(self) -> dict:
197202
'name': self.from_name
198203
}
199204

200-
# Pass only email if no name was provided
201-
if self.to_name is None:
205+
# Pass only email if no name was provided or if email is a list with emails
206+
if self.to_name is None or isinstance(self.to_email, List):
202207
result['to'] = self.to_email
203208
else:
204209
result['to'] = {

tests/.env.dist

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
TEST_API_KEY=SomeValidAPIKey
22
TEST_FROM_EMAIL=SomeValid@FromAddress
33
TEST_TO_EMAIL=SomeValid@ToAddress
4+
TEST_SECOND_TO_EMAIL=SomeValid@ToAddress
5+
TEST_THIRD_TO_EMAIL=SomeValid@ToAddress
46
TEST_FROM_NAME="John Smith"
57
TEST_TO_NAME="John Doe"
8+
TEST_SECOND_TO_NAME="John Doe"
9+
TEST_THIRD_TO_NAME="John Doe"
610
TEST_TEMPLATE_ID=SomeValidTemplateID
711
TEST_INVALID_FROM_EMAIL=somename@mail.com
812
TEST_SUBJECT="PyTest Session"

tests/test_mail.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
import os
22
import pytest
3+
from typing import List
34
from envialosimple.transaccional import Transaccional
45
from envialosimple.transaccional.mail import MailParams, Attachment
56

67
api_key = os.environ.get('TEST_API_KEY')
78
from_email = os.environ.get('TEST_FROM_EMAIL')
89
from_name = os.environ.get('TEST_FROM_NAME')
910
to_email = os.environ.get('TEST_TO_EMAIL')
11+
second_to_email = os.environ.get('TEST_SECOND_TO_EMAIL')
12+
third_to_email = os.environ.get('TEST_THIRD_TO_EMAIL')
1013
to_name = os.environ.get('TEST_TO_NAME')
14+
second_to_name = os.environ.get('TEST_SECOND_TO_NAME')
15+
third_to_name = os.environ.get('TEST_THIRD_TO_NAME')
1116
subject = os.environ.get('TEST_SUBJECT')
1217
template_id = os.environ.get('TEST_TEMPLATE_ID')
1318
reply_to = os.environ.get('TEST_REPLY_TO')
@@ -112,6 +117,33 @@ def test_basic_send():
112117
assert (outcome['queued'] is True)
113118

114119

120+
def test_multiple_to_send():
121+
"""
122+
Send an E-Mail with Multiple recipients
123+
"""
124+
params = MailParams()
125+
params.from_email = from_email
126+
params.from_name = from_name
127+
params.to_email = [
128+
{"email": to_email, "name": to_name},
129+
{"email": second_to_email, "name": second_to_name},
130+
{"email": third_to_email}
131+
]
132+
params.subject = subject
133+
params.reply_to = reply_to
134+
params.preview_text = preview_text
135+
params.html = "<body>PyTest Multiple recipients {{sub}}</body>"
136+
params.text = "PyTest Multiple recipients {{sub}}"
137+
params.context = {'sub': 'substitution'}
138+
139+
outcome = estr.mail.send(params)
140+
141+
assert (type(outcome) is list)
142+
assert ('id' in outcome[0])
143+
assert ('queued' in outcome[0])
144+
assert (outcome[0]['queued'] is True)
145+
146+
115147
def test_attachment_send():
116148
"""
117149
Send E-Mail with attachment

0 commit comments

Comments
 (0)