5
5
from .content import Content
6
6
from .subject import Subject
7
7
8
-
9
8
class Mail (object ):
10
9
"""Creates the response body for v3/mail/send"""
11
10
def __init__ (
@@ -36,7 +35,7 @@ def __init__(
36
35
self ._contents = None
37
36
self ._custom_args = None
38
37
self ._headers = None
39
- self ._personalizations = None
38
+ self ._personalizations = []
40
39
self ._sections = None
41
40
self ._asm = None
42
41
self ._batch_id = None
@@ -55,38 +54,7 @@ def __init__(
55
54
if subject is not None :
56
55
self .subject = subject
57
56
if to_emails is not None :
58
- if is_multiple == True :
59
- if isinstance (to_emails , list ):
60
- for email in to_emails :
61
- personalization = Personalization ()
62
- personalization .add_to (email )
63
- self .add_personalization (personalization )
64
- else :
65
- personalization = Personalization ()
66
- personalization .add_to (to_emails )
67
- self .add_personalization (personalization )
68
- if global_substitutions is not None :
69
- if isinstance (global_substitutions , list ):
70
- for substitution in global_substitutions :
71
- for p in self .personalizations :
72
- p .add_substitution (substitution )
73
- else :
74
- for p in self .personalizations :
75
- p .add_substitution (global_substitutions )
76
- else :
77
- personalization = Personalization ()
78
- if isinstance (to_emails , list ):
79
- for email in to_emails :
80
- personalization .add_to (email )
81
- else :
82
- personalization .add_to (to_emails )
83
- if global_substitutions is not None :
84
- if isinstance (global_substitutions , list ):
85
- for substitution in global_substitutions :
86
- personalization .add_substitution (substitution )
87
- else :
88
- personalization .add_substitution (global_substitutions )
89
- self .add_personalization (personalization )
57
+ self ._set_emails (to_emails , global_substitutions , is_multiple )
90
58
if plain_text_content is not None :
91
59
self .add_content (plain_text_content )
92
60
if html_content is not None :
@@ -95,9 +63,9 @@ def __init__(
95
63
def __str__ (self ):
96
64
return str (self .get ())
97
65
98
- def _ensure_append (self , new_items , append_to ):
66
+ def _ensure_append (self , new_items , append_to , index = 0 ):
99
67
append_to = append_to or []
100
- append_to .append ( new_items )
68
+ append_to .insert ( index , new_items )
101
69
return append_to
102
70
103
71
def _ensure_insert (self , new_items , insert_to ):
@@ -112,6 +80,75 @@ def _flatten_dicts(self, dicts):
112
80
def _get_or_none (self , from_obj ):
113
81
return from_obj .get () if from_obj is not None else None
114
82
83
+ def _set_emails (self , emails , global_substitutions = None , is_multiple = False , p = 0 ):
84
+ # Send Multiple Emails to Multiple Recipients
85
+ if is_multiple == True :
86
+ if isinstance (emails , list ):
87
+ for email in emails :
88
+ if p == 0 and self ._personalizations [p ] == None :
89
+ personalization = Personalization ()
90
+ self .add_personalization (personalization , index = p )
91
+ else :
92
+ self ._personalizations [p ].add_email (email )
93
+ else :
94
+ personalization = Personalization ()
95
+ personalization .add_email (emails )
96
+ self .add_personalization (personalization )
97
+ if global_substitutions is not None :
98
+ if isinstance (global_substitutions , list ):
99
+ for substitution in global_substitutions :
100
+ for p in self .personalizations :
101
+ p .add_substitution (substitution )
102
+ else :
103
+ for p in self .personalizations :
104
+ p .add_substitution (global_substitutions )
105
+ else :
106
+ try :
107
+ personalization = self ._personalizations [p ]
108
+ has_internal_personalization = True
109
+ except IndexError :
110
+ personalization = Personalization ()
111
+ has_internal_personalization = False
112
+
113
+ if isinstance (emails , list ):
114
+ for email in emails :
115
+ personalization .add_email (email )
116
+ else :
117
+ personalization .add_email (emails )
118
+ if global_substitutions is not None :
119
+ if isinstance (global_substitutions , list ):
120
+ for substitution in global_substitutions :
121
+ personalization .add_substitution (substitution )
122
+ else :
123
+ personalization .add_substitution (global_substitutions )
124
+
125
+ if not has_internal_personalization :
126
+ self .add_personalization (personalization )
127
+
128
+ @property
129
+ def to (self ):
130
+ pass
131
+
132
+ @to .setter
133
+ def to (self , to_emails , global_substitutions = None , is_multiple = False , p = 0 ):
134
+ self ._set_emails (to_emails , None , is_multiple = is_multiple , p = p )
135
+
136
+ @property
137
+ def cc (self ):
138
+ pass
139
+
140
+ @cc .setter
141
+ def cc (self , bcc_emails , global_substitutions = None , is_multiple = False , p = 0 ):
142
+ self ._set_emails (bcc_emails , None , is_multiple = is_multiple , p = p )
143
+
144
+ @property
145
+ def bcc (self ):
146
+ pass
147
+
148
+ @bcc .setter
149
+ def bcc (self , bcc_emails , global_substitutions = None , is_multiple = False , p = 0 ):
150
+ self ._set_emails (bcc_emails , None , is_multiple = is_multiple , p = p )
151
+
115
152
@property
116
153
def attachments (self ):
117
154
return self ._attachments
@@ -148,6 +185,14 @@ def add_content(self, content):
148
185
def headers (self ):
149
186
return self ._headers
150
187
188
+ @property
189
+ def header (self ):
190
+ pass
191
+
192
+ @header .setter
193
+ def header (self , header ):
194
+ self .add_header (header )
195
+
151
196
def add_header (self , header ):
152
197
if isinstance (header , dict ):
153
198
(k , v ) = list (header .items ())[0 ]
@@ -159,9 +204,9 @@ def add_header(self, header):
159
204
def personalizations (self ):
160
205
return self ._personalizations
161
206
162
- def add_personalization (self , personalizations ):
207
+ def add_personalization (self , personalizations , index = 0 ):
163
208
self ._personalizations = self ._ensure_append (
164
- personalizations , self ._personalizations )
209
+ personalizations , self ._personalizations , index )
165
210
166
211
@property
167
212
def sections (self ):
0 commit comments