| 1 |
<?php if (! defined('ABSPATH')) exit; // Exit if accessed directly |
| 2 |
class HC3_Email |
| 3 |
{ |
| 4 |
protected $emailHtml = 1; |
| 5 |
protected $emailFrom; |
| 6 |
protected $emailFromName; |
| 7 |
|
| 8 |
public function __construct( |
| 9 |
HC3_Settings $settings |
| 10 |
) |
| 11 |
{ |
| 12 |
$this->emailHtml = $settings->get( 'email_html' ); |
| 13 |
$this->emailFrom = $settings->get( 'email_from' ); |
| 14 |
$this->emailFromName = $settings->get( 'email_fromname' ); |
| 15 |
} |
| 16 |
|
| 17 |
public function send( $to, $subj, $msg ) |
| 18 |
{ |
| 19 |
if( $this->emailHtml ){ |
| 20 |
$msg = nl2br( $msg ); |
| 21 |
} |
| 22 |
else { |
| 23 |
$msg = strip_tags( $msg ); |
| 24 |
} |
| 25 |
|
| 26 |
add_filter( 'wp_mail_content_type', array($this, 'set_html_mail_content_type') ); |
| 27 |
add_filter( 'wp_mail_charset', array($this, 'set_charset') ); |
| 28 |
|
| 29 |
$headers = array(); |
| 30 |
if( strlen($this->emailFrom) ){ |
| 31 |
$headers[] = 'From: ' . $this->emailFromName . ' <' . $this->emailFrom . '>'; |
| 32 |
} |
| 33 |
|
| 34 |
@wp_mail( $to, $subj, $msg, $headers ); |
| 35 |
|
| 36 |
remove_filter( 'wp_mail_content_type', array($this, 'set_html_mail_content_type') ); |
| 37 |
remove_filter( 'wp_mail_charset', array($this, 'set_charset') ); |
| 38 |
|
| 39 |
return $this; |
| 40 |
} |
| 41 |
|
| 42 |
public function set_html_mail_content_type() |
| 43 |
{ |
| 44 |
$return = $this->emailHtml ? 'text/html' : 'text/plain'; |
| 45 |
return $return; |
| 46 |
} |
| 47 |
|
| 48 |
public function set_charset( $charset ) |
| 49 |
{ |
| 50 |
$return = 'utf-8'; |
| 51 |
return $return; |
| 52 |
} |
| 53 |
} |