summaryrefslogtreecommitdiff
path: root/Postman/Phpmailer/PhpmailerInit.php
blob: 60d27f26295c0752a4352c6ba4a289929ca44a11 (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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
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();