diff options
author | yehudah <yehudah@b8457f37-d9ea-0310-8a92-e5e31aec5664> | 2019-05-15 12:32:33 +0000 |
---|---|---|
committer | yehudah <yehudah@b8457f37-d9ea-0310-8a92-e5e31aec5664> | 2019-05-15 12:32:33 +0000 |
commit | aa1735487a16b4003af048653e413dee19d79783 (patch) | |
tree | d2cc4a3680ea451b3eab2f2ac3c511d97e5a61ee /Postman/Phpmailer/PostsmtpMailer.php | |
parent | 38153c0f3e739f3d89a1a7734fd7d199bf457fab (diff) | |
download | Post-SMTP-aa1735487a16b4003af048653e413dee19d79783.zip |
new files
Diffstat (limited to 'Postman/Phpmailer/PostsmtpMailer.php')
-rw-r--r-- | Postman/Phpmailer/PostsmtpMailer.php | 122 |
1 files changed, 122 insertions, 0 deletions
diff --git a/Postman/Phpmailer/PostsmtpMailer.php b/Postman/Phpmailer/PostsmtpMailer.php new file mode 100644 index 0000000..9eee0a7 --- /dev/null +++ b/Postman/Phpmailer/PostsmtpMailer.php @@ -0,0 +1,122 @@ +<?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(); + + // create a PostmanMessage instance + $message = $postmanWpMail->createNewMessage(); + + $message->setFrom( $senderEmail, $senderName ); + $message->addHeaders( $this->getHeaders() ); + $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 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 ) { + $headers[] = "{$header[0]}: {$header[1]}"; + } + + return $headers; + } + + public function postman_wp_mail_result() { + $result = [ + 'time' => '', + '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 |