| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * Copyright (C) 2020-2025 Iain Cambridge | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * This program is free software: you can redistribute it and/or modify | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * it under the terms of the GNU LESSER GENERAL PUBLIC LICENSE as published by | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * the Free Software Foundation, either version 2.1 of the License, or | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * (at your option) any later version. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * This program is distributed in the hope that it will be useful, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * GNU Lesser General Public License for more details. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * You should have received a copy of the GNU General Public License | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  * along with this program.  If not, see <https://www.gnu.org/licenses/>. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | namespace Parthenon\Notification\Sender; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | use Parthenon\Common\LoggerAwareTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | use Parthenon\Notification\Configuration; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | use Parthenon\Notification\EmailInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | use Parthenon\Notification\EmailSenderInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | use Parthenon\Notification\Exception\UnableToSendMessageException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | use SendGrid\Mail\Attachment; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | use SendGrid\Mail\Mail; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  | final class SendGridEmailSender implements EmailSenderInterface | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |     use LoggerAwareTrait; | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |     private \SendGrid $sendGrid; | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |     private Configuration $configuration; | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |     public function __construct(\SendGrid $sendGrid, Configuration $configuration) | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |         $this->sendGrid = $sendGrid; | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |         $this->configuration = $configuration; | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     public function send(EmailInterface $message) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         $email = new Mail(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         $email->setFrom($message->getFromAddress() ?? $this->configuration->getFromAddress(), $message->getFromName() ?? $this->configuration->getFromName()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         $email->addTo($message->getToAddress(), $message->getToName()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         if ($message->isTemplate()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |             $email->setTemplateId($message->getTemplateName()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |             $email->addDynamicTemplateDatas($message->getTemplateVariables()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |             $email->setSubject($message->getSubject()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |             $email->addContent('text/html', $message->getContent()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         foreach ($message->getAttachments() as $attachment) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |             $sendgridAttachment = new Attachment(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |             $sendgridAttachment->setContent($attachment->getContent()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |             $sendgridAttachment->setFilename($attachment->getName()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |             $sendgridAttachment->setDisposition('attachment'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |             $email->addAttachment($sendgridAttachment); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |             $response = $this->sendGrid->send($email); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |             if (202 !== $response->statusCode()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |                 throw new \Exception($response->body()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |             $this->getLogger()->info('Sent email via sendgrid', ['to_address' => $message->getToAddress(), 'body' => $response->body(), 'status_code' => $response->statusCode()]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         } catch (\Throwable $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |             $this->getLogger()->warning('Unable to send email via sendgrid', ['exception' => $e]); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 76 |  |  |             throw new UnableToSendMessageException($e->getMessage(), (int) $e->getCode(), $e); | 
            
                                                                        
                                                                
            
                                    
            
            
                | 77 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 79 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 80 |  |  |  |