2
2
3
3
namespace Mondago \MSGraph \Mail ;
4
4
5
- use GuzzleHttp \ClientInterface ;
6
- use Illuminate \Mail \Transport \Transport as LaravelTransport ;
7
- use Illuminate \Support \Str ;
8
-
9
- class Transport extends LaravelTransport
5
+ use Illuminate \Support \Facades \Http ;
6
+ use Symfony \Component \Mailer \SentMessage ;
7
+ use Symfony \Component \Mailer \Transport \AbstractTransport ;
8
+ use Symfony \Component \Mime \Email ;
9
+ use Symfony \Component \Mime \MessageConverter ;
10
+ use Symfony \Component \Mime \Part \DataPart ;
11
+
12
+ class Transport extends AbstractTransport
10
13
{
11
14
/**
12
15
* Graph api configuration
13
16
* @var array
14
17
*/
15
- private $ config ;
16
-
17
- private $ http ;
18
+ private array $ config ;
18
19
19
- public function __construct (ClientInterface $ client , array $ config )
20
+ public function __construct (array $ config )
20
21
{
22
+ parent ::__construct (null , null );
21
23
$ this ->config = $ config ;
22
- $ this ->http = $ client ;
23
24
}
24
25
25
- public function send (\Swift_Mime_SimpleMessage $ message , &$ failedRecipients = null )
26
+
27
+ protected function doSend (SentMessage $ message ): void
26
28
{
27
- $ this ->beforeSendPerformed ($ message );
28
29
$ token = $ this ->getToken ();
29
- $ emailMessage = $ this ->getMessage ($ message );
30
- $ url = sprintf ('https://graph.microsoft.com/v1.0/users/%s/sendMail ' , urlencode ($ emailMessage ['from ' ]['emailAddress ' ]['address ' ]));
31
-
32
- try {
33
- $ this ->http ->post ($ url , [
34
- 'headers ' => [
35
- 'Accept ' => 'application/json ' ,
36
- 'Authorization ' => sprintf ('Bearer %s ' , $ token ),
37
- ],
38
- 'json ' => [
39
- 'message ' => $ emailMessage ,
40
- ],
41
- ]);
42
-
43
- $ this ->sendPerformed ($ message );
44
-
45
- return $ this ->numberOfRecipients ($ message );
46
-
47
- } catch (\Exception $ e ) {
48
- throw $ e ;
49
- }
30
+ $ email = MessageConverter::toEmail ($ message ->getOriginalMessage ());
31
+ $ url = sprintf ('https://graph.microsoft.com/v1.0/users/%s/sendMail ' , $ email ->getFrom ()[0 ]->getEncodedAddress ());
32
+ $ response = Http::withHeaders ([
33
+ 'Authorization ' => sprintf ('Bearer %s ' , $ token )
34
+ ])->post ($ url , [
35
+ "message " => $ this ->getMessage ($ email )
36
+ ]);
37
+ $ response ->throw ();
50
38
}
51
39
52
40
public function getToken ()
53
41
{
54
42
$ url = sprintf ('https://login.microsoftonline.com/%s/oauth2/v2.0/token ' , $ this ->config ['tenant ' ]);
55
- try {
56
- $ response = $ this ->http ->request ('POST ' , $ url , [
57
- 'form_params ' => [
58
- 'client_id ' => $ this ->config ['client_id ' ],
59
- 'client_secret ' => $ this ->config ['client_secret ' ],
60
- 'scope ' => 'https://graph.microsoft.com/.default ' ,
61
- 'grant_type ' => 'client_credentials '
62
- ],
63
- ]);
64
- $ data = json_decode ($ response ->getBody ()->getContents ());
43
+ $ response = Http::asForm ()->post ($ url , [
44
+ 'client_id ' => $ this ->config ['client_id ' ],
45
+ 'client_secret ' => $ this ->config ['client_secret ' ],
46
+ 'scope ' => 'https://graph.microsoft.com/.default ' ,
47
+ 'grant_type ' => 'client_credentials '
48
+ ]);
49
+ $ response ->throw ();
65
50
66
- return $ data ->access_token ;
67
- } catch (\Exception $ e ) {
68
- throw $ e ;
69
- }
51
+ return $ response ['access_token ' ];
70
52
}
71
53
72
54
public function __toString (): string
@@ -75,84 +57,59 @@ public function __toString(): string
75
57
}
76
58
77
59
/*
78
- * @link https://docs.microsoft.com/en-us/graph/api/resources/message?view=graph-rest-1.0
60
+ * https://docs.microsoft.com/en-us/graph/api/resources/message?view=graph-rest-1.0
79
61
*/
80
- private function getMessage (\ Swift_Mime_SimpleMessage $ email )
62
+ private function getMessage (Email $ email )
81
63
{
82
64
return array_filter ([
83
- "from " => $ this ->getRecipientsCollection ($ email ->getFrom ()) [0 ],
84
- "sender " => $ this ->getRecipientsCollection ($ email ->getFrom ()) [0 ],
65
+ "from " => $ this ->getRecipient ($ email ->getFrom ()[0 ]) ,
66
+ "sender " => $ this ->getRecipient ($ email ->getFrom ()[0 ]) ,
85
67
"toRecipients " => $ this ->getRecipientsCollection ($ email ->getTo ()),
86
68
"ccRecipients " => $ this ->getRecipientsCollection ($ email ->getCc ()),
87
69
"bccRecipients " => $ this ->getRecipientsCollection ($ email ->getBcc ()),
88
70
"replyTo " => $ this ->getRecipientsCollection ($ email ->getReplyTo ()),
89
71
"subject " => $ email ->getSubject (),
90
72
"body " => [
91
- "contentType " => $ this -> getContentType ( $ email ) ,
92
- "content " => $ email ->getBody ()
73
+ "contentType " => $ email -> getTextBody () ? ' Text ' : ' HTML ' ,
74
+ "content " => $ email ->getTextBody () ?? $ email -> getHtmlBody (),
93
75
],
94
- "attachments " => $ this ->getAttachmentsCollection ($ email ->getChildren ())
76
+ "attachments " => $ this ->getAttachmentsCollection ($ email ->getAttachments ())
95
77
]);
96
78
}
97
79
98
- private function getRecipientsCollection ($ addresses ): array
80
+ private function getRecipientsCollection (array $ addresses ): array
99
81
{
100
- $ collection = [];
101
- if (!$ addresses ) {
102
- return [];
103
- }
104
- if (is_string ($ addresses )) {
105
- $ addresses = [
106
- $ addresses => null ,
107
- ];
108
- }
109
-
110
- foreach ($ addresses as $ email => $ name ) {
111
- # https://docs.microsoft.com/en-us/graph/api/resources/recipient?view=graph-rest-1.0
112
- $ collection [] = [
113
- 'emailAddress ' => [
114
- 'name ' => $ name ,
115
- 'address ' => $ email ,
116
- ],
117
- ];
118
- }
82
+ return array_map ('self::getRecipient ' , $ addresses );
83
+ }
119
84
120
- return $ collection ;
85
+ /*
86
+ * https://docs.microsoft.com/en-us/graph/api/resources/recipient?view=graph-rest-1.0
87
+ */
88
+ private function getRecipient ($ address ): array
89
+ {
90
+ return [
91
+ 'emailAddress ' => array_filter ([
92
+ 'address ' => $ address ->getAddress (),
93
+ 'name ' => $ address ->getName (),
94
+ ])
95
+ ];
121
96
}
122
97
123
98
private function getAttachmentsCollection ($ attachments )
124
99
{
125
- $ collection = [];
126
-
127
- foreach ($ attachments as $ attachment ) {
128
- if (!$ attachment instanceof \Swift_Mime_Attachment) {
129
- continue ;
130
- }
131
- // https://docs.microsoft.com/en-us/graph/api/resources/fileattachment?view=graph-rest-1.0
132
- $ collection [] = [
133
- 'name ' => $ attachment ->getFilename (),
134
- 'contentId ' => $ attachment ->getId (),
135
- 'contentType ' => $ attachment ->getContentType (),
136
- 'contentBytes ' => base64_encode ($ attachment ->getBody ()),
137
- 'size ' => strlen ($ attachment ->getBody ()),
138
- '@odata.type ' => '#microsoft.graph.fileAttachment ' ,
139
- 'isInline ' => $ attachment instanceof \Swift_Mime_EmbeddedFile,
140
- ];
141
-
142
- }
143
-
144
- return $ collection ;
100
+ return array_map ('self::getAttachment ' , $ attachments );
145
101
}
146
102
147
- public function getContentType (\Swift_Mime_SimpleMessage $ email ): string
103
+ /*
104
+ * https://docs.microsoft.com/en-us/graph/api/resources/fileattachment?view=graph-rest-1.0
105
+ */
106
+ private function getAttachment (DataPart $ attachment )
148
107
{
149
- if (Str::contains ($ email ->getBodyContentType (), ['html ' ])) {
150
- return 'HTML ' ;
151
- } else {
152
- if (Str::contains ($ email ->getBodyContentType (), ['text ' , 'plain ' ])) {
153
- return 'Text ' ;
154
- }
155
- }
156
- return 'HTML ' ;
108
+ return array_filter ([
109
+ "@odata.type " => "#microsoft.graph.fileAttachment " ,
110
+ "name " => $ attachment ->getName () ?? $ attachment ->getFilename (),
111
+ "contentType " => $ attachment ->getContentType (),
112
+ "contentBytes " => base64_encode ($ attachment ->getBody ()),
113
+ ]);
157
114
}
158
115
}
0 commit comments