-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathnuemailer.php
148 lines (144 loc) · 6.03 KB
/
nuemailer.php
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
<?php
function nuEmail($to_list=array(),$from_address='',$from_name='',$content='',$subject='',$file_list=array(),$html=false,$cc_list=array(), $bcc_list=array(), $reply_to_list=array(),$debug=0,$method='SMTP'){ // changed
ob_start();
$nuEmailSettings = nuMarshallEmailSettings($from_address, $from_name, $html, $reply_to_list);
require_once($nuEmailSettings->phpmailer_path);
$mail = new PHPMailer();
$mail->SMTPDebug = $debug;
if ( $method == 'SMTP' ) {
$mail->isSMTP();
}
if ( $method == 'sendmail' ) {
$mail->isSendmail();
}
$mail->Subject = $subject;
$mail->Body = $content;
$mail->Username = $nuEmailSettings->username; // defaults to ''
$mail->Password = $nuEmailSettings->password; // defaults to ''
$mail->Host = $nuEmailSettings->host; // defaults to 127.0.0.1
$mail->From = $nuEmailSettings->from_address; // defaults to ''
$mail->FromName = $nuEmailSettings->from_name; // defaults to ''
$mail->Port = $nuEmailSettings->smtp_port; // defaults to 25
$mail->SMTPSecure = $nuEmailSettings->smtp_secure; // defaults to '' otherwsie it will be tls or ssl
$mail->SMTPAuth = $nuEmailSettings->smtp_use_authentication; // defaults to false
$mail->WordWrap = $nuEmailSettings->word_wrap; // defaults to 120
$mail->CharSet = $nuEmailSettings->charset; // defaults to UTF-8
$mail->IsHTML($nuEmailSettings->html);
_nuEmailHelperAdd($mail, $to_list, 'AddAddress');
_nuEmailHelperAdd($mail, $nuEmailSettings->reply_to_list, 'AddReplyTo');
_nuEmailHelperAttach($mail, $file_list);
$result = array();
try {
$result[0] = $mail->Send();
$result[1] = "Message sent successfully";
$result[2] = $mail->ErrorInfo;
} catch(Exception $e) {
$result[0] = false;
$result[1] = $e->errorMessage();
$result[2] = $mail->ErrorInfo;
}
_nuEmailHelperClean($file_list);
$result[3] = ob_get_contents();
ob_end_clean();
return $result;
}
function _nuMarshallEmailSettingsHelper($obj, $key, $default = '') {
if ( !is_object($obj) ) {
return $default;
}
if ( !isset($obj->$key) ) {
return $default;
}
if ( empty(trim($obj->$key)) ) {
return $default;
}
return trim($obj->$key);
}
function nuMarshallEmailSettings( $from_address = '', $from_name = '', $html = false, $reply_to_list = array() ) {
$word_press_path = '../../../wp-includes/class-phpmailer.php';
$standalone_path = 'phpmailer/nuemailer/class-phpmailer.php';
$setup = db_setup();
$nuEmailSettings = new stdClass();
$nuEmailSettings->error_text = '';
$nuEmailSettings->username = _nuMarshallEmailSettingsHelper($setup, 'set_smtp_username');
$nuEmailSettings->password = _nuMarshallEmailSettingsHelper($setup, 'set_smtp_password');
$nuEmailSettings->host = _nuMarshallEmailSettingsHelper($setup, 'set_smtp_host', '127.0.0.1');
$nuEmailSettings->smtp_port = _nuMarshallEmailSettingsHelper($setup, 'set_smtp_port', '25');
$nuEmailSettings->smtp_use_secure = _nuMarshallEmailSettingsHelper($setup, 'set_smtp_use_ssl', false);
$nuEmailSettings->smtp_use_authentication = _nuMarshallEmailSettingsHelper($setup, 'set_smtp_use_authentication', false);
$nuEmailSettings->from_address = $from_address != '' ? $from_address : _nuMarshallEmailSettingsHelper($setup, 'set_smtp_from_address');
$nuEmailSettings->from_name = $from_name != '' ? $from_name : _nuMarshallEmailSettingsHelper($setup, 'set_smtp_from_name');
$nuEmailSettings->word_wrap = 120;
$nuEmailSettings->charset = 'UTF-8';
$nuEmailSettings->html = $html;
$nuEmailSettings->phpmailer_path = $standalone_path;
$nuEmailSettings->reply_to_list = array();
$nuEmailSettings->smtp_secure = '';
if ( $nuEmailSettings->smtp_port == '587' ) {
$nuEmailSettings->smtp_secure = 'tls';
$nuEmailSettings->verbose_host = 'tls://'.$nuEmailSettings->host.':587';
}
if ( $nuEmailSettings->smtp_port == '465' ) {
$nuEmailSettings->smtp_secure = 'ssl';
$nuEmailSettings->verbose_host = 'ssl://'.$nuEmailSettings->host.':465';
}
if ( empty($reply_to_list) ) {
if ( $from_address != '' ) {
$nuEmailSettings->reply_to_list = array($from_address => $from_name);
}
}
if ( isset($_SESSION) ) {
if ( $_SESSION['nubuilder_session_data']['PLUGIN'] ) {
if ( file_exists($word_press_path) ) {
$nuEmailSettings->phpmailer_path = $word_press_path;
} else {
$nuEmailSettings->error_text .= 'Cannot locate phpmailer files';
}
}
}
var_dump($nuEmailSettings);
return $nuEmailSettings;
}
function _nuEmailHelperAdd(&$mail, $names_addresses, $method) {
if (_is_my_array_associative($names_addresses)) {
_nuEmailHelperAddAssociative($mail, $names_addresses, $method);
} else {
_nuEmailHelperAddOrdinal($mail, $names_addresses, $method);
}
}
function _nuEmailHelperAddOrdinal(&$mail, $names_addresses, $method) {
for ( $x=0; $x<count($names_addresses); $x++ ) {
$mail->$method($names_addresses[$x], '');
}
}
function _nuEmailHelperAddAssociative(&$mail, $names_addresses, $method) {
foreach ( $names_addresses as $key => $value ) {
$mail->$method($key, $value);
}
}
function _nuEmailHelperAttach(&$mail, $file_list = array() ) {
if ( !is_array($file_list) ) {
return;
}
if ( !_is_my_array_associative($file_list) ) {
return;
}
foreach($file_list as $filename=>$filesource) {
$mail->AddAttachment($filesource,$filename);
}
}
function _nuEmailHelperClean($file_list = array() ) {
if ( !is_array($file_list) ) {
return;
}
if ( !_is_my_array_associative($file_list) ) {
return;
}
foreach($file_list as $filename=>$filesource) {
@unlink($filesource);
}
}
function _is_my_array_associative($arr) {
return array_key_exists('0',$arr) ? false : true;
}
?>