blob: f3e0479b383d0fc6008f1f842f66e5b12086f3eb (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
|
<?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;
}
}
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 );
}
}
|