PluginProbe
Zoho ZeptoMail / 3.1.4
Zoho ZeptoMail v3.1.4
trunk 1.0.1 1.0.2 1.0.3 1.0.4 2.0.0 2.0.1 2.0.2 2.0.3 2.1.0 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 3.0.1 3.1.0 3.1.1 3.1.2 3.1.3 3.1.4 3.2.0 3.2.1 3.2.2 3.2.3 All 35 releases
transmail / transMail.php

transMail.php in Zoho ZeptoMail 3.1.4, at transMail.php

544 lines 22.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /*
3 Plugin Name: ZeptoMail
4 Version: 3.1.4
5 Plugin URI: https://zeptomail.zoho.com/
6 Author: Zoho Mail
7 Author URI: https://www.zoho.com/zeptomail/
8 Description: Configure your ZeptoMail account to send email from your WordPress site.
9 Text Domain: ZeptoMail
10 Domain Path: /languages
11 */
12 /*
13 Copyright (c) 2015, ZOHO CORPORATION
14 All rights reserved.
15
16 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
17
18 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
19
20 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
21
22 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 */
24 require_once plugin_dir_path( __FILE__ ) . 'includes/class-transmail-helper.php';
25
26 function ztm_zmplugin_script() {
27 wp_enqueue_style( 'zm_zohoTransMail_style', plugin_dir_url( __FILE__ ) . 'assets/css/style.css', false, '1.0.0' );
28 }
29
30 add_action( 'admin_enqueue_scripts', 'ztm_zmplugin_script');
31
32 function zohoTransMail_deactivate() {
33 //--------------Clear the credentials once deactivated-------------------
34 delete_option('transmail_send_mail_token');
35 delete_option('transmail_bounce_email_address');
36 delete_option('transmail_from_email_id');
37 delete_option('transmail_from_name');
38 delete_option('transmail_content_type');
39 delete_option('transmail_domain_name');
40
41 }
42
43 register_deactivation_hook( __FILE__, 'zohoTransMail_deactivate');
44
45
46
47 function transmail_integ_settings() {
48 add_menu_page (
49 'Welcome to ZeptoMail by Zoho Mail',
50 'ZeptoMail',
51 'manage_options',
52 'transmail-settings',
53 'transmail_settings_callback' ,
54 'dashicons-email'
55 );
56 add_submenu_page (
57 'transmail-settings',
58 'ZeptoMail by Zoho Mail',
59 'Configure Account',
60 'manage_options',
61 'transmail-settings',
62 'transmail_settings_callback'
63 );
64 add_submenu_page (
65 'transmail-settings',
66 'Send Mail - ZeptoMail by Zoho Mail',
67 'Test Mail',
68 'manage_options',
69 'transmail-send-mail',
70 'transmail_send_mail_callback'
71 );
72 }
73
74
75
76
77 function transmail_settings_callback() {
78
79 if (isset($_POST['transmail_submit']) && !empty($_POST)) {
80 $nonce = sanitize_text_field($_REQUEST['_wpnonce']);
81 if (!wp_verify_nonce($nonce, 'transmail_settings_nonce')) {
82 echo '<div class="error"><p><strong>'.esc_html__('Reload the page again').'</strong></p></div>'."\n";
83 } else {
84 $need_to_test = 0;
85 $transmail_send_mail_token = sanitize_text_field($_POST['transmail_send_mail_token']);
86 $transmail_from_email_id = sanitize_email($_POST['transmail_from_email_id']);
87 $transmail_domain_name = sanitize_text_field($_POST['transmail_domain_name']);
88 $transmail_from_name = sanitize_text_field($_POST['transmail_from_name']);
89 $transmail_content_type = sanitize_text_field($_POST['transmail_content_type']);
90 if(strcmp($transmail_from_email_id, get_option('transmail_from_email_id')) != 0
91 || strcmp($transmail_send_mail_token, base64_decode(get_option('transmail_send_mail_token'))) != 0) {
92
93 $need_to_test = 1;
94
95 }
96 if(get_option('transmail_send_mail_token') == null || strcmp(get_option("last_test_result"), "failed") == 0) {
97 $need_to_test = 1;
98 }
99 update_option('transmail_send_mail_token',base64_encode($transmail_send_mail_token), false);
100 update_option('transmail_from_email_id',$transmail_from_email_id, false);
101 update_option('transmail_from_name',stripslashes($transmail_from_name), false);
102 update_option('transmail_content_type',$transmail_content_type, false);
103 update_option('transmail_domain_name',$transmail_domain_name, false);
104 if ( $need_to_test == 1) {
105 $headers = array('Content-Type: text/html; charset=UTF-8');
106 wp_mail($transmail_from_email_id, "ZeptoMail plugin for WordPress - Test Email", "<html><body><p>Hello,</p><br><br><p>We're glad you're using our ZeptoMail plugin. This is a test email to verify your configuration details.
107 Thank you for choosing ZeptoMail for your transactional email needs.<p><br><br>Team ZeptoMail", $headers, null);
108 $data = json_decode(get_option('transmail_test_mail_case'));
109 if (!empty($data->data)) {
110 echo '<div class="updated"><p><strong>'.esc_html__('Plugin has been configured successfully!').'</strong></p></div>'."\n";
111 update_option("last_test_result","sucess",false);
112 } else {
113
114 $message= ''.$data->error->details[0]->message;
115 if(!empty($data->error)) {
116 if(!empty($data->error->details[0]->message) && strcmp($data->error->details[0]->message,"Invalid API Token found") == 0 ) {
117 $message = "Configuration failed. Enter a valid Send Mail token and try again.";
118 }
119 if(!empty($data->error->details[0]->target) && strcmp($data->error->details[0]->target,"from") == 0 ) {
120 $message = "Configuration failed. Enter a valid From address and try again.";
121 }
122 update_option("last_test_result","failed",false);
123 }
124 else{
125 $message= 'Configuration failed. Verify details and try again. For further assistance, contact <a href="mailto:support@zeptomail.com">support.</a>';
126 }
127 echo '<div class="error"><p><strong>'.($message).'</strong></p></div>'."\n";
128 }
129 } else {
130 echo '<div class="updated"><p><strong>'.esc_html__('Configuration saved successfully.').'</strong></p></div>'."\n";
131 }
132 }
133 }
134 ?>
135 <head>
136 <meta charset="UTF-8">
137 <title>ZeptoMail by Zoho Mail</title>
138 </head>
139 <body>
140 <form method="post" action="<?php echo $_SERVER["REQUEST_URI"]; ?>">
141 <?php wp_nonce_field('transmail_settings_nonce'); ?>
142 <div class="page"><div class="page__content">
143 <div class="page__header">
144 <h2 style="display: flex; align-items: center;" ><img src=<?php echo esc_url(plugins_url('assets/images/icon.png',__FILE__))?> title="Zoho" alt="Zoho" width="60" style="margin-right: 15px;"> Welcome to ZeptoMail by Zoho Mail</h2>
145 <p>Visit <a class="zm_a" href=<?php echo esc_url("https://zeptomail.zoho.com/#dashboard/setupDetail")?> target="_blank">here</a> to generate your Send Mail token.</p>
146 </div>
147 <div class="form">
148 <div class="form__row">
149 <label class="form--label">Where is your account hosted?</label>
150 <select class="form--input form--input--select" name="transmail_domain_name">
151 <option value="zoho.com" <?php if(get_option('transmail_domain_name') == "zoho.com") {?> selected="true"<?php } ?>>zeptomail.zoho.com</option>
152 <option value="zoho.eu" <?php if(get_option('transmail_domain_name') == "zoho.eu") {?> selected="true"<?php } ?>>zeptomail.zoho.eu</option>
153 <option value="zoho.in" <?php if(get_option('transmail_domain_name') == "zoho.in") {?> selected="true"<?php }?>>zeptomail.zoho.in</option>
154 <option value="zoho.com.cn" <?php if(get_option('transmail_domain_name') == "zoho.com.cn") {?>selected="true"<?php }?>>zeptomail.zoho.com.cn</option>
155 <option value="zoho.com.au" <?php if(get_option('transmail_domain_name') == "zoho.com.au"){?>selected="true"<?php }?>>zeptomail.zoho.com.au</option>
156 <option value="zohocloud.ca" <?php if(get_option('transmail_domain_name') == "zohocloud.ca"){?>selected="true"<?php }?>>zeptomail.zohocloud.ca</option>
157 <option value="zoho.sa" <?php if(get_option('transmail_domain_name') == "zoho.sa"){?>selected="true"<?php }?>>zeptomail.zoho.sa</option>
158 </select> <br><i class="form__row-info">The region where your ZeptoMail account is hosted. This is the URL displayed on login to your account.</i> </div>
159 <div class="form__row">
160 <label class="form--label">Send Mail Token</label>
161 <input type="password" value="<?php echo base64_decode(get_option('transmail_send_mail_token')); ?>" name="transmail_send_mail_token" class="form--input" id="transmail_send_mail_token" required/>
162 <i class="form__row-info">Send Mail token of the relevant Mail Agent generated in your ZeptoMail account.</i>
163 </div>
164 <div class="form__row">
165 <label class="form--label">From Email Address</label>
166 <input type="text" name="transmail_from_email_id" value="<?php echo get_option('transmail_from_email_id') ?>" class="form--input" id="transmail_from_email_id" required/> <i class="form__row-info">The email address that will be used as the From address to send emails
167 .</i> </div>
168 <div class="form__row">
169 <label class="form--label">From Name</label>
170 <input type="text" name="transmail_from_name" value="<?php echo get_option('transmail_from_name') ?>" class="form--input" id="transmail_from_name" required/> <i class="form__row-info">The sender name displayed on the emails sent from the plugin.</i> </div>
171 <div class="form__row">
172 <label class="form--label">Mail Format</label>
173 <select class="form--input form--input--select" name="transmail_content_type">
174 <option value="plaintext" <?php if(get_option('transmail_content_type') == "plaintext") {?> selected="true"<?php } ?>>Plaintext</option>
175 <option value="html" <?php if(get_option('transmail_content_type') == "html") {?> selected="true"<?php } ?>>HTML</option>
176 </select> <br><i class="form__row-info">The preferred email format for the body of your email.</i> </div>
177 <div class="form__row form__row-btn">
178 <input type="submit" name="transmail_submit" id="transmail_submit" class="btn" value="Save"/>
179 </div>
180 </div>
181 </div>
182 </div>
183 </form>
184 </body>
185 <?php
186
187
188 }
189 add_action('admin_menu','transmail_integ_settings');
190
191
192
193
194
195 function transmail_send_mail_callback() {
196
197
198
199
200 $option = get_option('transmail_send_mail_token');
201 if(!empty($option)){
202
203 if(is_admin() || current_user_can('administrator')) {
204 if(isset($_POST['transmail_send_mail_submit']) && !empty($_POST)){
205 $nonce = sanitize_text_field($_REQUEST['_wpnonce']);
206 if (!wp_verify_nonce($nonce, 'transmail_send_mail_nonce')) {
207 echo '<div class="error"><p><strong>'.esc_html__('Reload the page again').'</strong></p></div>'."\n";
208 } else {
209 if(empty($option)){
210 echo '<div class="error"><p><strong>'.esc_html__('Account not Configured').'</strong></p></div>'."\n";
211 }
212 $toAddressTest = sanitize_email($_POST['transmail_to_address']);
213 $subjectTest = sanitize_text_field($_POST['transmail_subject']);
214 $contentTest = sanitize_text_field($_POST['transmail_content']);
215 if(wp_mail($toAddressTest,$subjectTest,$contentTest,'', null)) {
216 echo '<div class="updated"><p><strong>'.esc_html__('Mail Sent Successfully').'</strong></p></div>'."\n";
217 } else {
218 echo '<div class="error"><p><strong>'.esc_html__('Mail Sending Failed').'</strong></p></div>'."\n";
219 }
220 }
221 }
222 }
223 ?>
224 <head>
225 <meta charset="UTF-8">
226 <title>Zoho Mail</title>
227 </head>
228
229 <form method="post" enctype="multipart/form-data" action="<?php echo $_SERVER["REQUEST_URI"]; ?>">
230 <?php wp_nonce_field('transmail_send_mail_nonce'); ?>
231 <body>
232 <div class="page"><div class="page__content">
233 <div class="page__header">
234 <h1>Send Mail <span class="ico-send"></span></h1>
235 </div>
236 <div class="form">
237 <div class="form__row">
238 <label class="form--label">To</label>
239 <input type="text" class="form--input" name="transmail_to_address" required = "required"/> </div>
240 <div class="form__row">
241 <label class="form--label">Subject</label>
242 <input type="text" class="form--input" name="transmail_subject" required = "required"/> </div>
243 <div class="form__row">
244 <label class="form--label">Content</label>
245 <input type="text" class="form--input" name="transmail_content"/> </div>
246 <div class="form__row form__row-btn"> <input type="submit" class = "btn" name="transmail_send_mail_submit" id="transmail_send_mail_submit" value="<?php _e('Send Mail');?>">
247
248 </div>
249 </div>
250 </div>
251 </div>
252 </body>
253 </form>
254 <?php
255
256 }
257 else {
258 echo '<div class="error"><p><strong>'.__('Configure Your Account.').'</strong></p></div>'."\n";
259 }
260
261 }
262
263
264 if(!function_exists('wp_mail')) {
265 function wp_mail( $to, $subject, $message, $headers = '', $attachments = array() ) {
266
267 $atts = apply_filters( 'wp_mail', compact( 'to', 'subject', 'message', 'headers', 'attachments' ) );
268
269 if ( isset( $atts['to'] ) ) {
270 $to = $atts['to'];
271 }
272 if ( !is_array( $to ) ) {
273 $to = explode( ',', $to );
274 }
275 if ( isset( $atts['subject'] ) ) {
276 $subject = $atts['subject'];
277 }
278 if ( isset( $atts['message'] ) ) {
279 $message = $atts['message'];
280 }
281 if ( isset( $atts['headers'] ) ) {
282 $headers = $atts['headers'];
283 } else {
284 $headers = '';
285 }
286 if ( isset( $atts['attachments'] ) ) {
287 $attachments = $atts['attachments'];
288 }
289 if (!is_array($attachments)) {
290 $attachments = $attachments ? array($attachments) : array();
291 }
292 foreach ($attachments as &$attachment) {
293 $attachment = str_replace("\r\n", "\n", $attachment);
294 }
295
296 $attachments = implode("\n", $attachments);
297
298 $content_type = null;
299 // Headers
300 $cc = $bcc = $reply_to = array();
301 $dynamicFrom = array();
302 if ( empty( $headers ) ) {
303 $headers = array('');
304 } else {
305 if ( !is_array( $headers ) ) {
306 // Explode the headers out, so this function can take both
307 // string headers and an array of headers.
308 $tempheaders = explode( "\n", str_replace( "\r\n", "\n", $headers ) );
309 } else {
310 $tempheaders = $headers;
311 }
312 $headers = array();
313 // If it's actually got contents
314 if ( !empty( $tempheaders ) ) {
315 // Iterate through the raw headers
316 foreach ( (array) $tempheaders as $header ) {
317 if ( strpos($header, ':') === false ) {
318 if ( false !== stripos( $header, 'boundary=' ) ) {
319 $parts = preg_split('/boundary=/i', trim( $header ) );
320 $boundary = trim( str_replace( array( "'", '"' ), '', $parts[1] ) );
321 }
322 continue;
323 }
324 // Explode them out
325 list( $name, $content ) = explode( ':', trim( $header ), 2 );
326
327 // Cleanup crew
328 $name = trim( $name );
329 $content = trim( $content );
330 $content_type = null;
331 $from = array();
332 switch ( strtolower( $name ) ) {
333 case 'content-type':
334 if ( strpos( $content, ';' ) !== false ) {
335 list( $type, $charset_content ) = explode( ';', $content );
336 $content_type = trim( $type );
337 if ( false !== stripos( $charset_content, 'charset=' ) ) {
338 $charset = trim( str_replace( array( 'charset=', '"' ), '', $charset_content ) );
339 } elseif ( false !== stripos( $charset_content, 'boundary=' ) ) {
340 $boundary = trim( str_replace( array( 'BOUNDARY=', 'boundary=', '"' ), '', $charset_content ) );
341 $charset = '';
342 }
343
344 // Avoid setting an empty $content_type.
345 } elseif ( '' !== trim( $content ) ) {
346 $content_type = trim( $content );
347 }
348 break;
349 case 'cc':
350 $cc = array_merge( (array) $cc, explode( ',', $content ) );
351 break;
352 case 'bcc':
353 $bcc = array_merge( (array) $bcc, explode( ',', $content ) );
354 break;
355 case 'reply-to':
356 $reply_to = array_merge( (array) $reply_to, explode( ',', $content ) );
357 break;
358 case 'from':
359 $dynamicFrom = array_merge( (array) $from, explode( ',', $content ) );
360 break;
361 default:
362 $headers[trim( $name )] = trim( $content );
363 break;
364 }
365 }
366 }
367 }
368 $content_type = apply_filters( 'wp_mail_content_type', $content_type );
369 $data = array();
370 $token = base64_decode(get_option('transmail_send_mail_token'));
371 $fromAddress = array();
372 if (!empty($from_name)) {
373 $fromAddress['name'] = $from_name;
374 } else {
375 $fromAddress['name'] = get_option('transmail_from_name');
376 }
377 if (!empty($dynamicFrom))
378 {
379 $dynpos = false;
380 $dynpos = strpos($dynamicFrom[0], '<');
381 if($dynpos !== false) {
382 $dynad = substr($dynamicFrom[0], $dynpos+1, strlen($dynamicFrom[0])-$dynpos-2);
383 $dynfrom['address'] = sanitize_email($dynad);
384 if($dynpos >0) {
385 $dynfrom['name'] = substr($dynamicFrom[0],0,$dynpos-1);
386 $fromAddress['name'] = $dynfrom['name'];
387 }
388 $fromAddress['address'] = $dynfrom['address'];
389 }
390
391 }
392 else {
393 $fromAddress['address'] = get_option('transmail_from_email_id');
394
395
396 }
397
398 $data['from'] = $fromAddress;
399 $zmbccs = array();
400 $zmbcc = array();
401 $zmbce = array();
402 if (!empty($bcc)) {
403 $count = 0;
404 foreach($bcc as $bc) {
405 $zmbcc['address'] = $bc;
406 $zmbce['email_address'] = $zmbcc;
407 $zmbccs[$count] = $zmbce;
408 $count = $count + 1;
409 }
410 $data['bcc'] = $zmbccs;
411 }
412
413 if(!empty($reply_to)) {
414 $replyTos = array();
415 $replyTo = array();
416 $rte = array();
417 $count = 0;
418 foreach($reply_to as $reply) {
419 $pos = strpos($reply, '<');
420 if($pos !== false) {
421 $ad = substr($reply, $pos+1, strlen($reply)-$pos-2);
422 $replyTo['address'] = $ad;
423 $replyTo['name'] = substr($reply,0,$pos-1);
424 } else {
425 $replyTo['address'] = $reply;
426 }
427 $replyTos[$count] = $replyTo;
428 $count = $count + 1;
429 }
430 $data['reply_to'] = $replyTos;
431 }
432 $data['subject'] = $subject;
433
434 if(!empty($to) && is_array($to)) {
435 $tos = array();
436 $count = 0;
437 foreach($to as $t) {
438 $toa = array();
439 $toe = array();
440 $pos = strpos($t, '<');
441 if($pos !== false) {
442 $ad = substr($t, $pos+1, strlen($t)-$pos-2);
443 $toa['address'] = sanitize_email($ad);
444 $toa['name'] = substr($t,0,$pos-1);
445 } else {
446 $toa['address'] = sanitize_email($t);
447 }
448 $toe['email_address'] = $toa;
449 $tos[$count] = $toe;
450 $count = $count + 1;
451 }
452 $data['to'] = $tos;
453 } else {
454 $toa = array();
455 $tos = array();
456 $toa['address'] = $to;
457 $tos[0] = $toa;
458 $data['to'] = $to;
459 }
460 if (!empty($attachments)) {
461 if (!is_array($attachments)) {
462 $attachments = explode("\n", $attachments);
463 }
464
465 $attachmentJSONArr = array();
466 $count = 0;
467
468 foreach ($attachments as $attfile) {
469 if (file_exists($attfile)) {
470 $attachmentupload = array(
471 'name' => basename($attfile),
472 'mime_type' => mime_content_type($attfile),
473 'content' => base64_encode(file_get_contents($attfile))
474 );
475 $attachmentJSONArr[$count] = $attachmentupload;
476 $count = $count + 1;
477 } else {
478 error_log("Attachment file does not exist: " . $attfile);
479 }
480 }
481
482 $data['attachments'] = $attachmentJSONArr;
483 }
484 if( $content_type == 'text/html' || get_option('transmail_content_type') == 'html') {
485 $data['htmlbody'] = $message;
486 } else {
487 $data['textbody'] = $message;
488
489 }
490
491
492 $headers1 = array(
493 'Authorization' => $token,
494 'User-Agent' => 'Zepto_WordPress'
495 );
496 $data_string = json_encode($data);
497 $args = array(
498 'body' => $data_string,
499 'headers' => $headers1,
500 'method' => 'POST'
501 );
502 $domainName = get_option('transmail_domain_name');
503 if (strpos($domainName, 'zoho') === false) {
504 $domainName = 'zoho.'.$domainName;
505 }
506 $urlToSend = Transmail_Helper::getZeptoMailUrlForDomain($domainName).'/v1.1/email';
507 $responseSending = wp_remote_post( $urlToSend, $args );
508 $http_code = wp_remote_retrieve_response_code($responseSending);
509 if(!is_wp_error( $responseSending ))
510 update_option('transmail_test_mail_case', $responseSending['body'], false);
511
512 $responseBody = wp_remote_retrieve_body($responseSending);
513 $responseData = json_decode($responseBody);
514
515 if($http_code == '200' || $http_code == '201') {
516 return true;
517 }
518
519 if (is_object($responseData) && isset($responseData->error)) {
520 $details = $responseData->error->details;
521 if (is_array($details) && isset($details[0]->message)) {
522 $message = $details[0]->message;
523 } else {
524 $message = "Error details are not available.";
525 }
526 } else {
527 $message = "Error property is not present in the response.";
528 }
529
530
531 $mail_data = array(
532 'to' => $to,
533 'subject' => $subject,
534 'message' => $message,
535 'headers' => $headers1,
536 'attachments' => $attachments
537 );
538
539 do_action( 'wp_mail_failed', new WP_Error( 'wp_mail_failed', $message, $mail_data ) );
540 return false;
541
542 }
543 }
544