88use Swift_Mime_Attachment ;
99use Swift_Mime_Message ;
1010use Swift_Transport ;
11+ use Psr \Log \LoggerInterface ;
1112
12- class SendGridTransport implements Swift_Transport {
13-
13+ class SendGridTransport implements Swift_Transport
14+ {
1415 /**
1516 * @see https://sendgrid.com/docs/API_Reference/Web_API_v3/Mail/errors.html
1617 * 2xx responses indicate a successful request. The request that you made is valid and successful.
@@ -36,60 +37,84 @@ class SendGridTransport implements Swift_Transport {
3637 */
3738 private $ sendGridApiKey ;
3839
39- public function __construct ($ sendGridApiKey ) {
40+ /**
41+ * Sendgrid mails categories.
42+ *
43+ * @var array
44+ */
45+ private $ sendGridCategories ;
46+
47+ /**
48+ * Logger.
49+ *
50+ * @var LoggerInterface
51+ */
52+ private $ logger ;
53+
54+ public function __construct ($ sendGridApiKey , $ sendGridCategories , LoggerInterface $ logger )
55+ {
4056 $ this ->sendGridApiKey = $ sendGridApiKey ;
57+ $ this ->sendGridCategories = $ sendGridCategories ;
58+ $ this ->logger = $ logger ;
4159 }
4260
43- public function isStarted () {
61+ public function isStarted ()
62+ {
4463 //Not used
4564 return true ;
4665 }
4766
48- public function start () {
67+ public function start ()
68+ {
4969 //Not used
5070 }
5171
52- public function stop () {
72+ public function stop ()
73+ {
5374 //Not used
5475 }
5576
5677 /**
57- * WARNING : $failedRecipients and return value are faked
78+ * WARNING : $failedRecipients and return value are faked.
5879 *
5980 * @param Swift_Mime_Message $message
60- * @param array $failedRecipients
81+ * @param array $failedRecipients
82+ *
6183 * @return int
6284 */
63- public function send (Swift_Mime_Message $ message , &$ failedRecipients = null ) {
64-
85+ public function send (Swift_Mime_Message $ message , &$ failedRecipients = null )
86+ {
6587 // prepare fake data.
66- $ sent = 0 ;
88+ $ sent = 0 ;
6789 $ prepareFailedRecipients = [];
6890
69-
7091 //Get the first from email (SendGrid PHP library only seems to support one)
7192 $ fromArray = $ message ->getFrom ();
72- $ fromName = reset ($ fromArray );
93+ $ fromName = reset ($ fromArray );
7394 $ fromEmail = key ($ fromArray );
7495
7596 $ mail = new SendGrid \Mail (); //Intentionally not using constructor arguments as they are tedious to work with
7697
98+ // categories can be useful if you use them like tags to, for example, distinguish different applications.
99+ foreach ($ this ->sendGridCategories as $ category ) {
100+ $ mail ->addCategory ($ category );
101+ }
102+
77103 $ mail ->setFrom (new SendGrid \Email ($ fromName , $ fromEmail ));
78104 $ mail ->setSubject ($ message ->getSubject ());
79105
80106 // extract content type from body to prevent multi-part content-type error
81107 $ finfo = new finfo (FILEINFO_MIME_TYPE );
82108 $ contentType = $ finfo ->buffer ($ message ->getBody ());
83- $ mail ->addContent (new SendGrid \Content ($ contentType ,
84- $ message ->getBody ()));
109+ $ mail ->addContent (new SendGrid \Content ($ contentType , $ message ->getBody ()));
85110
86111 $ personalization = new SendGrid \Personalization ();
87112
88113 // process TO
89114 if ($ toArr = $ message ->getTo ()) {
90115 foreach ($ toArr as $ email => $ name ) {
91116 $ personalization ->addTo (new SendGrid \Email ($ name , $ email ));
92- $ sent ++ ;
117+ ++ $ sent ;
93118 $ prepareFailedRecipients [] = $ email ;
94119 }
95120 }
@@ -98,7 +123,7 @@ public function send(Swift_Mime_Message $message, &$failedRecipients = null) {
98123 if ($ ccArr = $ message ->getCc ()) {
99124 foreach ($ ccArr as $ email => $ name ) {
100125 $ personalization ->addCc (new SendGrid \Email ($ name , $ email ));
101- $ sent ++ ;
126+ ++ $ sent ;
102127 $ prepareFailedRecipients [] = $ email ;
103128 }
104129 }
@@ -107,12 +132,12 @@ public function send(Swift_Mime_Message $message, &$failedRecipients = null) {
107132 if ($ bccArr = $ message ->getBcc ()) {
108133 foreach ($ bccArr as $ email => $ name ) {
109134 $ personalization ->addBcc (new SendGrid \Email ($ name , $ email ));
110- $ sent ++ ;
135+ ++ $ sent ;
111136 $ prepareFailedRecipients [] = $ email ;
112137 }
113138 }
114139
115- // process attachment (not inline)
140+ // process attachment
116141 if ($ attachments = $ message ->getChildren ()) {
117142 foreach ($ attachments as $ attachment ) {
118143 if ($ attachment instanceof Swift_Mime_Attachment) {
@@ -123,6 +148,9 @@ public function send(Swift_Mime_Message $message, &$failedRecipients = null) {
123148 $ sAttachment ->setDisposition ($ attachment ->getDisposition ());
124149 $ sAttachment ->setContentId ($ attachment ->getId ());
125150 $ mail ->addAttachment ($ sAttachment );
151+ } elseif (in_array ($ attachment ->getContentType (), ['text/plain ' , 'text/html ' ])) {
152+ // add part if any is defined, to avoid error please set body as text and part as html
153+ $ mail ->addContent (new SendGrid \Content ($ attachment ->getContentType (), $ attachment ->getBody ()));
126154 }
127155 }
128156 }
@@ -134,12 +162,11 @@ public function send(Swift_Mime_Message $message, &$failedRecipients = null) {
134162 $ response = $ sendGrid ->client ->mail ()->send ()->post ($ mail );
135163
136164 // only 2xx status are ok
137- if (
138- $ response ->statusCode () < self ::STATUS_OK_SUCCESSFUL_MIN_RANGE ||
139- self ::STATUS_SUCCESSFUL_MAX_RANGE < $ response ->statusCode ()) {
140-
165+ if ($ response ->statusCode () < self ::STATUS_OK_SUCCESSFUL_MIN_RANGE ||
166+ self ::STATUS_SUCCESSFUL_MAX_RANGE < $ response ->statusCode ()) {
141167 // to force big boom error uncomment this line
142168 //throw new \Swift_TransportException("Error when sending message. Return status :".$response->statusCode());
169+ $ this ->logger ->error ($ response ->statusCode ().': ' .$ response ->body ());
143170
144171 // copy failed recipients
145172 foreach ($ prepareFailedRecipients as $ recipient ) {
@@ -151,8 +178,8 @@ public function send(Swift_Mime_Message $message, &$failedRecipients = null) {
151178 return $ sent ;
152179 }
153180
154- public function registerPlugin (Swift_Events_EventListener $ plugin ) {
181+ public function registerPlugin (Swift_Events_EventListener $ plugin )
182+ {
155183 // unused
156184 }
157-
158185}
0 commit comments