6
6
use SendGrid \Mail \From ;
7
7
use SendGrid \Mail \Mail ;
8
8
use SendGrid \Mail \ReplyTo ;
9
+ use SendGrid \Mail \Attachment ;
9
10
10
11
class SendGridMessage
11
12
{
@@ -42,6 +43,13 @@ class SendGridMessage
42
43
*/
43
44
public $ payload = [];
44
45
46
+ /**
47
+ * An array of attachments for the message.
48
+ *
49
+ * @var array
50
+ */
51
+ public $ attachments = [];
52
+
45
53
/**
46
54
* The sandbox mode for SendGrid
47
55
*
@@ -103,6 +111,26 @@ public function payload($payload)
103
111
return $ this ;
104
112
}
105
113
114
+ public function attach ($ attachments )
115
+ {
116
+ /*
117
+ $attachments should be an array of individual attachments. content should be base64 encoded.
118
+
119
+ Example:
120
+ $attachments = array(
121
+ array(
122
+ 'content' => base64_encode($content),
123
+ 'type' => 'application/pdf',
124
+ 'filename' => 'filename.pdf'
125
+ )
126
+ );
127
+ */
128
+
129
+ $ this ->attachments = $ attachments ;
130
+
131
+ return $ this ;
132
+ }
133
+
106
134
/**
107
135
* @return Mail
108
136
*/
@@ -124,6 +152,24 @@ public function build(): Mail
124
152
$ email ->addDynamicTemplateData ((string ) $ key , $ value );
125
153
}
126
154
155
+ if (is_array ($ this ->attachments ) && !empty ($ this ->attachments )) {
156
+ foreach ($ this ->attachments as $ attachment ) {
157
+ $ disposition = (isset ($ attachment ['disposition ' ])) ? strtolower ($ attachment ['disposition ' ]) : "attachment " ;
158
+
159
+ $ sgAttachment = new Attachment ();
160
+ $ sgAttachment ->setType ($ attachment ['type ' ]);
161
+ $ sgAttachment ->setContent ($ attachment ['content ' ]);
162
+ $ sgAttachment ->setDisposition ($ disposition );
163
+ $ sgAttachment ->setFilename ($ attachment ['filename ' ]);
164
+
165
+ if ($ disposition === "inline " ) {
166
+ $ sgAttachment ->setContentID ($ attachment ['filename ' ]);
167
+ }
168
+
169
+ $ email ->addAttachment ($ sgAttachment );
170
+ }
171
+ }
172
+
127
173
return $ email ;
128
174
}
129
175
0 commit comments