| 1 |
<?php |
| 2 |
|
| 3 |
class HappyForms_Email_Message { |
| 4 |
|
| 5 |
/** |
| 6 |
* The sender address. |
| 7 |
* |
| 8 |
* @since 1.4.3 |
| 9 |
* |
| 10 |
* @var string |
| 11 |
*/ |
| 12 |
private $from = array(); |
| 13 |
|
| 14 |
/** |
| 15 |
* The sender name. |
| 16 |
* |
| 17 |
* @since 1.4.3 |
| 18 |
* |
| 19 |
* @var string |
| 20 |
*/ |
| 21 |
private $from_name; |
| 22 |
|
| 23 |
/** |
| 24 |
* The reply-to address. |
| 25 |
* |
| 26 |
* @since 1.4.3 |
| 27 |
* |
| 28 |
* @var string |
| 29 |
*/ |
| 30 |
private $reply_to = array(); |
| 31 |
|
| 32 |
/** |
| 33 |
* The list of recipients. |
| 34 |
* |
| 35 |
* @since 1.4.3 |
| 36 |
* |
| 37 |
* @var array |
| 38 |
*/ |
| 39 |
private $to = array(); |
| 40 |
|
| 41 |
/** |
| 42 |
* The list of cc addresses. |
| 43 |
* |
| 44 |
* @since 1.4.3 |
| 45 |
* |
| 46 |
* @var array |
| 47 |
*/ |
| 48 |
private $ccs = array(); |
| 49 |
|
| 50 |
/** |
| 51 |
* The list of bcc addresses. |
| 52 |
* |
| 53 |
* @since 1.4.3 |
| 54 |
* |
| 55 |
* @var array |
| 56 |
*/ |
| 57 |
private $bccs = array(); |
| 58 |
|
| 59 |
/** |
| 60 |
* The email subject. |
| 61 |
* |
| 62 |
* @since 1.4.3 |
| 63 |
* |
| 64 |
* @var string |
| 65 |
*/ |
| 66 |
private $subject; |
| 67 |
|
| 68 |
/** |
| 69 |
* The email content type. |
| 70 |
* |
| 71 |
* @since 1.4.3 |
| 72 |
* |
| 73 |
* @var string |
| 74 |
*/ |
| 75 |
private $content_type = 'text/html'; |
| 76 |
|
| 77 |
/** |
| 78 |
* The email message content. |
| 79 |
* |
| 80 |
* @since 1.4.3 |
| 81 |
* |
| 82 |
* @var string |
| 83 |
*/ |
| 84 |
private $content; |
| 85 |
|
| 86 |
private $attachments = array(); |
| 87 |
|
| 88 |
/** |
| 89 |
* The submission message this email is linked to. |
| 90 |
* |
| 91 |
* @since 1.4.3 |
| 92 |
* |
| 93 |
* @var array |
| 94 |
*/ |
| 95 |
public $message; |
| 96 |
|
| 97 |
public function __construct( $message = array() ) { |
| 98 |
$this->from = array(); |
| 99 |
$this->to = array(); |
| 100 |
$this->reply_to = array(); |
| 101 |
$this->ccs = array(); |
| 102 |
$this->bccs = array(); |
| 103 |
$this->subject = ''; |
| 104 |
$this->content = ''; |
| 105 |
$this->message = $message; |
| 106 |
} |
| 107 |
|
| 108 |
public function set_from( $email, $name = '' ) { |
| 109 |
$from = apply_filters( 'happyforms_email_from', $email, $this->message ); |
| 110 |
$from = is_array( $from ) ? $from : array( $from ); |
| 111 |
$from = array_values( array_filter( array_map( 'trim', $from ) ) ); |
| 112 |
|
| 113 |
$this->from = $from; |
| 114 |
} |
| 115 |
|
| 116 |
public function get_from() { |
| 117 |
$from = array_map( array( happyforms_get_email_encoder(), 'encode_email' ), $this->from ); |
| 118 |
$from = implode( ', ', $from ); |
| 119 |
|
| 120 |
return $from; |
| 121 |
} |
| 122 |
|
| 123 |
public function set_from_name( $name ) { |
| 124 |
$from_name = apply_filters( 'happyforms_email_from_name', $name, $this->message ); |
| 125 |
|
| 126 |
$this->from_name = $from_name; |
| 127 |
} |
| 128 |
|
| 129 |
public function get_from_name() { |
| 130 |
return $this->from_name; |
| 131 |
} |
| 132 |
|
| 133 |
public function set_to( $to ) { |
| 134 |
/** |
| 135 |
* Filter the list of recipients for this email message. |
| 136 |
* |
| 137 |
* @since 1.4.3 |
| 138 |
* |
| 139 |
* @param array $recipients Current list of recipients. |
| 140 |
* @param array $message The submission this email was triggered from. |
| 141 |
* |
| 142 |
* @return array |
| 143 |
*/ |
| 144 |
$to = apply_filters( 'happyforms_email_to', $to, $this->message ); |
| 145 |
$to = is_array( $to ) ? $to : array( $to ); |
| 146 |
$to = array_values( array_filter( array_map( 'trim', $to ) ) ); |
| 147 |
|
| 148 |
$this->to = $to; |
| 149 |
} |
| 150 |
|
| 151 |
public function get_to() { |
| 152 |
$to = array_map( array( happyforms_get_email_encoder(), 'encode_email' ), $this->to ); |
| 153 |
$to = implode( ', ', $to ); |
| 154 |
|
| 155 |
return $to; |
| 156 |
} |
| 157 |
|
| 158 |
public function set_ccs( $ccs = array() ) { |
| 159 |
/** |
| 160 |
* Filter the list of recipients for this email message. |
| 161 |
* |
| 162 |
* @since 1.4.3 |
| 163 |
* |
| 164 |
* @param array $recipients Current list of recipients. |
| 165 |
* @param array $message The submission this email was triggered from. |
| 166 |
* |
| 167 |
* @return array |
| 168 |
*/ |
| 169 |
$ccs = apply_filters( 'happyforms_email_ccs', $ccs, $this->message ); |
| 170 |
$ccs = is_array( $ccs ) ? $ccs : array( $ccs ); |
| 171 |
$ccs = array_values( array_filter( array_map( 'trim', $ccs ) ) ); |
| 172 |
|
| 173 |
$this->ccs = $ccs; |
| 174 |
} |
| 175 |
|
| 176 |
public function get_ccs() { |
| 177 |
$ccs = array_map( array( happyforms_get_email_encoder(), 'encode_email' ), $this->ccs ); |
| 178 |
$ccs = implode( ', ', $ccs ); |
| 179 |
|
| 180 |
return $ccs; |
| 181 |
} |
| 182 |
|
| 183 |
public function set_bccs( $bccs = array() ) { |
| 184 |
/** |
| 185 |
* Filter the list of recipients for this email message. |
| 186 |
* |
| 187 |
* @since 1.4.3 |
| 188 |
* |
| 189 |
* @param array $recipients Current list of recipients. |
| 190 |
* @param array $message The submission this email was triggered from. |
| 191 |
* |
| 192 |
* @return array |
| 193 |
*/ |
| 194 |
$bccs = apply_filters( 'happyforms_email_bccs', $bccs, $this->message ); |
| 195 |
$bccs = is_array( $bccs ) ? $bccs : array( $bccs ); |
| 196 |
$bccs = array_values( array_filter( array_map( 'trim', $bccs ) ) ); |
| 197 |
|
| 198 |
$this->bccs = $bccs; |
| 199 |
} |
| 200 |
|
| 201 |
public function get_bccs() { |
| 202 |
$bccs = array_map( array( happyforms_get_email_encoder(), 'encode_email' ), $this->bccs ); |
| 203 |
$bccs = implode( ', ', $bccs ); |
| 204 |
|
| 205 |
return $bccs; |
| 206 |
} |
| 207 |
|
| 208 |
public function set_reply_to( $reply_to = array() ) { |
| 209 |
/** |
| 210 |
* Filter the list of recipients for this email message. |
| 211 |
* |
| 212 |
* @since 1.4.3 |
| 213 |
* |
| 214 |
* @param array $recipients Current list of recipients. |
| 215 |
* @param array $message The submission this email was triggered from. |
| 216 |
* |
| 217 |
* @return array |
| 218 |
*/ |
| 219 |
$reply_to = apply_filters( 'happyforms_email_reply_to', $reply_to, $this->message ); |
| 220 |
$reply_to = is_array( $reply_to ) ? $reply_to : array( $reply_to ); |
| 221 |
$reply_to = array_values( array_filter( array_map( 'trim', $reply_to ) ) ); |
| 222 |
|
| 223 |
$this->reply_to = $reply_to; |
| 224 |
} |
| 225 |
|
| 226 |
public function get_reply_to() { |
| 227 |
$reply_to = array_map( array( happyforms_get_email_encoder(), 'encode_email' ), $this->reply_to ); |
| 228 |
$reply_to = implode( ', ', $reply_to ); |
| 229 |
|
| 230 |
return $reply_to; |
| 231 |
} |
| 232 |
|
| 233 |
public function set_subject( $subject = '' ) { |
| 234 |
$subject = trim( $subject ); |
| 235 |
|
| 236 |
/** |
| 237 |
* Filter the subject for this email message. |
| 238 |
* |
| 239 |
* @since 1.4.3 |
| 240 |
* |
| 241 |
* @param string $subject Current subject. |
| 242 |
* @param array $message The submission this email was triggered from. |
| 243 |
* @param array $recipients The address this email is being sent to. |
| 244 |
* |
| 245 |
* @return string |
| 246 |
*/ |
| 247 |
$subject = apply_filters( 'happyforms_email_subject', $subject, $this->message, $this->to ); |
| 248 |
|
| 249 |
$this->subject = $subject; |
| 250 |
} |
| 251 |
|
| 252 |
public function get_subject() { |
| 253 |
return $this->subject; |
| 254 |
} |
| 255 |
|
| 256 |
public function set_content( $content = '' ) { |
| 257 |
$content = trim( $content ); |
| 258 |
|
| 259 |
/** |
| 260 |
* Filter the content for this email message. |
| 261 |
* |
| 262 |
* @since 1.4.3 |
| 263 |
* |
| 264 |
* @param string $content Current content. |
| 265 |
* @param array $message The submission this email was triggered from. |
| 266 |
* @param array $recipients The address this email is being sent to. |
| 267 |
* |
| 268 |
* @return string |
| 269 |
*/ |
| 270 |
$content = apply_filters( 'happyforms_email_content', $content, $this->message, $this->to ); |
| 271 |
|
| 272 |
$this->content = $content; |
| 273 |
} |
| 274 |
|
| 275 |
public function get_content() { |
| 276 |
return $this->content; |
| 277 |
} |
| 278 |
|
| 279 |
public function get_response() { |
| 280 |
return $this->message; |
| 281 |
} |
| 282 |
|
| 283 |
public function get_headers() { |
| 284 |
$headers = array(); |
| 285 |
|
| 286 |
$reply_to = $this->get_reply_to(); |
| 287 |
|
| 288 |
if ( ! empty( $reply_to ) ) { |
| 289 |
array_push( $headers, 'Reply-To: ' . $reply_to ); |
| 290 |
} |
| 291 |
|
| 292 |
$ccs = $this->get_ccs(); |
| 293 |
|
| 294 |
if ( ! empty( $ccs ) ) { |
| 295 |
array_push( $headers, 'Cc: ' . $ccs ); |
| 296 |
} |
| 297 |
|
| 298 |
$bccs = $this->get_bccs(); |
| 299 |
|
| 300 |
if ( ! empty( $bccs ) ) { |
| 301 |
array_push( $headers, 'Bcc: ' . $bccs ); |
| 302 |
} |
| 303 |
|
| 304 |
return $headers; |
| 305 |
} |
| 306 |
|
| 307 |
public function add_attachment( $path ) { |
| 308 |
$this->attachments[] = $path; |
| 309 |
} |
| 310 |
|
| 311 |
public function get_attachments() { |
| 312 |
return $this->attachments; |
| 313 |
} |
| 314 |
|
| 315 |
public function get_content_type() { |
| 316 |
/** |
| 317 |
* Filter the content type for this email message. |
| 318 |
* |
| 319 |
* @since 1.4.3 |
| 320 |
* |
| 321 |
* @param string $content_type Current content type. |
| 322 |
* @param array $message The submission this email was triggered from. |
| 323 |
* |
| 324 |
* @return string |
| 325 |
*/ |
| 326 |
$content_type = apply_filters( 'happyforms_email_content_type', $this->content_type, $this->message ); |
| 327 |
|
| 328 |
return $content_type; |
| 329 |
} |
| 330 |
|
| 331 |
public function send() { |
| 332 |
add_filter( 'wp_mail_content_type', array( $this, 'get_content_type' ) ); |
| 333 |
|
| 334 |
$from = $this->get_from(); |
| 335 |
|
| 336 |
if ( $from ) { |
| 337 |
add_filter( 'wp_mail_from', array( $this, 'get_from' ) ); |
| 338 |
} |
| 339 |
|
| 340 |
if ( $this->from_name ) { |
| 341 |
add_filter( 'wp_mail_from_name', array( $this, 'get_from_name' ) ); |
| 342 |
} |
| 343 |
|
| 344 |
$result = wp_mail( $this->get_to(), $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments() ); |
| 345 |
|
| 346 |
remove_filter( 'wp_mail_content_type', array( $this, 'get_content_type' ) ); |
| 347 |
|
| 348 |
if ( $from ) { |
| 349 |
remove_filter( 'wp_mail_from', array( $this, 'get_from' ) ); |
| 350 |
} |
| 351 |
|
| 352 |
if ( $this->from_name ) { |
| 353 |
remove_filter( 'wp_mail_from_name', array( $this, 'get_from_name' ) ); |
| 354 |
} |
| 355 |
|
| 356 |
return $result; |
| 357 |
} |
| 358 |
|
| 359 |
} |
| 360 |
|