/var/www/html_fr/wp-content/plugins/fluent-smtp/app/Services/Mailer/Providers/TransMail/Handler.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
<?php

namespace FluentMail\App\Services\Mailer\Providers\TransMail;

use 
FluentMail\Includes\Support\Arr;
use 
FluentMail\App\Services\Mailer\BaseHandler;

class 
Handler extends BaseHandler
{
    use 
ValidatorTrait;

    protected 
$emailSentCode 200;

    protected 
$url null;

    public function 
send()
    {
        if (
$this->preSend()) {
            
$this->setUrl();
            return 
$this->postSend();
        }

        return 
$this->handleResponse(new \WP_Error(422__('Something went wrong!''fluent-smtp'), []));
    }

    protected function 
setUrl()
    {
        return 
$this->url 'https://transmail.zoho.' $this->getSetting('domain_name') . '/v1.1/email';
    }

    public function 
postSend()
    {
        
$body = [
            
'bounce_address' => $this->getSetting('bounce_address'),
            
'from'           => json_encode([
                
'name'    => $this->phpMailer->FromName,
                
'address' => $this->phpMailer->From,
            ]),
            
'to'             => json_encode([
                [
                    
'email_address' => [
                        
'address' => '',
                        
'name'    => ''
                    
]
                ]
            ]),
            
'cc'             => json_encode([
                [
                    
'email_address' => [
                        
'address' => '',
                        
'name'    => ''
                    
]
                ]
            ]),
            
'bcc'            => json_encode([
                [
                    
'email_address' => [
                        
'address' => '',
                        
'name'    => ''
                    
]
                ]
            ]),
            
'reply_to'       => json_encode([
                
'address' => '',
                
'name'    => ''
            
]),
            
'subject'        => $this->getSubject()
        ];

        if (
$this->phpMailer->ContentType == 'text/html') {
            
$body['htmlbody'] = $this->getBody();
        } else if (
$this->phpMailer->ContentType == 'multipart/alternative') {
            
$body['htmlbody'] = $this->getBody();
            
$body['textbody'] = $this->getParam('alt_body');
        } else {
            
$body['textbody'] = $this->getBody();
        }

        
$headers1 = array(
            
'Authorization' => 'Zoho-enczapikey ' $this->getSetting('token'),
            
'Content-Type'  => 'application/json',
            
'Accept'        => 'application/json'
        
);

        
$data_string json_encode($body);

        
$args = array(
            
'body'    => $data_string,
            
'headers' => $headers1,
            
'method'  => 'POST'
        
);

        
$response wp_safe_remote_post($this->url$args);

        if (
is_wp_error($response)) {
            
$returnResponse = new \WP_Error($response->get_error_code(), $response->get_error_message(), $response->get_error_messages());
        } else {
            
$responseBody wp_remote_retrieve_body($response);
            
$responseCode wp_remote_retrieve_response_code($response);

            
$isOKCode $responseCode == $this->emailSentCode;

            if (
$isOKCode) {
                
$responseBody \json_decode($responseBodytrue);
            }

            if (
$isOKCode && isset($responseBody['id'])) {
                
$returnResponse = [
                    
'id'      => Arr::get($responseBody'id'),
                    
'message' => Arr::get($responseBody'message')
                ];
            } else {
                
$returnResponse = new \WP_Error($responseCode__('Mailgun API Error''fluent-smtp'), $responseBody);
            }
        }

        
$this->response $returnResponse;

        return 
$this->handleResponse($this->response);
    }

    public function 
setSettings($settings)
    {
        if (
$settings['key_store'] == 'wp_config') {
            
$settings['api_key'] = defined('FLUENTMAIL_MAILGUN_API_KEY') ? FLUENTMAIL_MAILGUN_API_KEY '';
            
$settings['domain_name'] = defined('FLUENTMAIL_MAILGUN_DOMAIN') ? FLUENTMAIL_MAILGUN_DOMAIN '';
        }
        
$this->settings $settings;

        return 
$this;
    }

    protected function 
getFrom()
    {
        return 
$this->getParam('from');
    }

    protected function 
getReplyTo()
    {
        return 
$this->getRecipients(
            
$this->getParam('headers.reply-to')
        );
    }

    protected function 
getTo()
    {
        return 
$this->getRecipients($this->getParam('to'));
    }

    protected function 
getCarbonCopy()
    {
        return 
$this->getRecipients($this->getParam('headers.cc'));
    }

    protected function 
getBlindCarbonCopy()
    {
        return 
$this->getRecipients($this->getParam('headers.bcc'));
    }

    protected function 
getRecipients($recipients)
    {
        
$array array_map(function ($recipient) {
            return isset(
$recipient['name'])
                ? 
$recipient['name'] . ' <' $recipient['email'] . '>'
                
$recipient['email'];
        }, 
$recipients);

        return 
implode(', '$array);
    }

    protected function 
getBody()
    {
        return 
$this->getParam('message');
    }

    protected function 
getAttachments($params)
    {
        
$data = [];
        
$payload '';
        
$attachments $this->attributes['attachments'];

        foreach (
$attachments as $attachment) {
            
$file false;

            try {
                if (
is_file($attachment[0]) && is_readable($attachment[0])) {
                    
$fileName basename($attachment[0]);
                    
$file file_get_contents($attachment[0]);
                }
            } catch (
\Exception $e) {
                
$file false;
            }

            if (
$file === false) {
                continue;
            }

            
$data[] = [
                
'content' => $file,
                
'name'    => $fileName,
            ];
        }

        if (!empty(
$data)) {
            
$boundary hash('sha256'uniqid(''true));

            foreach (
$params['body'] as $key => $value) {
                if (
is_array($value)) {
                    foreach (
$value as $child_key => $child_value) {
                        
$payload .= '--' $boundary;
                        
$payload .= "\r\n";
                        
$payload .= 'Content-Disposition: form-data; name="' $key "\"\r\n\r\n";
                        
$payload .= $child_value;
                        
$payload .= "\r\n";
                    }
                } else {
                    
$payload .= '--' $boundary;
                    
$payload .= "\r\n";
                    
$payload .= 'Content-Disposition: form-data; name="' $key '"' "\r\n\r\n";
                    
$payload .= $value;
                    
$payload .= "\r\n";
                }
            }

            foreach (
$data as $key => $attachment) {
                
$payload .= '--' $boundary;
                
$payload .= "\r\n";
                
$payload .= 'Content-Disposition: form-data; name="attachment[' $key ']"; filename="' $attachment['name'] . '"' "\r\n\r\n";
                
$payload .= $attachment['content'];
                
$payload .= "\r\n";
            }

            
$payload .= '--' $boundary '--';

            
$params['body'] = $payload;

            
$params['headers']['Content-Type'] = 'multipart/form-data; boundary=' $boundary;

            
$this->attributes['headers']['content-type'] = 'multipart/form-data';
        }

        return 
$params;
    }

    protected function 
getRequestHeaders()
    {
        
$apiKey $this->getSetting('api_key');

        return [
            
'Authorization' => 'Basic ' base64_encode('api:' $apiKey)
        ];
    }
}