1111 * See the License for the specific language governing permissions and
1212 * limitations under the License.
1313 **/
14+ use Exception ;
1415use GuzzleHttp \Client ;
1516use GuzzleHttp \Exception \ClientException ;
1617use GuzzleHttp \Exception \RequestException ;
2223use libs \utils \ICacheService ;
2324use models \exceptions \ValidationException ;
2425/**
25- * Class MailApi
26+ * Class MailService
2627 * @package App\Services\Apis
2728 */
28- final class MailApi extends AbstractOAuth2Api implements IMailApi
29+ final class MailService extends AbstractOAuth2Api implements IMailApi, IEmailTemplatesApi
2930{
3031
3132 /**
@@ -107,7 +108,7 @@ public function sendEmail
107108 (
108109 sprintf
109110 (
110- "MailApi ::sendEmail template_identifier %s to_email %s payload %s " ,
111+ "MailService ::sendEmail template_identifier %s to_email %s payload %s " ,
111112 $ template_identifier ,
112113 $ to_email ,
113114 json_encode ($ payload )
@@ -132,12 +133,12 @@ public function sendEmail
132133 ];
133134
134135 if (!empty ($ cc_email )){
135- Log::debug (sprintf ("MailApi ::sendEmail setting cc_email %s " , $ cc_email ));
136+ Log::debug (sprintf ("MailService ::sendEmail setting cc_email %s " , $ cc_email ));
136137 $ payload ['cc_email ' ] = trim ($ cc_email );
137138 }
138139
139140 if (!empty ($ bcc_email )){
140- Log::debug (sprintf ("MailApi ::sendEmail setting bcc_email %s " , $ bcc_email ));
141+ Log::debug (sprintf ("MailService ::sendEmail setting bcc_email %s " , $ bcc_email ));
141142 $ payload ['bcc_email ' ] = trim ($ bcc_email );
142143 }
143144
@@ -152,7 +153,7 @@ public function sendEmail
152153 (
153154 sprintf
154155 (
155- "MailApi ::sendEmail template_identifier %s to_email %s body %s " ,
156+ "MailService ::sendEmail template_identifier %s to_email %s body %s " ,
156157 $ template_identifier ,
157158 $ to_email ,
158159 $ body
@@ -173,7 +174,7 @@ public function sendEmail
173174 (
174175 sprintf
175176 (
176- "MailApi ::sendEmail template_identifier %s to_email %s payload %s error %s " ,
177+ "MailService ::sendEmail template_identifier %s to_email %s payload %s error %s " ,
177178 $ template_identifier ,
178179 $ to_email ,
179180 json_encode ($ payload ),
@@ -188,4 +189,63 @@ public function sendEmail
188189 throw $ ex ;
189190 }
190191 }
192+
193+ /**
194+ * @param string $template_id
195+ * @return mixed
196+ * @throws \GuzzleHttp\Exception\GuzzleException
197+ * @throws \League\OAuth2\Client\Provider\Exception\IdentityProviderException
198+ */
199+ public function getEmailTemplate (string $ template_id ) {
200+ Log::debug ("MailService::getEmailTemplate " );
201+
202+ try {
203+ $ query = [
204+ 'access_token ' => $ this ->getAccessToken ()
205+ ];
206+
207+ $ response = $ this ->client ->get ("/api/v1/mail-templates/ {$ template_id }" , [
208+ 'query ' => $ query ,
209+ ]
210+ );
211+ return json_decode ($ response ->getBody ()->getContents (), true );
212+ }
213+ catch (Exception $ ex ) {
214+ $ this ->cleanAccessToken ();
215+ Log::error ($ ex );
216+ throw $ ex ;
217+ }
218+ }
219+
220+ /**
221+ * @param array $payload
222+ * @param string $html_template
223+ * @return mixed
224+ * @throws \GuzzleHttp\Exception\GuzzleException|\League\OAuth2\Client\Provider\Exception\IdentityProviderException
225+ */
226+ public function getEmailPreview (array $ payload , string $ html_template )
227+ {
228+ Log::debug ("MailService::getEmailPreview " );
229+
230+ try {
231+ $ query = [
232+ 'access_token ' => $ this ->getAccessToken ()
233+ ];
234+
235+ $ response = $ this ->client ->put ('/api/v1/mail-templates/all/render ' , [
236+ 'query ' => $ query ,
237+ RequestOptions::JSON => [
238+ "html " => $ html_template ,
239+ "payload " => $ payload
240+ ]
241+ ]
242+ );
243+ return json_decode ($ response ->getBody ()->getContents (), true );
244+ }
245+ catch (Exception $ ex ) {
246+ $ this ->cleanAccessToken ();
247+ Log::error ($ ex );
248+ throw $ ex ;
249+ }
250+ }
191251}
0 commit comments