FluentSMTP – WP Mail SMTP, Amazon SES, SendGrid, MailGun and Any SMTP Connector Plugin - Version 1.0.1

Version Description

(Date: January 24, 2021) = * Fix UTF-8 issues * SendInBlue wp-config constant issue fixed * Fallback from name issue fixed * Search for Email Logs has been fixed

Download this release

Release Info

Developer techjewel
Plugin Icon FluentSMTP – WP Mail SMTP, Amazon SES, SendGrid, MailGun and Any SMTP Connector Plugin
Version 1.0.1
Comparing to
See all releases

Version 1.0.1

Files changed (56) hide show
  1. app/App.php +24 -0
  2. app/Bindings.php +20 -0
  3. app/Functions/helpers.php +188 -0
  4. app/Functions/pluggable.php +443 -0
  5. app/Hooks/Handlers/AdminMenuHandler.php +135 -0
  6. app/Hooks/Handlers/ExceptionHandler.php +35 -0
  7. app/Hooks/Handlers/ProviderValidator.php +35 -0
  8. app/Hooks/actions.php +21 -0
  9. app/Hooks/filters.php +14 -0
  10. app/Http/Controllers/Controller.php +58 -0
  11. app/Http/Controllers/DashboardController.php +39 -0
  12. app/Http/Controllers/LoggerController.php +82 -0
  13. app/Http/Controllers/SettingsController.php +360 -0
  14. app/Http/routes.php +21 -0
  15. app/Models/Logger.php +430 -0
  16. app/Models/Model.php +25 -0
  17. app/Models/Settings.php +207 -0
  18. app/Models/Traits/SendTestEmailTrait.php +37 -0
  19. app/Services/Mailer/BaseHandler.php +355 -0
  20. app/Services/Mailer/FluentPHPMailer.php +56 -0
  21. app/Services/Mailer/Manager.php +118 -0
  22. app/Services/Mailer/Providers/AmazonSes/Handler.php +314 -0
  23. app/Services/Mailer/Providers/AmazonSes/SimpleEmailService.php +635 -0
  24. app/Services/Mailer/Providers/AmazonSes/SimpleEmailServiceMessage.php +638 -0
  25. app/Services/Mailer/Providers/AmazonSes/SimpleEmailServiceRequest.php +376 -0
  26. app/Services/Mailer/Providers/AmazonSes/Validator.php +73 -0
  27. app/Services/Mailer/Providers/AmazonSes/ValidatorTrait.php +66 -0
  28. app/Services/Mailer/Providers/DefaultMail/Handler.php +51 -0
  29. app/Services/Mailer/Providers/Factory.php +53 -0
  30. app/Services/Mailer/Providers/Mailgun/Handler.php +264 -0
  31. app/Services/Mailer/Providers/Mailgun/ValidatorTrait.php +40 -0
  32. app/Services/Mailer/Providers/PepiPost/Handler.php +262 -0
  33. app/Services/Mailer/Providers/PepiPost/ValidatorTrait.php +32 -0
  34. app/Services/Mailer/Providers/SendGrid/Handler.php +265 -0
  35. app/Services/Mailer/Providers/SendGrid/ValidatorTrait.php +37 -0
  36. app/Services/Mailer/Providers/SendInBlue/Handler.php +227 -0
  37. app/Services/Mailer/Providers/SendInBlue/ValidatorTrait.php +31 -0
  38. app/Services/Mailer/Providers/Smtp/Handler.php +145 -0
  39. app/Services/Mailer/Providers/Smtp/ValidatorTrait.php +38 -0
  40. app/Services/Mailer/Providers/SparkPost/Handler.php +250 -0
  41. app/Services/Mailer/Providers/SparkPost/ValidatorTrait.php +32 -0
  42. app/Services/Mailer/Providers/config.php +168 -0
  43. app/Services/Mailer/ValidatorTrait.php +39 -0
  44. app/Services/Reporting.php +156 -0
  45. app/index.php +1 -0
  46. app/views/admin/email_html.php +46 -0
  47. app/views/admin/email_text.php +5 -0
  48. app/views/admin/general_connection_info.php +20 -0
  49. app/views/admin/menu.php +1 -0
  50. app/views/admin/ses_connection_info.php +43 -0
  51. assets/admin/css/fluent-mail-admin.css +1 -0
  52. assets/admin/css/fonts/element-icons.ttf +0 -0
  53. assets/admin/css/fonts/element-icons.woff +0 -0
  54. assets/admin/js/boot.js +1 -0
  55. assets/admin/js/fluent-mail-admin-app.js +1 -0
  56. assets/admin/js/vendor.js +2 -0
app/App.php ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ namespace FluentMail\App;
4
+
5
+ use FluentMail\Includes\Core\Application;
6
+
7
+ class App
8
+ {
9
+ public static function getInstance($module = null)
10
+ {
11
+ $app = Application::getInstance();
12
+
13
+ if ($module) {
14
+ return $app[$module];
15
+ }
16
+
17
+ return $app;
18
+ }
19
+
20
+ public static function __callStatic($method, $params)
21
+ {
22
+ return static::getInstance($method);
23
+ }
24
+ }
app/Bindings.php ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ $singletons = [
4
+ 'manager' => 'FluentMail\App\Services\Mailer\Manager',
5
+ 'smtp' => 'FluentMail\App\Services\Mailer\Providers\Smtp\Handler',
6
+ 'ses' => 'FluentMail\App\Services\Mailer\Providers\AmazonSes\Handler',
7
+ 'mailgun' => 'FluentMail\App\Services\Mailer\Providers\Mailgun\Handler',
8
+ 'sendgrid' => 'FluentMail\App\Services\Mailer\Providers\SendGrid\Handler',
9
+ 'pepipost' => 'FluentMail\App\Services\Mailer\Providers\PepiPost\Handler',
10
+ 'sparkpost' => 'FluentMail\App\Services\Mailer\Providers\SparkPost\Handler',
11
+ 'default' => 'FluentMail\App\Services\Mailer\Providers\DefaultMail\Handler',
12
+ 'sendinblue' => 'FluentMail\App\Services\Mailer\Providers\SendInBlue\Handler',
13
+ ];
14
+
15
+ foreach ($singletons as $key => $className) {
16
+ $app->alias($className, $key);
17
+ $app->singleton($className, function($app) use ($className) {
18
+ return new $className();
19
+ });
20
+ }
app/Functions/helpers.php ADDED
@@ -0,0 +1,188 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ use FluentMail\App\Services\Mailer\Manager;
4
+ use FluentMail\App\Services\Mailer\Providers\Factory;
5
+ use FluentMail\App\Services\Mailer\Providers\AmazonSes\SimpleEmailService;
6
+
7
+ if (!function_exists('fluentMail')) {
8
+ function fluentMail($module = null)
9
+ {
10
+ return FluentMail\App\App::getInstance($module);
11
+ }
12
+ }
13
+
14
+ if (!function_exists('fluentMailMix')) {
15
+ function fluentMailMix($path, $manifestDirectory = '')
16
+ {
17
+ return fluentMail('url.assets') . ltrim($path, '/');
18
+ }
19
+ }
20
+
21
+ if (!function_exists('fluentMailAssetUrl')) {
22
+ function fluentMailAssetUrl($path = null)
23
+ {
24
+ $assetUrl = fluentMail('url.assets');
25
+ return $path ? ($assetUrl . $path) : $assetUrl;
26
+ }
27
+ }
28
+
29
+ if (!function_exists('dd')) {
30
+ function dd()
31
+ {
32
+ foreach (func_get_args() as $arg) {
33
+ echo "<pre>";
34
+ print_r($arg);
35
+ echo "</pre>";
36
+ }
37
+ die;
38
+ }
39
+ }
40
+
41
+ if (!function_exists('ddd')) {
42
+ function ddd($data)
43
+ {
44
+ foreach (func_get_args() as $arg) {
45
+ echo "<pre>";
46
+ print_r($arg);
47
+ echo "</pre>";
48
+ }
49
+ }
50
+ }
51
+
52
+ if (!function_exists('fluentMailWpParseArgs')) {
53
+ function fluentMailWpParseArgs($args, $defaults = [])
54
+ {
55
+ $newArgs = (array)$defaults;
56
+
57
+ foreach ($args as $key => $value) {
58
+ if (is_array($value) && isset($newArgs[$key])) {
59
+ $newArgs[$key] = fluentMailWpParseArgs($value, $newArgs[$key]);
60
+ } else {
61
+ $newArgs[$key] = $value;
62
+ }
63
+ }
64
+
65
+ return $newArgs;
66
+ }
67
+ }
68
+
69
+ if (!function_exists('fluentMailIsListedSenderEmail')) {
70
+ function fluentMailIsListedSenderEmail($email)
71
+ {
72
+ static $settings;
73
+
74
+ if (!$settings) {
75
+ $settings = get_option('fluentmail-settings');
76
+ }
77
+
78
+ if (!$settings) {
79
+ return false;
80
+ }
81
+ return !empty($settings['mappings'][$email]);
82
+ }
83
+ }
84
+
85
+ if (!function_exists('fluentMailDefaultConnection')) {
86
+ function fluentMailDefaultConnection()
87
+ {
88
+ static $defaultConnection;
89
+
90
+ if ($defaultConnection) {
91
+ return $defaultConnection;
92
+ }
93
+
94
+ $settings = get_option('fluentmail-settings');
95
+
96
+ if (!$settings) {
97
+ return [];
98
+ }
99
+
100
+ if (
101
+ isset($settings['misc']['default_connection']) &&
102
+ isset($settings['connections'][$settings['misc']['default_connection']])
103
+ ) {
104
+ $default = $settings['misc']['default_connection'];
105
+ $defaultConnection = $settings['connections'][$default]['provider_settings'];
106
+ } else if (count($settings['connections'])) {
107
+ $connection = reset($settings['connections']);
108
+ $defaultConnection = $connection['provider_settings'];
109
+ } else {
110
+ $defaultConnection = [];
111
+ }
112
+
113
+ return $defaultConnection;
114
+
115
+ }
116
+ }
117
+
118
+ if (!function_exists('fluentMailgetConnection')) {
119
+ function fluentMailgetConnection($email)
120
+ {
121
+ $factory = fluentMail(Factory::class);
122
+ if (!($connection = $factory->get($email))) {
123
+ $connection = fluentMailDefaultConnection();
124
+ }
125
+
126
+ return $connection;
127
+ }
128
+ }
129
+
130
+ if (!function_exists('fluentMailGetProvider')) {
131
+ function fluentMailGetProvider($fromEmail)
132
+ {
133
+ static $providers = [];
134
+
135
+ if (isset($providers[$fromEmail])) {
136
+ return $providers[$fromEmail];
137
+ }
138
+
139
+ $manager = fluentMail(Manager::class);
140
+
141
+ $mappings = $manager->getSettings('mappings');
142
+
143
+ if (isset($mappings[$fromEmail])) {
144
+ $connectionId = $mappings[$fromEmail];
145
+ $connections = $manager->getSettings('connections');
146
+ $connection = $connections[$connectionId]['provider_settings'];
147
+ } else {
148
+ if ($connection = fluentMailDefaultConnection()) {
149
+ $connection['force_from_email_id'] = $connection['sender_email'];
150
+ }
151
+ }
152
+
153
+ if ($connection) {
154
+ $factory = fluentMail(Factory::class);
155
+ $driver = $factory->make($connection['provider']);
156
+ $driver->setSettings($connection);
157
+ $providers[$fromEmail] = $driver;
158
+ } else {
159
+ $providers[$fromEmail] = false;
160
+ }
161
+
162
+ return $providers[$fromEmail];
163
+ }
164
+ }
165
+
166
+ if (!function_exists('fluentMailSesConnection')) {
167
+ function fluentMailSesConnection($connection)
168
+ {
169
+ static $drivers = [];
170
+
171
+ if (isset($drivers[$connection['sender_email']])) {
172
+ return $drivers[$connection['sender_email']];
173
+ }
174
+
175
+ $region = 'email.' . $connection['region'] . '.amazonaws.com';
176
+
177
+ $ses = new SimpleEmailService(
178
+ $connection['access_key'],
179
+ $connection['secret_key'],
180
+ $region,
181
+ false
182
+ );
183
+
184
+ $drivers[$connection['sender_email']] = $ses;
185
+
186
+ return $drivers[$connection['sender_email']];
187
+ }
188
+ }
app/Functions/pluggable.php ADDED
@@ -0,0 +1,443 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ if ( ! function_exists( 'wp_mail' ) ) :
4
+ /**
5
+ * Sends an email, similar to PHP's mail function.
6
+ *
7
+ * A true return value does not automatically mean that the user received the
8
+ * email successfully. It just only means that the method used was able to
9
+ * process the request without any errors.
10
+ *
11
+ * The default content type is `text/plain` which does not allow using HTML.
12
+ * However, you can set the content type of the email by using the
13
+ * {@see 'wp_mail_content_type'} filter.
14
+ *
15
+ * The default charset is based on the charset used on the blog. The charset can
16
+ * be set using the {@see 'wp_mail_charset'} filter.
17
+ *
18
+ * @since 1.2.1
19
+ *
20
+ * @global PHPMailer\PHPMailer\PHPMailer $phpmailer
21
+ *
22
+ * @param string|array $to Array or comma-separated list of email addresses to send message.
23
+ * @param string $subject Email subject
24
+ * @param string $message Message contents
25
+ * @param string|array $headers Optional. Additional headers.
26
+ * @param string|array $attachments Optional. Files to attach.
27
+ * @return bool Whether the email contents were sent successfully.
28
+ */
29
+ function wp_mail( $to, $subject, $message, $headers = '', $attachments = array() ) {
30
+ // Compact the input, apply the filters, and extract them back out.
31
+
32
+ /**
33
+ * Filters the wp_mail() arguments.
34
+ *
35
+ * @since 2.2.0
36
+ *
37
+ * @param array $args A compacted array of wp_mail() arguments, including the "to" email,
38
+ * subject, message, headers, and attachments values.
39
+ */
40
+ $atts = apply_filters(
41
+ 'wp_mail', compact('to', 'subject', 'message', 'headers', 'attachments')
42
+ );
43
+
44
+ if ( isset( $atts['to'] ) ) {
45
+ $to = $atts['to'];
46
+ }
47
+
48
+ if ( ! is_array( $to ) ) {
49
+ $to = explode( ',', $to );
50
+ }
51
+
52
+ if ( isset( $atts['subject'] ) ) {
53
+ $subject = $atts['subject'];
54
+ }
55
+
56
+ if ( isset( $atts['message'] ) ) {
57
+ $message = $atts['message'];
58
+ }
59
+
60
+ if ( isset( $atts['headers'] ) ) {
61
+ $headers = $atts['headers'];
62
+ }
63
+
64
+ if ( isset( $atts['attachments'] ) ) {
65
+ $attachments = $atts['attachments'];
66
+ }
67
+
68
+ if ( ! is_array( $attachments ) ) {
69
+ $attachments = explode( "\n", str_replace( "\r\n", "\n", $attachments ) );
70
+ }
71
+
72
+ global $phpmailer;
73
+
74
+ // (Re)create it, if it's gone missing.
75
+ if ( ! ( $phpmailer instanceof PHPMailer\PHPMailer\PHPMailer ) ) {
76
+ require_once ABSPATH . WPINC . '/PHPMailer/PHPMailer.php';
77
+ require_once ABSPATH . WPINC . '/PHPMailer/SMTP.php';
78
+ require_once ABSPATH . WPINC . '/PHPMailer/Exception.php';
79
+ $phpmailer = new PHPMailer\PHPMailer\PHPMailer( true );
80
+
81
+ $phpmailer::$validator = static function ( $email ) {
82
+ return (bool) is_email( $email );
83
+ };
84
+ }
85
+
86
+ if (($class = get_class($phpmailer)) != 'PHPMailer\PHPMailer\PHPMailer') {
87
+ do_action(
88
+ 'wp_mail_failed',
89
+ new WP_Error(
90
+ 400,
91
+ "Oops! PHPMailer is modified by $class."
92
+ )
93
+ );
94
+ }
95
+
96
+ // Headers.
97
+ $cc = array();
98
+ $bcc = array();
99
+ $reply_to = array();
100
+
101
+ if ( empty( $headers ) ) {
102
+ $headers = array();
103
+ } else {
104
+ if ( ! is_array( $headers ) ) {
105
+ // Explode the headers out, so this function can take
106
+ // both string headers and an array of headers.
107
+ $tempheaders = explode( "\n", str_replace( "\r\n", "\n", $headers ) );
108
+ } else {
109
+ $tempheaders = $headers;
110
+ }
111
+ $headers = array();
112
+
113
+ // If it's actually got contents.
114
+ if ( ! empty( $tempheaders ) ) {
115
+ // Iterate through the raw headers.
116
+ foreach ( (array) $tempheaders as $header ) {
117
+ if ( strpos( $header, ':' ) === false ) {
118
+ if ( false !== stripos( $header, 'boundary=' ) ) {
119
+ $parts = preg_split( '/boundary=/i', trim( $header ) );
120
+ $boundary = trim( str_replace( array( "'", '"' ), '', $parts[1] ) );
121
+ }
122
+ continue;
123
+ }
124
+ // Explode them out.
125
+ list( $name, $content ) = explode( ':', trim( $header ), 2 );
126
+
127
+ // Cleanup crew.
128
+ $name = trim( $name );
129
+ $content = trim( $content );
130
+
131
+ switch ( strtolower( $name ) ) {
132
+ // Mainly for legacy -- process a "From:" header if it's there.
133
+ case 'from':
134
+ $bracket_pos = strpos( $content, '<' );
135
+ if ( false !== $bracket_pos ) {
136
+ // Text before the bracketed email is the "From" name.
137
+ if ( $bracket_pos > 0 ) {
138
+ $from_name = substr( $content, 0, $bracket_pos - 1 );
139
+ $from_name = str_replace( '"', '', $from_name );
140
+ $from_name = trim( $from_name );
141
+ }
142
+
143
+ $from_email = substr( $content, $bracket_pos + 1 );
144
+ $from_email = str_replace( '>', '', $from_email );
145
+ $from_email = trim( $from_email );
146
+
147
+ // Avoid setting an empty $from_email.
148
+ } elseif ( '' !== trim( $content ) ) {
149
+ $from_email = trim( $content );
150
+ }
151
+ break;
152
+ case 'content-type':
153
+ if ( strpos( $content, ';' ) !== false ) {
154
+ list( $type, $charset_content ) = explode( ';', $content );
155
+ $content_type = trim( $type );
156
+ if ( false !== stripos( $charset_content, 'charset=' ) ) {
157
+ $charset = trim( str_replace( array( 'charset=', '"' ), '', $charset_content ) );
158
+ } elseif ( false !== stripos( $charset_content, 'boundary=' ) ) {
159
+ $boundary = trim( str_replace( array( 'BOUNDARY=', 'boundary=', '"' ), '', $charset_content ) );
160
+ $charset = '';
161
+ }
162
+
163
+ // Avoid setting an empty $content_type.
164
+ } elseif ( '' !== trim( $content ) ) {
165
+ $content_type = trim( $content );
166
+ }
167
+ break;
168
+ case 'cc':
169
+ $cc = array_merge( (array) $cc, explode( ',', $content ) );
170
+ break;
171
+ case 'bcc':
172
+ $bcc = array_merge( (array) $bcc, explode( ',', $content ) );
173
+ break;
174
+ case 'reply-to':
175
+ $reply_to = array_merge( (array) $reply_to, explode( ',', $content ) );
176
+ break;
177
+ default:
178
+ // Add it to our grand headers array.
179
+ $headers[ trim( $name ) ] = trim( $content );
180
+ break;
181
+ }
182
+ }
183
+ }
184
+ }
185
+
186
+ // Empty out the values that may be set.
187
+ $phpmailer->clearAllRecipients();
188
+ $phpmailer->clearAttachments();
189
+ $phpmailer->clearCustomHeaders();
190
+ $phpmailer->clearReplyTos();
191
+
192
+
193
+ /*
194
+ * If we don't have an email from the input headers, default to wordpress@$sitename
195
+ * Some hosts will block outgoing mail from this address if it doesn't exist,
196
+ * but there's no easy alternative. Defaulting to admin_email might appear to be
197
+ * another option, but some hosts may refuse to relay mail from an unknown domain.
198
+ * See https://core.trac.wordpress.org/ticket/5007.
199
+ */
200
+ $defaultConnection = false;
201
+ if ( ! isset( $from_email ) ) {
202
+ $defaultConnection = fluentMailDefaultConnection();
203
+ if (!empty($defaultConnection['sender_email'])) {
204
+ $from_email = $defaultConnection['sender_email'];
205
+ } else {
206
+ // Get the site domain and get rid of www.
207
+ $sitename = wp_parse_url( network_home_url(), PHP_URL_HOST );
208
+ if ( 'www.' === substr( $sitename, 0, 4 ) ) {
209
+ $sitename = substr( $sitename, 4 );
210
+ }
211
+ $from_email = 'wordpress@' . $sitename;
212
+ }
213
+ }
214
+
215
+ // Set "From" name and email.
216
+ // If we don't have a name from the input headers.
217
+ if ( ! isset( $from_name ) ) {
218
+ if($defaultConnection && !empty($defaultConnection['sender_name'])) {
219
+ $from_name = $defaultConnection['sender_name'];
220
+ } else {
221
+ $provider = fluentMailGetProvider($from_email);
222
+ if ($provider && !empty($provider->getSetting('sender_name'))) {
223
+ $from_name = $provider->getSetting('sender_name');
224
+ } else {
225
+ $from_name = 'WordPress';
226
+ }
227
+ }
228
+ }
229
+
230
+ /**
231
+ * Filters the email address to send from.
232
+ *
233
+ * @since 2.2.0
234
+ *
235
+ * @param string $from_email Email address to send from.
236
+ */
237
+ $from_email = apply_filters('wp_mail_from', $from_email);
238
+
239
+ /**
240
+ * Filters the name to associate with the "from" email address.
241
+ *
242
+ * @since 2.3.0
243
+ *
244
+ * @param string $from_name Name associated with the "from" email address.
245
+ */
246
+ $from_name = apply_filters('wp_mail_from_name', $from_name);
247
+
248
+ try {
249
+ $phpmailer->setFrom( $from_email, $from_name, false );
250
+ } catch ( PHPMailer\PHPMailer\Exception $e ) {
251
+ $mail_error_data = compact( 'to', 'subject', 'message', 'headers', 'attachments' );
252
+ $mail_error_data['phpmailer_exception_code'] = $e->getCode();
253
+
254
+ /** This filter is documented in wp-includes/pluggable.php */
255
+ do_action(
256
+ 'wp_mail_failed',
257
+ new WP_Error(
258
+ 'wp_mail_failed',
259
+ $e->getMessage(),
260
+ $mail_error_data
261
+ )
262
+ );
263
+
264
+ return false;
265
+ }
266
+
267
+ // Set mail's subject and body.
268
+ $phpmailer->Subject = $subject;
269
+ $phpmailer->Body = $message;
270
+
271
+ // Set destination addresses, using appropriate methods for handling addresses.
272
+ $address_headers = compact( 'to', 'cc', 'bcc', 'reply_to' );
273
+
274
+ foreach ( $address_headers as $address_header => $addresses ) {
275
+ if ( empty( $addresses ) ) {
276
+ continue;
277
+ }
278
+
279
+ foreach ( (array) $addresses as $address ) {
280
+ try {
281
+ // Break $recipient into name and address parts if in the format "Foo <bar@baz.com>".
282
+ $recipient_name = '';
283
+
284
+ if ( preg_match( '/(.*)<(.+)>/', $address, $matches ) ) {
285
+ if ( count( $matches ) == 3 ) {
286
+ $recipient_name = $matches[1];
287
+ $address = $matches[2];
288
+ }
289
+ }
290
+
291
+ switch ( $address_header ) {
292
+ case 'to':
293
+ $phpmailer->addAddress( $address, $recipient_name );
294
+ break;
295
+ case 'cc':
296
+ $phpmailer->addCc( $address, $recipient_name );
297
+ break;
298
+ case 'bcc':
299
+ $phpmailer->addBcc( $address, $recipient_name );
300
+ break;
301
+ case 'reply_to':
302
+ $phpmailer->addReplyTo( $address, $recipient_name );
303
+ break;
304
+ }
305
+ } catch ( PHPMailer\PHPMailer\Exception $e ) {
306
+ continue;
307
+ }
308
+ }
309
+ }
310
+
311
+ // Set to use PHP's mail().
312
+ $phpmailer->isMail();
313
+
314
+ // Set Content-Type and charset.
315
+
316
+ // If we don't have a content-type from the input headers.
317
+ if ( ! isset( $content_type ) ) {
318
+ $content_type = 'text/plain';
319
+ }
320
+
321
+ /**
322
+ * Filters the wp_mail() content type.
323
+ *
324
+ * @since 2.3.0
325
+ *
326
+ * @param string $content_type Default wp_mail() content type.
327
+ */
328
+ $content_type = apply_filters( 'wp_mail_content_type', $content_type );
329
+
330
+ $phpmailer->ContentType = $content_type;
331
+
332
+ // Set whether it's plaintext, depending on $content_type.
333
+ if ( 'text/html' === $content_type ) {
334
+ $phpmailer->isHTML( true );
335
+ }
336
+
337
+ // If we don't have a charset from the input headers.
338
+ if ( ! isset( $charset ) ) {
339
+ $charset = get_bloginfo( 'charset' );
340
+ }
341
+
342
+ /**
343
+ * Filters the default wp_mail() charset.
344
+ *
345
+ * @since 2.3.0
346
+ *
347
+ * @param string $charset Default email charset.
348
+ */
349
+ $phpmailer->CharSet = apply_filters( 'wp_mail_charset', $charset );
350
+
351
+ // Set custom headers.
352
+ if ( ! empty( $headers ) ) {
353
+ foreach ( (array) $headers as $name => $content ) {
354
+ // Only add custom headers not added automatically by PHPMailer.
355
+ if ( ! in_array( $name, array( 'MIME-Version', 'X-Mailer' ), true ) ) {
356
+ try {
357
+ $phpmailer->addCustomHeader( sprintf( '%1$s: %2$s', $name, $content ) );
358
+ } catch ( PHPMailer\PHPMailer\Exception $e ) {
359
+ continue;
360
+ }
361
+ }
362
+ }
363
+
364
+ if ( false !== stripos( $content_type, 'multipart' ) && ! empty( $boundary ) ) {
365
+ $phpmailer->addCustomHeader( sprintf( 'Content-Type: %s; boundary="%s"', $content_type, $boundary ) );
366
+ }
367
+ }
368
+
369
+ if ( ! empty( $attachments ) ) {
370
+ foreach ( $attachments as $attachment ) {
371
+ try {
372
+ $phpmailer->addAttachment( $attachment );
373
+ } catch ( PHPMailer\PHPMailer\Exception $e ) {
374
+ continue;
375
+ }
376
+ }
377
+ }
378
+
379
+ /**
380
+ * Fires after PHPMailer is initialized.
381
+ *
382
+ * @since 2.2.0
383
+ *
384
+ * @param PHPMailer $phpmailer The PHPMailer instance (passed by reference).
385
+ */
386
+ do_action_ref_array( 'phpmailer_init', array( &$phpmailer ) );
387
+
388
+
389
+ // Send!
390
+ try {
391
+ // Trap the fluentSMTPMail mailer here
392
+ $phpmailer = new FluentMail\App\Services\Mailer\FluentPHPMailer($phpmailer);
393
+ return $phpmailer->send();
394
+
395
+ } catch ( PHPMailer\PHPMailer\Exception $e ) {
396
+
397
+ $mail_error_data = compact( 'to', 'subject', 'message', 'headers', 'attachments' );
398
+ $mail_error_data['phpmailer_exception_code'] = $e->getCode();
399
+
400
+ /**
401
+ * Fires after a PHPMailer\PHPMailer\Exception is caught.
402
+ *
403
+ * @since 4.4.0
404
+ *
405
+ * @param WP_Error $error A WP_Error object with the PHPMailer\PHPMailer\Exception message, and an array
406
+ * containing the mail recipient, subject, message, headers, and attachments.
407
+ */
408
+ do_action(
409
+ 'wp_mail_failed',
410
+ new WP_Error(
411
+ 'wp_mail_failed',
412
+ $e->getMessage(),
413
+ $mail_error_data
414
+ )
415
+ );
416
+
417
+ return false;
418
+ }
419
+ }
420
+ else:
421
+ if (!wp_doing_ajax()):
422
+ add_action('admin_notices', function() {
423
+ if(!current_user_can('manage_options')) {
424
+ return;
425
+ }
426
+ ?>
427
+ <div class="notice notice-warning is-dismissible">
428
+ <p>
429
+ <?php
430
+ _e(
431
+ 'The <strong>FluentSMTP</strong> plugin depends on
432
+ <a target="_blank" href="https://developer.wordpress.org/reference/functions/wp_mail/">wp_mail</a> pluggable function and
433
+ plugin is not able to extend it. Please check if another plugin is using this and disable it for <strong>Fluent Mail</strong> to work!',
434
+ 'fluent-smtp'
435
+ );
436
+ ?>
437
+ </p>
438
+ </div>
439
+ <?php
440
+ });
441
+ endif;
442
+ endif;
443
+
app/Hooks/Handlers/AdminMenuHandler.php ADDED
@@ -0,0 +1,135 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ namespace FluentMail\App\Hooks\Handlers;
4
+
5
+ use FluentMail\App\Models\Settings;
6
+ use FluentMail\Includes\Core\Application;
7
+ use FluentMail\App\Services\Mailer\Manager;
8
+
9
+ class AdminMenuHandler
10
+ {
11
+ protected $app = null;
12
+
13
+ public function __construct(Application $application)
14
+ {
15
+ $this->app = $application;
16
+ }
17
+
18
+ public function addFluentMailMenu()
19
+ {
20
+ $title = $this->app->applyCustomFilters('admin-menu-title', __('Fluent SMTP', 'fluent-smtp'));
21
+
22
+ add_submenu_page(
23
+ 'options-general.php',
24
+ $title,
25
+ $title,
26
+ 'manage_options',
27
+ 'fluent-mail',
28
+ [$this, 'renderApp'],
29
+ 16
30
+ );
31
+ }
32
+
33
+ public function renderApp()
34
+ {
35
+ $this->enqueueAssets();
36
+ $this->app->view->render('admin.menu');
37
+ }
38
+
39
+ private function enqueueAssets()
40
+ {
41
+ wp_enqueue_script(
42
+ 'fluent_mail_admin_app_boot',
43
+ fluentMailMix('admin/js/boot.js'),
44
+ ['jquery'],
45
+ FLUENTMAIL_PLUGIN_VERSION
46
+ );
47
+
48
+ wp_enqueue_script('fluentmail-chartjs', fluentMailMix('libs/chartjs/Chart.min.js'), [], FLUENTMAIL_PLUGIN_VERSION);
49
+ wp_enqueue_script('fluentmail-vue-chartjs', fluentMailMix('libs/chartjs/vue-chartjs.min.js'), [], FLUENTMAIL_PLUGIN_VERSION);
50
+
51
+
52
+ wp_enqueue_style(
53
+ 'fluent_mail_admin_app', fluentMailMix('admin/css/fluent-mail-admin.css'), [], FLUENTMAIL_PLUGIN_VERSION
54
+ );
55
+
56
+ wp_localize_script('fluent_mail_admin_app_boot', 'FluentMailAdmin', [
57
+ 'slug' => FLUENTMAIL,
58
+ 'brand_logo' => esc_url(fluentMailMix('images/logo.svg')),
59
+ 'nonce' => wp_create_nonce(FLUENTMAIL),
60
+ 'settings' => $this->getMailerSettings(),
61
+ 'has_fluentcrm' => defined('FLUENTCRM'),
62
+ 'has_fluentform' => defined('FLUENTFORM'),
63
+ 'has_ninja_tables' => defined('NINJA_TABLES_VERSION'),
64
+ 'disable_recommendation' => apply_filters('fluentmail_disable_recommendation', false),
65
+ 'plugin_url' => 'https://fluentsmtp.com/?utm_source=wp&utm_medium=install&utm_campaign=dashboard'
66
+ ]);
67
+
68
+ do_action('fluent_mail_loading_app');
69
+
70
+ wp_enqueue_script(
71
+ 'fluent_mail_admin_app',
72
+ fluentMailMix('admin/js/fluent-mail-admin-app.js'),
73
+ ['fluent_mail_admin_app_boot'],
74
+ FLUENTMAIL_PLUGIN_VERSION,
75
+ true
76
+ );
77
+
78
+ wp_enqueue_script(
79
+ 'fluent_mail_admin_app_vendor',
80
+ fluentMailMix('admin/js/vendor.js'),
81
+ ['fluent_mail_admin_app_boot'],
82
+ FLUENTMAIL_PLUGIN_VERSION,
83
+ true
84
+ );
85
+ }
86
+
87
+ protected function getMailerSettings()
88
+ {
89
+ $settings = $this->app->make(Manager::class)->getMailerConfigAndSettings(true);
90
+
91
+ $settings = array_merge(
92
+ $settings,
93
+ [
94
+ 'user_email' => wp_get_current_user()->user_email
95
+ ]
96
+ );
97
+
98
+ return $settings;
99
+ }
100
+
101
+ public function maybeAdminNotice()
102
+ {
103
+ if (!current_user_can('manage_options')) {
104
+ return;
105
+ }
106
+
107
+ $connections = $this->app->make(Manager::class)->getConfig('connections');
108
+
109
+ global $wp_version;
110
+
111
+ $requireUpdate = version_compare($wp_version,'5.5', '<');
112
+
113
+ if($requireUpdate) { ?>
114
+ <div class="notice notice-warning">
115
+ <p>
116
+ <?php echo sprintf(__('WordPress version 5.5 or greater is required for FluentSMTP. You are using version %s currently. Please update your WordPress Core to use FluentSMTP Plugin.', 'fluent-smtp'), $wp_version); ?>
117
+ </p>
118
+ </div>
119
+ <?php } else if(empty($connections)) {
120
+ ?>
121
+ <div class="notice notice-warning">
122
+ <p>
123
+ <?php _e('FluentSMTP requires to configure properly. Please configure FluentSMTP to make your email delivery works.', 'fluent-smtp'); ?>
124
+ </p>
125
+ <p>
126
+ <a href="<?php echo admin_url('options-general.php?page=fluent-mail#/'); ?>" class="button button-primary">
127
+ <?php _e('Configure FluentSMTP', 'fluent-smtp'); ?>
128
+ </a>
129
+ </p>
130
+ </div>
131
+ <?php
132
+ }
133
+
134
+ }
135
+ }
app/Hooks/Handlers/ExceptionHandler.php ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ namespace FluentMail\App\Hooks\Handlers;
4
+
5
+ class ExceptionHandler
6
+ {
7
+ protected $handlers = [
8
+ 'FluentMail\Includes\Support\ForbiddenException' => 'handleForbiddenException',
9
+ 'FluentMail\Includes\Support\ValidationException' => 'handleValidationException'
10
+ ];
11
+
12
+ public function handle($e)
13
+ {
14
+ foreach ($this->handlers as $key => $value) {
15
+ if ($e instanceof $key) {
16
+ return $this->{$value}($e);
17
+ }
18
+ }
19
+ }
20
+
21
+ public function handleForbiddenException($e)
22
+ {
23
+ wp_send_json_error([
24
+ 'message' => $e->getMessage()
25
+ ], $e->getCode() ?: 403);
26
+ }
27
+
28
+ public function handleValidationException($e)
29
+ {
30
+ wp_send_json_error([
31
+ 'message' => $e->getMessage(),
32
+ 'errors' => $e->errors()
33
+ ], $e->getCode() ?: 422);
34
+ }
35
+ }
app/Hooks/Handlers/ProviderValidator.php ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ namespace FluentMail\App\Hooks\Handlers;
4
+
5
+ class ProviderValidator
6
+ {
7
+ public function handle($provider, $errors = [])
8
+ {
9
+ if ($validator = $this->getProviderValidator($provider, $errors)) {
10
+ return $validator->validate();
11
+ }
12
+
13
+ return $errors;
14
+ }
15
+
16
+ protected function getProviderValidator($provider, $errors)
17
+ {
18
+ $key = $provider['provider'];
19
+
20
+ $path = FluentMail('path.app') . 'Services/Mailer/Providers/' . $key;
21
+
22
+ $file = $path . '/' . 'Validator.php';
23
+
24
+
25
+ if (file_exists($file)) {
26
+ $ns = 'FluentMail\App\Services\Mailer\Providers\\' . $key;
27
+
28
+ $class = $ns . '\Validator';
29
+
30
+ if (class_exists($class)) {
31
+ return new $class($provider, $errors);
32
+ }
33
+ }
34
+ }
35
+ }
app/Hooks/actions.php ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ $app->addCustomAction('handle_exception', 'ExceptionHandler@handle');
4
+
5
+ $app->addAction('admin_menu', 'AdminMenuHandler@addFluentMailMenu');
6
+
7
+ $app->addAction('admin_notices', 'AdminMenuHandler@maybeAdminNotice');
8
+
9
+ $app->addAction('fluentmail_do_daily_scheduled_tasks', function () {
10
+ $manager = fluentMail(\FluentMail\App\Services\Mailer\Manager::class);
11
+ $logSaveDays = $manager->getSettings('misc.log_saved_interval_days');
12
+ $logSaveDays = intval($logSaveDays);
13
+ if($logSaveDays) {
14
+ (new \FluentMail\App\Models\Logger())->deleteLogsOlderThan($logSaveDays);
15
+ }
16
+
17
+ $day = date('d');
18
+ if($day == '01') {
19
+ // this is a monthly cron
20
+ }
21
+ });
app/Hooks/filters.php ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+
4
+ add_filter( 'plugin_action_links_' . plugin_basename( FLUENTMAIL_PLUGIN_FILE ), function ($links) {
5
+ $links['settings'] = sprintf(
6
+ '<a href="%s" aria-label="%s">%s</a>',
7
+ admin_url('options-general.php?page=fluent-mail#/connections'),
8
+ esc_attr__( 'Go to Fluent SMTP Settings page', 'fluent-smtp' ),
9
+ esc_html__( 'Settings', 'fluent-smtp' )
10
+ );
11
+
12
+ return $links;
13
+
14
+ }, 10, 1 );
app/Http/Controllers/Controller.php ADDED
@@ -0,0 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ namespace FluentMail\App\Http\Controllers;
4
+
5
+ use FluentMail\App\App;
6
+
7
+ abstract class Controller
8
+ {
9
+ /**
10
+ * @var \FluentMail\App\Plugin
11
+ */
12
+ protected $app = null;
13
+
14
+ /**
15
+ * @var \FluentMail\Includes\Request\Request
16
+ */
17
+ protected $request = null;
18
+
19
+ /**
20
+ * @var \FluentMail\Includes\Response\Response
21
+ */
22
+ protected $response = null;
23
+
24
+ public function __construct()
25
+ {
26
+ $this->app = App::getInstance();
27
+ $this->request = $this->app['request'];
28
+ $this->response = $this->app['response'];
29
+ }
30
+
31
+ public function send($data = null, $code = 200)
32
+ {
33
+ return $this->response->send($data, $code);
34
+ }
35
+
36
+ public function sendSuccess($data = null, $code = 200)
37
+ {
38
+ return $this->response->sendSuccess($data, $code);
39
+ }
40
+
41
+ public function sendError($data = null, $code = 423)
42
+ {
43
+ return $this->response->sendError($data, $code);
44
+ }
45
+
46
+ public function verify()
47
+ {
48
+ $permission = 'manage_options';
49
+ if(!current_user_can($permission)) {
50
+ wp_send_json_error([
51
+ 'message' => __('You do not have permission to do this action', 'fluent-smtp')
52
+ ]);
53
+ die();
54
+ }
55
+
56
+ return true;
57
+ }
58
+ }
app/Http/Controllers/DashboardController.php ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ namespace FluentMail\App\Http\Controllers;
4
+
5
+ use FluentMail\App\Models\Logger;
6
+ use FluentMail\App\Services\Mailer\Manager;
7
+ use FluentMail\App\Services\Reporting;
8
+ use FluentMail\Includes\Request\Request;
9
+
10
+ class DashboardController extends Controller
11
+ {
12
+ public function index(Logger $logger, Manager $manager)
13
+ {
14
+ $this->verify();
15
+
16
+ $connections = $manager->getSettings('connections', []);
17
+
18
+ return $this->send([
19
+ 'stats' => $logger->getStats(),
20
+ 'settings_stat' => [
21
+ 'connection_counts' => count($connections),
22
+ 'active_senders' => count($manager->getSettings('mappings', [])),
23
+ 'auto_delete_days' => $manager->getSettings('misc.log_saved_interval_days'),
24
+ 'log_enabled' => $manager->getSettings('misc.log_emails')
25
+ ]
26
+ ]);
27
+ }
28
+
29
+ public function getSendingStats(Request $request, Reporting $reporting)
30
+ {
31
+ list($from, $to) = $request->get('date_range');
32
+
33
+ return $this->send([
34
+ 'stats' => $reporting->getSendingStats($from, $to)
35
+ ]);
36
+
37
+ }
38
+
39
+ }
app/Http/Controllers/LoggerController.php ADDED
@@ -0,0 +1,82 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ namespace FluentMail\App\Http\Controllers;
4
+
5
+ use FluentMail\App\Models\Logger;
6
+ use FluentMail\Includes\Request\Request;
7
+
8
+ class LoggerController extends Controller
9
+ {
10
+ public function get(Request $request, Logger $logger)
11
+ {
12
+ $this->verify();
13
+
14
+ return $this->send(
15
+ $logger->get(
16
+ $request->except(['nonce', 'action'])
17
+ )
18
+ );
19
+ }
20
+
21
+ public function show(Request $request, Logger $logger)
22
+ {
23
+ $this->verify();
24
+
25
+ $result = $logger->navigate($request->all());
26
+
27
+ return $this->sendSuccess($result);
28
+ }
29
+
30
+ public function delete(Request $request, Logger $logger)
31
+ {
32
+ $this->verify();
33
+
34
+ $id = (array) $request->get('id');
35
+
36
+ $logger->delete($id);
37
+
38
+ if ($id && $id[0] == 'all') {
39
+ $subject = 'All logs';
40
+ } else {
41
+ $count = count($id);
42
+ $subject = $count > 1 ? "{$count} Logs" : 'Log';
43
+ }
44
+
45
+ return $this->sendSuccess([
46
+ 'message' => "{$subject} deleted successfully."
47
+ ]);
48
+ }
49
+
50
+ public function retry(Request $request, Logger $logger)
51
+ {
52
+ $this->verify();
53
+
54
+ try {
55
+ $this->app->addAction('wp_mail_failed', function($response) use ($logger, $request) {
56
+
57
+ $log = $logger->find($id = $request->get('id'));
58
+ $log['retries'] = $log['retries'] + 1;
59
+ $logger->updateLog($log, ['id' => $id]);
60
+
61
+ $this->sendError([
62
+ 'message' => $response->get_error_message(),
63
+ 'errors' => $response->get_error_data()
64
+ ], $response->get_error_code());
65
+ });
66
+
67
+ if ($email = $logger->resendEmailFromLog($request->get('id'), $request->get('type'))) {
68
+ return $this->sendSuccess([
69
+ 'email' => $email,
70
+ 'message' => __('Email sent successfully.', 'fluent-smtp')
71
+ ]);
72
+ }
73
+
74
+ throw new \Exception('Something went wrong', 400);
75
+
76
+ } catch (\Exception $e) {
77
+ return $this->sendError([
78
+ 'message' => $e->getMessage()
79
+ ], $e->getCode());
80
+ }
81
+ }
82
+ }
app/Http/Controllers/SettingsController.php ADDED
@@ -0,0 +1,360 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ namespace FluentMail\App\Http\Controllers;
4
+
5
+ use Exception;
6
+ use FluentMail\App\Models\Settings;
7
+ use FluentMail\Includes\Request\Request;
8
+ use FluentMail\Includes\Support\ValidationException;
9
+ use FluentMail\App\Services\Mailer\Providers\Factory;
10
+
11
+ class SettingsController extends Controller
12
+ {
13
+ public function index(Settings $settings)
14
+ {
15
+ $this->verify();
16
+
17
+ try {
18
+ $settings = $settings->get();
19
+
20
+ return $this->sendSuccess([
21
+ 'settings' => $settings
22
+ ]);
23
+
24
+ } catch (Exception $e) {
25
+ return $this->sendError([
26
+ 'message' => $e->getMessage()
27
+ ], $e->getCode());
28
+ }
29
+ }
30
+
31
+ public function validate(Request $request, Settings $settings, Factory $factory)
32
+ {
33
+ $this->verify();
34
+
35
+ try {
36
+ $data = $request->except(['action', 'nonce']);
37
+
38
+ $provider = $factory->make($data['provider']['key']);
39
+
40
+ $provider->validateBasicInformation($data);
41
+
42
+ $this->sendSuccess();
43
+
44
+ } catch (ValidationException $e) {
45
+ $this->sendError($e->errors(), $e->getCode());
46
+ }
47
+ }
48
+
49
+ public function store(Request $request, Settings $settings, Factory $factory)
50
+ {
51
+ $this->verify();
52
+
53
+ try {
54
+ $data = $request->except(['action', 'nonce']);
55
+
56
+ $data = wp_unslash($data);
57
+
58
+ $provider = $factory->make($data['connection']['provider']);
59
+
60
+ $connection = $data['connection'];
61
+ $this->validateConnection($provider, $connection);
62
+ $provider->checkConnection($connection);
63
+
64
+ $data['valid_senders'] = $provider->getValidSenders($connection);
65
+
66
+ $settings->store($data);
67
+