diff options
Diffstat (limited to 'Postman/Phpmailer')
-rw-r--r-- | Postman/Phpmailer/PhpmailerInit.php | 169 | ||||
-rw-r--r-- | Postman/Phpmailer/PostsmtpMailer.php | 103 |
2 files changed, 272 insertions, 0 deletions
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 @@ +<?php + +class PhpmailerInit { + + /** + * @var array + */ + private $mail_error; + + /** + * @var + */ + private $transcript; + + /** + * @var PostmanMessage + */ + private $message; + + /** + * @var PostmanOptions + */ + private $options; + + /** + * PhpmailerInit constructor. + */ + public function __construct() + { + $this->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 @@ +<?php +require_once ABSPATH . WPINC . '/class-phpmailer.php'; +require_once ABSPATH . WPINC . '/class-smtp.php'; + +add_action('plugins_loaded', function() { + global $phpmailer; + + $phpmailer = new PostsmtpMailer(true); +}); + +class PostsmtpMailer extends PHPMailer { + + private $options; + + private $error; + + public function __construct($exceptions = null) + { + parent::__construct($exceptions); + + $this->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 |