| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Emailer Class |
| 5 |
*/ |
| 6 |
class WeForms_Emailer { |
| 7 |
|
| 8 |
/** |
| 9 |
* The default gateway to send |
| 10 |
* |
| 11 |
* @var null|WeForms_Mailer_Contract |
| 12 |
*/ |
| 13 |
private $gateway = null; |
| 14 |
|
| 15 |
function __construct() { |
| 16 |
|
| 17 |
require_once WEFORMS_INCLUDES . '/email/gateways/interface-mailer.php'; |
| 18 |
|
| 19 |
$this->gateway = $this->get_sending_gateway(); |
| 20 |
} |
| 21 |
|
| 22 |
/** |
| 23 |
* Get available gateways |
| 24 |
* |
| 25 |
* @return array |
| 26 |
*/ |
| 27 |
public function get_available_gateways() { |
| 28 |
require_once WEFORMS_INCLUDES . '/email/gateways/class-emailer-wpmail.php'; |
| 29 |
|
| 30 |
$gateways = apply_filters( 'weforms_email_gateways', array( |
| 31 |
'wordpress' => new WeForms_Emailer_WPMail() |
| 32 |
) ); |
| 33 |
|
| 34 |
return $gateways; |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Get the sending gateway class |
| 39 |
* |
| 40 |
* @return WeForms_Mailer_Contract |
| 41 |
*/ |
| 42 |
public function get_sending_gateway() { |
| 43 |
$gateway = weforms_get_settings( 'email_gateway', 'WordPress' ); |
| 44 |
$available_gateways = $this->get_available_gateways(); |
| 45 |
|
| 46 |
if ( array_key_exists( $gateway, $available_gateways ) ) { |
| 47 |
return $available_gateways[ $gateway ]; |
| 48 |
} |
| 49 |
|
| 50 |
return $available_gateways['wordpress']; |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Send email via the gateway |
| 55 |
* |
| 56 |
* @param string $to Email addresses to send message |
| 57 |
* @param string $subject Email subject |
| 58 |
* @param string $body Message contents |
| 59 |
* @param string $headers Optional. Files to attach. |
| 60 |
* |
| 61 |
* @return bool |
| 62 |
*/ |
| 63 |
public function send( $to, $subject, $body, $headers ) { |
| 64 |
return $this->gateway->send( $to, $subject, $body, $headers ); |
| 65 |
} |
| 66 |
|
| 67 |
} |
| 68 |
|