@@ -79,6 +79,51 @@ protected function process(EmailMessage $message): array
7979 }
8080 }
8181
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+
82127 $ response = new Response ($ this ->getType ());
83128 $ result = $ this ->request (
84129 method: 'POST ' ,
@@ -87,40 +132,24 @@ protected function process(EmailMessage $message): array
87132 'Authorization: Bearer ' .$ this ->apiKey ,
88133 'Content-Type: application/json ' ,
89134 ],
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 ,
107136 );
108137
109138 $ statusCode = $ result ['statusCode ' ];
110139
111140 if ($ statusCode === 202 ) {
112141 $ response ->setDeliveredTo (\count ($ message ->getTo ()));
113142 foreach ($ message ->getTo () as $ to ) {
114- $ response ->addResultForRecipient ($ to );
143+ $ response ->addResult ($ to );
115144 }
116145 } else {
117146 foreach ($ message ->getTo () as $ to ) {
118147 if (\is_string ($ result ['response ' ])) {
119- $ response ->addResultForRecipient ($ to , $ result ['response ' ]);
148+ $ response ->addResult ($ to , $ result ['response ' ]);
120149 } 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 ' ]);
122151 } else {
123- $ response ->addResultForRecipient ($ to , 'Unknown error ' );
152+ $ response ->addResult ($ to , 'Unknown error ' );
124153 }
125154 }
126155 }
0 commit comments