From 34356f6d27e564c0d0b687e6cb384e31219222b0 Mon Sep 17 00:00:00 2001 From: Yehuda Hassine Date: Wed, 1 May 2019 00:25:40 +0300 Subject: smtp mailer + bugs --- Postman/Phpmailer/PhpmailerInit.php | 169 +++++++++++++++++++++++++++++++++++ Postman/Phpmailer/PostsmtpMailer.php | 103 +++++++++++++++++++++ 2 files changed, 272 insertions(+) create mode 100644 Postman/Phpmailer/PhpmailerInit.php create mode 100644 Postman/Phpmailer/PostsmtpMailer.php (limited to 'Postman/Phpmailer') diff --git a/Postman/Phpmailer/PhpmailerInit.php b/Postman/Phpmailer/PhpmailerInit.php new file mode 100644 index 0000000..60d27f2 --- /dev/null +++ b/Postman/Phpmailer/PhpmailerInit.php @@ -0,0 +1,169 @@ +set_vars(); + $this->hooks(); + } + + public function set_vars() { + $this->options = PostmanOptions::getInstance(); + } + + public function hooks() + { + add_action( 'phpmailer_init', [ $this, 'phpmailer_init'] ); + add_action( 'wp_mail_failed', [ $this, 'wp_mail_failed' ] ); + add_filter( 'postman_wp_mail_result', [ $this, 'postman_wp_mail_result'] ); + } + + /** + * @param PHPMailer $mailer + */ + public function phpmailer_init($mailer) { + + if ( $this->options->getTransportType() !== 'smtp' ) { + return $mailer; + } + + $mailer->SMTPDebug = 2; + $mailer->isSMTP(); + $mailer->Host = $this->options->getHostname(); + $mailer->SMTPAuth = $this->options->getAuthenticationType() !== 'none'; + $mailer->AuthType = $this->options->getAuthenticationType() !== 'none' ? $this->options->getAuthenticationType() : ''; + $mailer->Port = $this->options->getPort(); + $mailer->Username = $this->options->getUsername(); + $mailer->Password = $this->options->getPassword(); + $mailer->SMTPSecure = $this->options->getEncryptionType(); + $mailer->Debugoutput = function($str, $level) { + $this->transcript = $str; + }; + + $this->build_message($mailer); + } + + /** + * @param PHPMailer $mailer + * @throws Exception + */ + private function build_message($mailer) { + require_once dirname(__DIR__) . '/PostmanWpMail.php'; + + // create a PostmanWpMail instance + $postmanWpMail = new PostmanWpMail(); + $postmanWpMail->init(); + + $senderEmail = $this->options->getMessageSenderEmail(); + $senderName = $this->options->getMessageSenderName(); + + // create a PostmanMessage instance + $this->message = $postmanWpMail->createNewMessage(); + + $this->message->setFrom( $senderEmail, $senderName ); + $this->message->addHeaders( $mailer->getCustomHeaders() ); + $this->message->setBodyTextPart( $mailer->AltBody ); + $this->message->setBodyHtmlPart( $mailer->Body ); + $this->message->setBody( $mailer->AltBody . $mailer->Body ); + $this->message->setSubject( $mailer->Subject ); + $this->message->addTo( $this->flatArray($mailer->getToAddresses() ) ); + $this->message->addCc( $this->flatArray($mailer->getCcAddresses() ) ); + $this->message->addBCc( $this->flatArray( $mailer->getBccAddresses() ) ); + $this->message->setReplyTo( $this->flatArray( $mailer->getReplyToAddresses() ) ); + $this->message->setAttachments( $mailer->getAttachments() ); + } + + private function flatArray($arr) { + $result = []; + foreach ( $arr as $key => $value ) { + if ( is_array( $value ) ) { + foreach ($value as $k => $v ) { + $value = $v; + } + } + + $result[] = $value; + } + + return $result; + } + + /** + * @param WP_Error $error + */ + public function wp_mail_failed( $error ) { + $error_code = 0; + $error_message = $error->get_error_message(); + + $e = new Exception( $error_message, $error_code ); + $this->mail_error = [ + 'time' => null, + 'exception' => $e, + 'transcript' => $this->transcript + ]; + + $this->save_log( $error ); + $this->check_fallback( $error->get_error_data() ); + } + + private function check_fallback( $data ) { + if ( ! $this->options->is_fallback ) { + $this->options->is_fallback = true; + extract( $data ); + + wp_mail( $to, $subject, $message, $headers, $attachments ); + } else { + $this->options->is_fallback = false; + } + } + + /** + * @param WP_Error $error + */ + private function save_log( $error ) { + require_once dirname(__DIR__) . '/Postman-Email-Log/PostmanEmailLogService.php'; + + $data = $error->get_error_data(); + + // build the email log entry + $log = new PostmanEmailLog(); + $log->success = false; + $log->originalTo = $data['to']; + $log->originalSubject = $data['subject']; + $log->originalMessage = $data['message']; + $log->originalHeaders = $data['headers']; + $log->statusMessage = $error->get_error_message(); + $log->sessionTranscript = $this->transcript; + + PostmanEmailLogService::getInstance()->writeFailureLog( $log, $this->message, $this->transcript, new PostmanSmtpModuleTransport(POST_BASE), $error->get_error_message() ); + } + + public function postman_wp_mail_result() { + return $this->mail_error; + } +} +new PhpmailerInit(); \ No newline at end of file diff --git a/Postman/Phpmailer/PostsmtpMailer.php b/Postman/Phpmailer/PostsmtpMailer.php new file mode 100644 index 0000000..b41b43c --- /dev/null +++ b/Postman/Phpmailer/PostsmtpMailer.php @@ -0,0 +1,103 @@ +options = PostmanOptions::getInstance(); + add_filter( 'postman_wp_mail_result', [ $this, 'postman_wp_mail_result' ] ); + } + + public function send() + { + require_once dirname(__DIR__) . '/PostmanWpMail.php'; + + // create a PostmanWpMail instance + $postmanWpMail = new PostmanWpMail(); + $postmanWpMail->init(); + + $senderEmail = $this->options->getMessageSenderEmail(); + $senderName = $this->options->getMessageSenderName(); + + $this->addCustomHeader('X-Mailer', 'PostSMTP/' . POST_SMTP_VER ); + + // create a PostmanMessage instance + $message = $postmanWpMail->createNewMessage(); + + $message->setFrom( $senderEmail, $senderName ); + $message->addHeaders( $this->getCustomHeaders() ); + $message->setBodyTextPart( $this->AltBody ); + $message->setBodyHtmlPart( $this->Body ); + $message->setBody( $this->Body ); + $message->setSubject( $this->Subject ); + $message->addTo( $this->flatArray($this->getToAddresses() ) ); + $message->setReplyTo( $this->flatArray( $this->getReplyToAddresses() ) ); + $message->addCc( $this->flatArray($this->getCcAddresses() ) ); + $message->addBCc( $this->flatArray( $this->getBccAddresses() ) ); + $message->setReplyTo( $this->flatArray( $this->getReplyToAddresses() ) ); + $message->setAttachments( $this->getAttachments() ); + + // create a PostmanEmailLog instance + $log = new PostmanEmailLog(); + + $log->originalTo = $this->flatArray($this->getToAddresses() ); + $log->originalSubject = $this->Subject; + $log->originalMessage = $this->Body; + $log->originalHeaders = $this->getCustomHeaders(); + + try { + return $postmanWpMail->sendMessage( $message, $log ); + } catch (phpmailerException $exc) { + + $this->error = $exc; + + $this->mailHeader = ''; + $this->setError($exc->getMessage()); + if ($this->exceptions) { + throw $exc; + } + return false; + } + + } + + public function postman_wp_mail_result() { + $result = [ + 'exception' => $this->error, + 'transcript' => '', + ]; + return $result; + } + + private function flatArray($arr) { + $result = []; + foreach ( $arr as $key => $value ) { + if ( is_array( $value ) ) { + foreach ($value as $k => $v ) { + if ( empty( $v ) ) { + continue; + } + $value = $v; + } + } + + $result[] = $value; + } + + return implode(',', $result ); + } +} \ No newline at end of file -- cgit v1.2.3 From 75e5f6e319d5d36f3076fe2eb449bb089b8ce3da Mon Sep 17 00:00:00 2001 From: Yehuda Hassine Date: Thu, 2 May 2019 07:42:15 +0300 Subject: small issues --- Postman/Phpmailer/PostsmtpMailer.php | 1 + 1 file changed, 1 insertion(+) (limited to 'Postman/Phpmailer') diff --git a/Postman/Phpmailer/PostsmtpMailer.php b/Postman/Phpmailer/PostsmtpMailer.php index b41b43c..82527d2 100644 --- a/Postman/Phpmailer/PostsmtpMailer.php +++ b/Postman/Phpmailer/PostsmtpMailer.php @@ -77,6 +77,7 @@ class PostsmtpMailer extends PHPMailer { public function postman_wp_mail_result() { $result = [ + 'time' => '', 'exception' => $this->error, 'transcript' => '', ]; -- cgit v1.2.3 From 038dc3839338d5384d263f2488a6f67adf99ec7c Mon Sep 17 00:00:00 2001 From: Yehuda Hassine Date: Sun, 12 May 2019 23:19:58 +0300 Subject: Dummy PHPMailer delivery + Message Id handler --- Postman/Phpmailer/PostsmtpMailer.php | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) (limited to 'Postman/Phpmailer') diff --git a/Postman/Phpmailer/PostsmtpMailer.php b/Postman/Phpmailer/PostsmtpMailer.php index 82527d2..f3e0479 100644 --- a/Postman/Phpmailer/PostsmtpMailer.php +++ b/Postman/Phpmailer/PostsmtpMailer.php @@ -33,13 +33,11 @@ class PostsmtpMailer extends PHPMailer { $senderEmail = $this->options->getMessageSenderEmail(); $senderName = $this->options->getMessageSenderName(); - $this->addCustomHeader('X-Mailer', 'PostSMTP/' . POST_SMTP_VER ); - // create a PostmanMessage instance $message = $postmanWpMail->createNewMessage(); $message->setFrom( $senderEmail, $senderName ); - $message->addHeaders( $this->getCustomHeaders() ); + $message->addHeaders( $this->getHeaders() ); $message->setBodyTextPart( $this->AltBody ); $message->setBodyHtmlPart( $this->Body ); $message->setBody( $this->Body ); @@ -75,6 +73,15 @@ class PostsmtpMailer extends PHPMailer { } + private function getHeaders() { + $headers = array(); + foreach ( $this->getCustomHeaders() as $header ) { + $headers[] = "{$header[0]}: {$header[1]}"; + } + + return $headers; + } + public function postman_wp_mail_result() { $result = [ 'time' => '', -- cgit v1.2.3 From 71c82c669fd430f3d8a40839a10eaa231081bbea Mon Sep 17 00:00:00 2001 From: Yehuda Hassine Date: Wed, 15 May 2019 09:14:23 +0300 Subject: fix attachments fetch --- Postman/Phpmailer/PhpmailerInit.php | 169 ----------------------------------- Postman/Phpmailer/PostsmtpMailer.php | 11 +++ 2 files changed, 11 insertions(+), 169 deletions(-) delete mode 100644 Postman/Phpmailer/PhpmailerInit.php (limited to 'Postman/Phpmailer') diff --git a/Postman/Phpmailer/PhpmailerInit.php b/Postman/Phpmailer/PhpmailerInit.php deleted file mode 100644 index 60d27f2..0000000 --- a/Postman/Phpmailer/PhpmailerInit.php +++ /dev/null @@ -1,169 +0,0 @@ -set_vars(); - $this->hooks(); - } - - public function set_vars() { - $this->options = PostmanOptions::getInstance(); - } - - public function hooks() - { - add_action( 'phpmailer_init', [ $this, 'phpmailer_init'] ); - add_action( 'wp_mail_failed', [ $this, 'wp_mail_failed' ] ); - add_filter( 'postman_wp_mail_result', [ $this, 'postman_wp_mail_result'] ); - } - - /** - * @param PHPMailer $mailer - */ - public function phpmailer_init($mailer) { - - if ( $this->options->getTransportType() !== 'smtp' ) { - return $mailer; - } - - $mailer->SMTPDebug = 2; - $mailer->isSMTP(); - $mailer->Host = $this->options->getHostname(); - $mailer->SMTPAuth = $this->options->getAuthenticationType() !== 'none'; - $mailer->AuthType = $this->options->getAuthenticationType() !== 'none' ? $this->options->getAuthenticationType() : ''; - $mailer->Port = $this->options->getPort(); - $mailer->Username = $this->options->getUsername(); - $mailer->Password = $this->options->getPassword(); - $mailer->SMTPSecure = $this->options->getEncryptionType(); - $mailer->Debugoutput = function($str, $level) { - $this->transcript = $str; - }; - - $this->build_message($mailer); - } - - /** - * @param PHPMailer $mailer - * @throws Exception - */ - private function build_message($mailer) { - require_once dirname(__DIR__) . '/PostmanWpMail.php'; - - // create a PostmanWpMail instance - $postmanWpMail = new PostmanWpMail(); - $postmanWpMail->init(); - - $senderEmail = $this->options->getMessageSenderEmail(); - $senderName = $this->options->getMessageSenderName(); - - // create a PostmanMessage instance - $this->message = $postmanWpMail->createNewMessage(); - - $this->message->setFrom( $senderEmail, $senderName ); - $this->message->addHeaders( $mailer->getCustomHeaders() ); - $this->message->setBodyTextPart( $mailer->AltBody ); - $this->message->setBodyHtmlPart( $mailer->Body ); - $this->message->setBody( $mailer->AltBody . $mailer->Body ); - $this->message->setSubject( $mailer->Subject ); - $this->message->addTo( $this->flatArray($mailer->getToAddresses() ) ); - $this->message->addCc( $this->flatArray($mailer->getCcAddresses() ) ); - $this->message->addBCc( $this->flatArray( $mailer->getBccAddresses() ) ); - $this->message->setReplyTo( $this->flatArray( $mailer->getReplyToAddresses() ) ); - $this->message->setAttachments( $mailer->getAttachments() ); - } - - private function flatArray($arr) { - $result = []; - foreach ( $arr as $key => $value ) { - if ( is_array( $value ) ) { - foreach ($value as $k => $v ) { - $value = $v; - } - } - - $result[] = $value; - } - - return $result; - } - - /** - * @param WP_Error $error - */ - public function wp_mail_failed( $error ) { - $error_code = 0; - $error_message = $error->get_error_message(); - - $e = new Exception( $error_message, $error_code ); - $this->mail_error = [ - 'time' => null, - 'exception' => $e, - 'transcript' => $this->transcript - ]; - - $this->save_log( $error ); - $this->check_fallback( $error->get_error_data() ); - } - - private function check_fallback( $data ) { - if ( ! $this->options->is_fallback ) { - $this->options->is_fallback = true; - extract( $data ); - - wp_mail( $to, $subject, $message, $headers, $attachments ); - } else { - $this->options->is_fallback = false; - } - } - - /** - * @param WP_Error $error - */ - private function save_log( $error ) { - require_once dirname(__DIR__) . '/Postman-Email-Log/PostmanEmailLogService.php'; - - $data = $error->get_error_data(); - - // build the email log entry - $log = new PostmanEmailLog(); - $log->success = false; - $log->originalTo = $data['to']; - $log->originalSubject = $data['subject']; - $log->originalMessage = $data['message']; - $log->originalHeaders = $data['headers']; - $log->statusMessage = $error->get_error_message(); - $log->sessionTranscript = $this->transcript; - - PostmanEmailLogService::getInstance()->writeFailureLog( $log, $this->message, $this->transcript, new PostmanSmtpModuleTransport(POST_BASE), $error->get_error_message() ); - } - - public function postman_wp_mail_result() { - return $this->mail_error; - } -} -new PhpmailerInit(); \ No newline at end of file diff --git a/Postman/Phpmailer/PostsmtpMailer.php b/Postman/Phpmailer/PostsmtpMailer.php index f3e0479..9eee0a7 100644 --- a/Postman/Phpmailer/PostsmtpMailer.php +++ b/Postman/Phpmailer/PostsmtpMailer.php @@ -73,6 +73,17 @@ class PostsmtpMailer extends PHPMailer { } + public function getAttachments() { + $attachments = parent::getAttachments(); + + $data = array(); + foreach ( $attachments as $attachment ) { + $data[] = $attachment[0]; + } + + return $data; + } + private function getHeaders() { $headers = array(); foreach ( $this->getCustomHeaders() as $header ) { -- cgit v1.2.3