@@ -79,6 +79,51 @@ protected function process(EmailMessage $message): array
79
79
}
80
80
}
81
81
82
+ $ attachments = [];
83
+
84
+ if (!\is_null ($ message ->getAttachments ())) {
85
+ $ size = 0 ;
86
+
87
+ foreach ($ message ->getAttachments () as $ attachment ) {
88
+ $ size += \filesize ($ attachment ->getPath ());
89
+ }
90
+
91
+ if ($ size > self ::MAX_ATTACHMENT_BYTES ) {
92
+ throw new \Exception ('Attachments size exceeds the maximum allowed size of 25MB ' );
93
+ }
94
+
95
+ foreach ($ message ->getAttachments () as $ attachment ) {
96
+ $ attachments [] = [
97
+ 'content ' => \base64_encode (\file_get_contents ($ attachment ->getPath ())),
98
+ 'filename ' => $ attachment ->getName (),
99
+ 'type ' => $ attachment ->getType (),
100
+ 'disposition ' => 'attachment ' ,
101
+ ];
102
+ }
103
+ }
104
+
105
+ $ body = [
106
+ 'personalizations ' => $ personalizations ,
107
+ 'reply_to ' => [
108
+ 'name ' => $ message ->getReplyToName (),
109
+ 'email ' => $ message ->getReplyToEmail (),
110
+ ],
111
+ 'from ' => [
112
+ 'name ' => $ message ->getFromName (),
113
+ 'email ' => $ message ->getFromEmail (),
114
+ ],
115
+ 'content ' => [
116
+ [
117
+ 'type ' => $ message ->isHtml () ? 'text/html ' : 'text/plain ' ,
118
+ 'value ' => $ message ->getContent (),
119
+ ],
120
+ ],
121
+ ];
122
+
123
+ if (!empty ($ attachments )) {
124
+ $ body ['attachments ' ] = $ attachments ;
125
+ }
126
+
82
127
$ response = new Response ($ this ->getType ());
83
128
$ result = $ this ->request (
84
129
method: 'POST ' ,
@@ -87,40 +132,24 @@ protected function process(EmailMessage $message): array
87
132
'Authorization: Bearer ' .$ this ->apiKey ,
88
133
'Content-Type: application/json ' ,
89
134
],
90
- body: \json_encode ([
91
- 'personalizations ' => $ personalizations ,
92
- 'reply_to ' => [
93
- 'name ' => $ message ->getReplyToName (),
94
- 'email ' => $ message ->getReplyToEmail (),
95
- ],
96
- 'from ' => [
97
- 'name ' => $ message ->getFromName (),
98
- 'email ' => $ message ->getFromEmail (),
99
- ],
100
- 'content ' => [
101
- [
102
- 'type ' => $ message ->isHtml () ? 'text/html ' : 'text/plain ' ,
103
- 'value ' => $ message ->getContent (),
104
- ],
105
- ],
106
- ]),
135
+ body: $ body ,
107
136
);
108
137
109
138
$ statusCode = $ result ['statusCode ' ];
110
139
111
140
if ($ statusCode === 202 ) {
112
141
$ response ->setDeliveredTo (\count ($ message ->getTo ()));
113
142
foreach ($ message ->getTo () as $ to ) {
114
- $ response ->addResultForRecipient ($ to );
143
+ $ response ->addResult ($ to );
115
144
}
116
145
} else {
117
146
foreach ($ message ->getTo () as $ to ) {
118
147
if (\is_string ($ result ['response ' ])) {
119
- $ response ->addResultForRecipient ($ to , $ result ['response ' ]);
148
+ $ response ->addResult ($ to , $ result ['response ' ]);
120
149
} elseif (!\is_null ($ result ['response ' ]['errors ' ][0 ]['message ' ] ?? null )) {
121
- $ response ->addResultForRecipient ($ to , $ result ['response ' ]['errors ' ][0 ]['message ' ]);
150
+ $ response ->addResult ($ to , $ result ['response ' ]['errors ' ][0 ]['message ' ]);
122
151
} else {
123
- $ response ->addResultForRecipient ($ to , 'Unknown error ' );
152
+ $ response ->addResult ($ to , 'Unknown error ' );
124
153
}
125
154
}
126
155
}
0 commit comments