Mailer.php 4.4 KB
Newer Older
Henri Queneau's avatar
Henri Queneau committed
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
<?php

include_once "tools/services/PhpMailer.php";

function object2array($object)
{
    $return = NULL;
    if (is_array($object)) {
        foreach ($object as $key => $value) {
            $return[$key] = object2array($value);
        }
    } else {
        if (is_object($object)) {
            $var = get_object_vars($object);
            if ($var) {
                foreach ($var as $key => $value) {
                    $return[$key] = ($key && !$value) ? NULL : object2array($value);
                }
            } else {
                return $object;
            }
        } else {
            return $object;
        }
    }
    return $return;
}

// Load config
$mailParam = simplexml_load_file('config/Mailer.xml');
$mailParam = object2array($mailParam);

$aArgsClass = [];

$aArgs['pathToLib'] = 'tools/PHPMailer/PHPMailerAutoload.php';

$theParam = $mailParam;
$theParam['mailer_class'] = 'Sendmail_PhpMailer_Service';

$mailer = new $theParam['mailer_class']($aArgsClass);

//smtp params
$aArgsSmtpParams = [];
$aArgsSmtpParams['smtp_host']     = $theParam['smtp_host'];
$aArgsSmtpParams['smtp_port']     = $theParam['smtp_port'];
$aArgsSmtpParams['domain']        = $theParam['domain'];
$aArgsSmtpParams['smtp_auth']     = (boolean) $theParam['smtp_auth'];
$aArgsSmtpParams['smtp_user']     = $theParam['smtp_user'];
$aArgsSmtpParams['smtp_password'] = $theParam['smtp_password'];
$aArgsSmtpParams['smtp_secure']   = $theParam['smtp_secure'];

foreach ($theParam['ssl_options'] as $key => $value) {
    if ($value == 'false') {
        $theParam['ssl_options'][$key] = false;
    } elseif ($value == 'true') {
        $theParam['ssl_options'][$key] = true;
    }
}

//var_dump($theParam['ssl_options']);exit;

$mailer->setSMTPParams($aArgsSmtpParams);
if (isset($theParam['ssl_options'])) {
    //smtp options for ssl
    $mailer->setSSLOptions($theParam['ssl_options']);
}

$aArgsMailerType['mailerType'] = $theParam['type'];
$mailer->setMailerType($aArgsMailerType);

//smtp params
$aArgsSignedEmailsParams = [];

if (
    (boolean) $theParam['signed_email']['enabled'] &&
    $theParam['signed_email']['enabled'] <> 'false'
) {
    $aArgsSignedEmailsParams['enabled'] = true;
    $aArgsSignedEmailsParams['certificate_path'] = $theParam['signed_email']['certificate_path'];
    $aArgsSignedEmailsParams['private_key_path'] = $theParam['signed_email']['private_key_path'];
    $aArgsSignedEmailsParams['secret_private_key_password'] = $theParam['signed_email']['secret_private_key_password'];
    $aArgsSignedEmailsParams['cert_chain_path'] = $theParam['signed_email']['cert_chain_path'];
} else {
    $aArgsSignedEmailsParams['enabled']= false;
}

$aArgsFrom['from'] = $theParam['mailfrom'];
//setFrom
$mailer->setFrom($aArgsFrom);
$body = 'Error : <br/><br/>' . $message 
    . '<br/><br/>See the batch directory errors for more details : <br/><br/>' 
    . $batchInfos->directory . ' <br/>';

//setReplyTo
$aArgsReplyTo['replyTo'] = $theParam['mailto'];
$mailer->setReplyTo($aArgsReplyTo);

//--> Set the return path
$aArgsReturnPath['returnPath'] = $userInfo['mail'];
$mailer->setReturnPath($aArgsReturnPath);

//--> To
$to = [
    'mail' => $theParam['mailto']
];

//--> Set subject
$aArgsSubj['subject'] = 'Error with MaarchCapture';
$mailer->setSubject($aArgsSubj);

$body = '<html><body>' . $body . '</body></html>';
$aArgsBody['body'] = [
    [
        'htmlContent' => $body,
        'textContent' => '',
        'isHTML' => true,
    ],
];

//setBody
$mailer->setBody($aArgsBody);

$aArgsCharset['charset'] = [
    [
        'htmlCharset' => $theParam['charset'], 
        'textCharset' => $theParam['charset'],
        'headCharset' => $theParam['charset'],
    ],
];
//--> Set charset
$mailer->setCharset($aArgsCharset);

// $aArgsAttach['attachment'] = [
//     [
//         'path' => $resFile['file_path'], 
//         'fileName' => $resFilename,
//         'mimeType' => $resFile['mime_type'],
//     ],
// ];

// //Add file
// $mailer->addAttachment($aArgsAttach); 

$mailer->setTo([$to]);

if ($aArgsSignedEmailsParams['enabled']) {
    $mailer->sign(
        $aArgsSignedEmailsParams
    );
}
//$mailer->MailObj->Mailer = "sendmail";
//$mailer->MailObj->Sendmail = "ssmtp";

$return = $mailer->send();

if (
    ($return == 1 && 
    ($theParam['type'] == "smtp" || $theParam['type'] == "mail" )) || 
    ($return == 0 && $theParam['type'] == "sendmail") || empty($to)
) {
        $execResult = 'S';
} else {
    $error = $mailer->getErrors();
    $execResult = 'E';
}