-
Notifications
You must be signed in to change notification settings - Fork 5
/
Mailer.php
142 lines (118 loc) · 2.41 KB
/
Mailer.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
<?php
/**
* @package snow-monkey-forms
* @author inc2734
* @license GPL-2.0+
*/
namespace Snow_Monkey\Plugin\Forms\App\Model;
use Snow_Monkey\Plugin\Forms\App\Validation;
class Mailer {
/**
* @var string
*/
protected $to = '';
/**
* @var string
*/
protected $subject = '';
/**
* @var string
*/
protected $body = '';
/**
* @var string
*/
protected $replyto = '';
/**
* @var string
*/
protected $from = '';
/**
* @var string
*/
protected $sender = '';
/**
* @var array Array of filepath.
*/
protected $attachments = array();
/**
* @var array
*/
protected $headers = array();
/**
* Constructor.
*
* @param array $args Array of properties.
*/
public function __construct( $args ) {
$properties = array_keys( get_object_vars( $this ) );
foreach ( $args as $key => $value ) {
if ( ! in_array( $key, $properties, true ) ) {
continue;
}
if ( is_array( $this->$key ) && is_array( $value ) ) {
$this->$key = $value;
} elseif ( ! is_array( $this->$key ) && ! is_array( $value ) ) {
$this->$key = $value;
}
}
}
/**
* Set from.
*
* @param string $from_email From.
* @return string
*/
public function _wp_mail_from( $from_email ) {
if ( empty( $this->from ) ) {
return $from_email;
}
$is_valid = Validation\Email::validate( $this->from );
if ( ! $is_valid ) {
return $from_email;
}
return $this->from;
}
/**
* Set from name.
*
* @param string $from_name From name.
* @return string
*/
public function _wp_mail_from_name( $from_name ) {
return empty( $this->sender ) ? $from_name : $this->sender;
}
/**
* Get headers for wp_mail.
*
* @return array
*/
public function _get_headers() {
if ( $this->replyto ) {
$this->headers[] = 'Reply-To: ' . $this->replyto;
}
return apply_filters( 'snow_monkey_forms/mailer/headers', $this->headers );
}
/**
* Send e-mail.
*
* @return boolean
*/
public function send() {
if ( ! $this->to ) {
return false;
}
add_filter( 'wp_mail_from', array( $this, '_wp_mail_from' ) );
add_filter( 'wp_mail_from_name', array( $this, '_wp_mail_from_name' ) );
$is_sended = wp_mail(
$this->to,
$this->subject,
$this->body,
$this->_get_headers(),
$this->attachments
);
remove_filter( 'wp_mail_from', array( $this, '_wp_mail_from' ) );
remove_filter( 'wp_mail_from_name', array( $this, '_wp_mail_from_name' ) );
return $is_sended;
}
}