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