3
3
4
4
namespace FlexFlux \LaravelElasticEmail ;
5
5
6
- use GuzzleHttp \ClientInterface ;
7
- use Illuminate \Mail \Transport \Transport ;
6
+ use Symfony \Component \Mime \Email ;
8
7
use Illuminate \Support \Facades \Storage ;
9
- use Swift_Mime_SimpleMessage ;
8
+ use Symfony \Component \Mailer \SentMessage ;
9
+ use Symfony \Component \Mime \Part \DataPart ;
10
+ use Symfony \Component \Mime \MessageConverter ;
11
+ use Symfony \Component \Mailer \Transport \AbstractTransport ;
10
12
11
- class ElasticTransport extends Transport
13
+ class ElasticTransport extends AbstractTransport
12
14
{
13
- protected $ client ;
14
15
protected $ key ;
15
- protected $ account ;
16
16
protected $ url = "https://api.elasticemail.com/v2/email/send " ;
17
17
18
- public function __construct (ClientInterface $ client , $ key, $ account )
18
+ public function __construct ($ key )
19
19
{
20
- $ this ->client = $ client ;
21
20
$ this ->key = $ key ;
22
- $ this ->account = $ account ;
23
21
}
24
22
25
- public function send ( Swift_Mime_SimpleMessage $ message , & $ failedRecipients = null )
23
+ public function __toString (): string
26
24
{
27
- $ this ->beforeSendPerformed ($ message );
25
+ return 'elasticemail ' ;
26
+ }
27
+
28
+ /**
29
+ * {@inheritdoc}
30
+ */
31
+ public function doSend (SentMessage $ message ) : void
32
+ {
33
+ $ email = MessageConverter::toEmail ($ message ->getOriginalMessage ());
28
34
29
35
$ data = [
30
36
'apikey ' => $ this ->key ,
31
- 'account ' => $ this ->account ,
32
- 'msgTo ' => $ this ->getEmailAddresses ($ message ),
33
- 'msgCC ' => $ this ->getEmailAddresses ($ message , 'getCc ' ),
34
- 'msgBcc ' => $ this -> getEmailAddresses ( $ message , ' getBcc ' ),
35
- 'msgFrom ' => $ this -> getFromAddress ( $ message )[ ' email ' ] ,
36
- 'msgFromName ' => $ this -> getFromAddress ( $ message )[ ' name ' ] ,
37
- 'from ' => $ this -> getFromAddress ( $ message )[ ' email ' ] ,
38
- 'fromName ' => $ this ->getFromAddress ( $ message )[ ' name ' ] ,
39
- 'to ' => $ this ->getEmailAddresses ($ message ),
40
- 'subject ' => $ message ->getSubject (),
41
- 'bodyHtml ' => $ message -> getBody (),
42
- 'bodyText ' => $ this -> getText ( $ message ),
43
- 'isTransactional ' => true ,
37
+ 'msgTo ' => $ this ->getEmailAddresses ( $ email ) ,
38
+ 'msgCC ' => $ this ->getEmailAddresses ($ email , ' getCc ' ),
39
+ 'msgBcc ' => $ this ->getEmailAddresses ($ email , 'getBcc ' ),
40
+ 'msgFrom ' => $ email -> getFrom ()[ 0 ]-> getAddress ( ),
41
+ 'msgFromName ' => $ email -> getFrom ()[ 0 ]-> getName () ,
42
+ 'from ' => $ email -> getFrom ()[ 0 ]-> getAddress () ,
43
+ 'fromName ' => $ email -> getFrom ()[ 0 ]-> getName () ,
44
+ 'replyTo ' => $ this ->getEmailAddresses ( $ email , ' getReplyTo ' ) ,
45
+ 'to ' => $ this ->getEmailAddresses ($ email ),
46
+ 'subject ' => $ email ->getSubject (),
47
+ 'bodyHtml ' => $ email -> getHtmlBody (),
48
+ 'bodyText ' => $ email -> getTextBody ( ),
49
+ 'isTransactional ' => $ email -> getHeaders ()-> getHeaderBody ( ' x-metadata-transactional ' ) ? true : false ,
44
50
];
45
51
46
- $ attachments = $ message -> getChildren ();
47
- $ attachmentCount = $ this -> checkAttachmentCount ($ attachments );
52
+ $ attachments = $ email -> getAttachments ();
53
+ $ attachmentCount = count ($ attachments );
48
54
49
55
if ($ attachmentCount > 0 ) {
50
56
$ data = $ this ->attach ($ attachments , $ data );
51
57
}
58
+
52
59
$ ch = curl_init ();
53
60
54
- curl_setopt_array ($ ch , array (
61
+ curl_setopt_array ($ ch , [
55
62
CURLOPT_URL => $ this ->url ,
56
63
CURLOPT_POST => true ,
57
64
CURLOPT_POSTFIELDS => $ data ,
58
65
CURLOPT_RETURNTRANSFER => true ,
59
66
CURLOPT_HEADER => false ,
60
- CURLOPT_SSL_VERIFYPEER => false
61
- ) );
67
+ CURLOPT_SSL_VERIFYPEER => false ,
68
+ ] );
62
69
63
- $ result = curl_exec ($ ch );
70
+ curl_exec ($ ch );
64
71
curl_close ($ ch );
65
72
66
73
if ($ attachmentCount > 0 ) {
67
74
$ this ->deleteTempAttachmentFiles ($ data , $ attachmentCount );
68
75
}
69
-
70
- return $ result ;
71
- }
72
-
73
- protected function getEmailAddresses (Swift_Mime_SimpleMessage $ message , $ method = 'getTo ' )
74
- {
75
- $ data = call_user_func ([$ message , $ method ]);
76
-
77
- if (is_array ($ data )) {
78
- return implode (', ' , array_keys ($ data ));
79
- }
80
- return '' ;
81
- }
82
-
83
- protected function getFromAddress (Swift_Mime_SimpleMessage $ message )
84
- {
85
- return [
86
- 'email ' => array_keys ($ message ->getFrom ())[0 ],
87
- 'name ' => array_values ($ message ->getFrom ())[0 ],
88
- ];
89
- }
90
-
91
- protected function getText (Swift_Mime_SimpleMessage $ message )
92
- {
93
- $ text = null ;
94
-
95
- foreach ($ message ->getChildren () as $ child ) {
96
- if ($ child ->getContentType () == 'text/plain ' ) {
97
- $ text = $ child ->getBody ();
98
- }
99
- }
100
-
101
- return $ text ;
102
76
}
103
77
104
- public function checkAttachmentCount ($ attachments )
105
- {
106
- $ count = 0 ;
107
- foreach ($ attachments AS $ attachment ) {
108
- if ($ attachment instanceof \Swift_Attachment) {
109
- $ count ++;
110
- }
111
- }
112
- return $ count ;
113
- }
78
+ /**
79
+ * Add attachments to post data array.
80
+ * @param $attachments
81
+ * @param $data
82
+ * @return mixed
83
+ */
114
84
115
85
public function attach ($ attachments , $ data )
116
86
{
117
87
if (is_array ($ attachments ) && count ($ attachments ) > 0 ) {
118
88
$ i = 1 ;
119
- foreach ($ attachments AS $ attachment ) {
120
- if ($ attachment instanceof \Swift_Attachment ) {
89
+ foreach ($ attachments as $ attachment ) {
90
+ if ($ attachment instanceof DataPart ) {
121
91
$ attachedFile = $ attachment ->getBody ();
122
- $ fileName = $ attachment ->getFilename ( );
92
+ $ fileName = $ attachment ->getPreparedHeaders ()-> getHeaderParameter ( ' Content-Disposition ' , ' filename ' );
123
93
$ ext = pathinfo ($ fileName , PATHINFO_EXTENSION );
124
- $ tempName = uniqid () . '. ' . $ ext ;
94
+ $ tempName = uniqid (). '. ' . $ ext ;
125
95
Storage::put ($ tempName , $ attachedFile );
126
- $ type = $ attachment ->getContentType ();
96
+ $ type = $ attachment ->getMediaType (). ' / ' . $ attachment -> getMediaSubtype ();
127
97
$ attachedFilePath = storage_path ($ tempName );
128
- $ data ['file_ ' . $ i ] = new \CURLFile ($ attachedFilePath , $ type , $ fileName );
98
+ $ data ['file_ ' . $ i ] = new \CurlFile ($ attachedFilePath , $ type , $ fileName );
129
99
$ i ++;
130
100
}
131
101
}
@@ -134,10 +104,37 @@ public function attach($attachments, $data)
134
104
return $ data ;
135
105
}
136
106
137
- protected function deleteTempAttachmentFiles ($ data , $ count )
107
+ /**
108
+ * Retrieve requested emailaddresses from email.
109
+ * @param Email $email
110
+ * @param string $method
111
+ * @return string
112
+ */
113
+
114
+ protected function getEmailAddresses (Email $ email , $ method = 'getTo ' )
115
+ {
116
+ $ data = call_user_func ([$ email , $ method ]);
117
+
118
+ $ addresses = [];
119
+ if (is_array ($ data )) {
120
+ foreach ($ data as $ address ) {
121
+ $ addresses [] = $ address ->getAddress ();
122
+ }
123
+ }
124
+
125
+ return implode (', ' , $ addresses );
126
+ }
127
+
128
+ /**
129
+ * Delete temporary attachment files.
130
+ * @param $data
131
+ * @param $count
132
+ */
133
+
134
+ protected function deleteTempAttachmentFiles ($ data , $ count ) : void
138
135
{
139
136
for ($ i = 1 ; $ i <= $ count ; $ i ++) {
140
- $ file = $ data ['file_ ' . $ i ]->name ;
137
+ $ file = $ data ['file_ ' . $ i ]->name ;
141
138
if (file_exists ($ file )) {
142
139
unlink ($ file );
143
140
}
0 commit comments