first commit
This commit is contained in:
@@ -0,0 +1,162 @@
|
||||
<?php if (!defined('FW')) die('Forbidden');
|
||||
|
||||
class FW_Extension_Mailer extends FW_Extension
|
||||
{
|
||||
private $is_configured_cache = null;
|
||||
|
||||
/**
|
||||
* @var FW_Ext_Mailer_Send_Method[]
|
||||
*/
|
||||
private $send_methods;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
protected function _init()
|
||||
{
|
||||
if (is_admin()) {
|
||||
add_action(
|
||||
'fw_extension_settings_form_render:'. $this->get_name(),
|
||||
array($this, '_action_extension_settings_form_render')
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
public function _action_extension_settings_form_render()
|
||||
{
|
||||
wp_enqueue_script(
|
||||
'fw_option_email_settings',
|
||||
$this->get_uri('/static/js/scripts.js'),
|
||||
array('jquery'),
|
||||
false,
|
||||
true
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $to
|
||||
* @param string $subject
|
||||
* @param string $message
|
||||
* @param array $data 'reply_to', 'cc', 'bcc'
|
||||
* @param array $settings Use this settings instead of db settings | Since 1.2.7
|
||||
* @return array {status: 0, message: '...'}
|
||||
*/
|
||||
public function send($to, $subject, $message, $data = array(), $settings = array())
|
||||
{
|
||||
if (empty($settings)) {
|
||||
$settings = $this->get_db_settings_option();
|
||||
}
|
||||
|
||||
$send_method = $this->get_send_method($settings['method']);
|
||||
|
||||
if (!$send_method) {
|
||||
return array(
|
||||
'status' => 0,
|
||||
'message' => __('Invalid send method', 'fw')
|
||||
);
|
||||
}
|
||||
|
||||
if (is_wp_error(
|
||||
$send_method_configuration = $send_method->prepare_settings_options_values(
|
||||
fw_akg($send_method->get_id(), $settings)
|
||||
)
|
||||
)) {
|
||||
return array(
|
||||
'status' => 0,
|
||||
'message' => $send_method_configuration->get_error_message()
|
||||
);
|
||||
}
|
||||
|
||||
$email = new FW_Ext_Mailer_Email();
|
||||
$email->set_to($to);
|
||||
$email->set_subject($subject);
|
||||
$email->set_body($message);
|
||||
|
||||
if (!empty($data['reply_to']) && method_exists($email, 'set_reply_to')) {
|
||||
$email->set_reply_to($data['reply_to']);
|
||||
}
|
||||
if (!empty($data['cc'])) {
|
||||
foreach ($data['cc'] as $_address => $_name) {
|
||||
$email->add_cc($_address, $_name);
|
||||
}
|
||||
}
|
||||
if (!empty($data['bcc'])) {
|
||||
foreach ($data['bcc'] as $_address => $_name) {
|
||||
$email->add_bcc($_address, $_name);
|
||||
}
|
||||
}
|
||||
|
||||
/** @since 1.2.10 */
|
||||
$email = apply_filters('fw_ext_mailer_before_send', $email);
|
||||
|
||||
$result = $send_method->send(
|
||||
$email,
|
||||
fw_akg($send_method->get_id(), $settings),
|
||||
$data
|
||||
);
|
||||
|
||||
return is_wp_error($result)
|
||||
? array(
|
||||
'status' => 0,
|
||||
'message' => $result->get_error_message()
|
||||
)
|
||||
: array(
|
||||
'status' => 1,
|
||||
'message' => __('The message has been successfully sent!', 'fw')
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if extension settings options are valid
|
||||
* @return bool
|
||||
*/
|
||||
public function is_configured()
|
||||
{
|
||||
if (is_null($this->is_configured_cache)) {
|
||||
$send_method = $this->get_send_method(
|
||||
$this->get_db_settings_option('method')
|
||||
);
|
||||
|
||||
if ($send_method) {
|
||||
$this->is_configured_cache = !is_wp_error(
|
||||
$send_method->prepare_settings_options_values(
|
||||
$this->get_db_settings_option($send_method->get_id())
|
||||
)
|
||||
);
|
||||
} else {
|
||||
$this->is_configured_cache = false;
|
||||
}
|
||||
}
|
||||
|
||||
return $this->is_configured_cache;
|
||||
}
|
||||
|
||||
public function get_send_methods()
|
||||
{
|
||||
if (empty($this->send_methods)) {
|
||||
require_once dirname(__FILE__) . '/includes/classes/class-fw-ext-mailer-email.php';
|
||||
require_once dirname(__FILE__) . '/includes/classes/class-fw-ext-mailer-send-method.php';
|
||||
|
||||
$this->send_methods = array();
|
||||
foreach (apply_filters('fw_ext_mailer_send_methods', array()) as $send_method) {
|
||||
$this->send_methods[ $send_method->get_id() ] = $send_method;
|
||||
}
|
||||
}
|
||||
|
||||
return $this->send_methods;
|
||||
}
|
||||
|
||||
public function get_send_method($method_id)
|
||||
{
|
||||
$this->get_send_methods(); // init cache
|
||||
|
||||
if (isset($this->send_methods[$method_id])) {
|
||||
return $this->send_methods[$method_id];
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
<?php if (!defined('FW')) die('Forbidden');
|
||||
|
||||
function fw_ext_mailer_send_mail($to, $subject, $message, $data = array()) {
|
||||
return fw()->extensions->get('mailer')->send($to, $subject, $message, $data);
|
||||
}
|
||||
|
||||
function fw_ext_mailer_is_configured() {
|
||||
return fw()->extensions->get('mailer')->is_configured();
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
<?php if (!defined('FW')) die('Forbidden');
|
||||
|
||||
function _action_fw_ext_mailer_option_types_init() {
|
||||
require_once dirname(__FILE__) . '/includes/option-type-mailer/class-fw-option-type-mailer.php';
|
||||
}
|
||||
add_action('fw_option_types_init', '_action_fw_ext_mailer_option_types_init');
|
||||
|
||||
function _filter_fw_ext_mailer_default_send_methods($send_methods) {
|
||||
require_once dirname(__FILE__) . '/includes/send-methods/class-fw-ext-mailer-send-method-wpmail.php';
|
||||
$send_methods[] = new FW_Ext_Mailer_Send_Method_WPMail;
|
||||
|
||||
require_once dirname(__FILE__) . '/includes/send-methods/class-fw-ext-mailer-send-method-smtp.php';
|
||||
$send_methods[] = new FW_Ext_Mailer_Send_Method_SMTP;
|
||||
|
||||
return $send_methods;
|
||||
}
|
||||
add_filter('fw_ext_mailer_send_methods', '_filter_fw_ext_mailer_default_send_methods');
|
||||
@@ -0,0 +1,164 @@
|
||||
<?php if (!defined('FW')) die('Forbidden');
|
||||
|
||||
/**
|
||||
* @deprecated
|
||||
*/
|
||||
class FW_Ext_Mailer_Sender
|
||||
{
|
||||
private $config;
|
||||
|
||||
public function __construct($config)
|
||||
{
|
||||
$this->config = $config;
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated
|
||||
*/
|
||||
public function send($to, $subject, $message)
|
||||
{
|
||||
trigger_error('Deprecated', E_USER_WARNING);
|
||||
|
||||
$config = $this->get_prepared_config();
|
||||
|
||||
if (!$config) {
|
||||
return array(
|
||||
'status' => 0,
|
||||
'message' => __('Invalid email configuration', 'fw')
|
||||
);
|
||||
} else {
|
||||
switch ($config['_method']) {
|
||||
case 'smtp':
|
||||
return $this->send_through_smtp($to, $subject, $message, $config);
|
||||
break;
|
||||
case 'wpmail':
|
||||
return $this->send_through_wpmail($to, $subject, $message, $config);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated
|
||||
*/
|
||||
public function get_prepared_config()
|
||||
{
|
||||
trigger_error('Deprecated', E_USER_WARNING);
|
||||
|
||||
$settings = $this->config;
|
||||
if (
|
||||
!$settings
|
||||
|| empty($settings['general']['from_name'])
|
||||
|| empty($settings['general']['from_address'])
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$conf = false;
|
||||
$method = trim($settings['method']);
|
||||
switch ($method) {
|
||||
case 'wpmail':
|
||||
$conf = array();
|
||||
break;
|
||||
case 'smtp':
|
||||
$smtp_settings = $settings['smtp'];
|
||||
$host = trim($smtp_settings['host']);
|
||||
$username = trim($smtp_settings['username']);
|
||||
$password = trim($smtp_settings['password']);
|
||||
|
||||
if (
|
||||
$username
|
||||
&& $password
|
||||
&& fw_is_valid_domain_name($host)
|
||||
) {
|
||||
$conf = array(
|
||||
'host' => $host,
|
||||
'username' => $username,
|
||||
'password' => $password,
|
||||
'secure' => $smtp_settings['secure'],
|
||||
'port' => trim($smtp_settings['port'])
|
||||
);
|
||||
|
||||
if (!in_array($conf['secure'], array('ssl', 'tls'))) {
|
||||
$conf['secure'] = false;
|
||||
}
|
||||
|
||||
// in case the port is missing or invalid
|
||||
if (empty($conf['port']) || !is_numeric($conf['port'])) {
|
||||
$conf['port'] = $conf['secure'] ? 465 : 25;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
// add general settings
|
||||
if (false !== $conf) {
|
||||
$conf = array_merge($conf, array(
|
||||
'from_address' => trim($settings['general']['from_address']),
|
||||
'from_name' => trim($settings['general']['from_name']),
|
||||
'_method' => $method
|
||||
));
|
||||
}
|
||||
|
||||
return $conf;
|
||||
}
|
||||
|
||||
private function send_through_smtp($to, $subject, $message, $config)
|
||||
{
|
||||
if(!class_exists('PHPMailer')) {
|
||||
require_once ABSPATH . WPINC . '/class-phpmailer.php';
|
||||
}
|
||||
|
||||
$mailer = new PHPMailer();
|
||||
|
||||
$mailer->isSMTP();
|
||||
$mailer->IsHTML(true);
|
||||
$mailer->Host = $config['host'];
|
||||
$mailer->Port = $config['port'];
|
||||
$mailer->SMTPSecure = $config['secure'];
|
||||
$mailer->SMTPAuth = true;
|
||||
$mailer->Username = $config['username'];
|
||||
$mailer->Password = $config['password'];
|
||||
$mailer->From = $config['from_address'];
|
||||
$mailer->FromName = $config['from_name'];
|
||||
|
||||
//$mailer->SMTPDebug = true;
|
||||
|
||||
if (is_array($to)) {
|
||||
foreach ($to as $mail)
|
||||
$mailer->AddAddress($mail);
|
||||
} else {
|
||||
$mailer->AddAddress($to);
|
||||
}
|
||||
|
||||
$mailer->Subject = $subject;
|
||||
$mailer->Body = $message;
|
||||
|
||||
$result = $mailer->send();
|
||||
|
||||
$mailer->ClearAddresses();
|
||||
$mailer->ClearAllRecipients();
|
||||
|
||||
unset($mailer);
|
||||
|
||||
return $result
|
||||
? array('status' => 1, 'message' => __('Email sent', 'fw'))
|
||||
: array('status' => 0, 'message' => __('Could not send via smtp', 'fw'));
|
||||
}
|
||||
|
||||
private function send_through_wpmail($to, $subject, $message, $config)
|
||||
{
|
||||
$headers = array();
|
||||
|
||||
$headers[] = 'Content-type: text/html; charset=iso-8859-1';
|
||||
|
||||
$headers[] = "From:". htmlspecialchars($config['from_name'], null, 'UTF-8')
|
||||
." <". htmlspecialchars($config['from_address'], null, 'UTF-8') .">";
|
||||
|
||||
$result = wp_mail($to, $subject, $message, $headers);
|
||||
|
||||
return $result
|
||||
? array('status' => 1, 'message' => __('Email sent', 'fw'))
|
||||
: array('status' => 0, 'message' => __('Could not send via wp_mail', 'fw'));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,122 @@
|
||||
<?php if (!defined('FW')) die('Forbidden');
|
||||
|
||||
class FW_Ext_Mailer_Email {
|
||||
protected $from_name = '';
|
||||
protected $from = '';
|
||||
protected $subject = '';
|
||||
protected $body = '';
|
||||
|
||||
// 'john@smith.com' => 'John Smith'
|
||||
protected $to = array();
|
||||
protected $cc = array();
|
||||
protected $bcc = array();
|
||||
|
||||
/**
|
||||
* @var string|array array('email' => 'Name')
|
||||
* @since 1.2.4
|
||||
*/
|
||||
protected $reply_to = '';
|
||||
|
||||
public function __construct() {
|
||||
$this->set_from_name(
|
||||
fw_ext('mailer')->get_db_settings_option('general/from_name')
|
||||
);
|
||||
$this->set_from(
|
||||
fw_ext('mailer')->get_db_settings_option('general/from_address')
|
||||
);
|
||||
}
|
||||
|
||||
public function get_from_name() {
|
||||
return $this->from_name;
|
||||
}
|
||||
|
||||
public function set_from_name($from_name) {
|
||||
$this->from_name = $from_name;
|
||||
}
|
||||
|
||||
public function get_from() {
|
||||
return $this->from;
|
||||
}
|
||||
|
||||
public function set_from($from) {
|
||||
$this->from = $from;
|
||||
}
|
||||
|
||||
public function get_to() {
|
||||
return $this->to;
|
||||
}
|
||||
|
||||
public function set_to($to) {
|
||||
if (!is_array($to)) {
|
||||
$to = explode(',', $to);
|
||||
}
|
||||
|
||||
$this->to = $to;
|
||||
}
|
||||
|
||||
public function get_subject() {
|
||||
return $this->subject;
|
||||
}
|
||||
|
||||
public function set_subject($subject) {
|
||||
$this->subject = $subject;
|
||||
}
|
||||
|
||||
public function get_body() {
|
||||
return $this->body;
|
||||
}
|
||||
|
||||
public function set_body($body) {
|
||||
$this->body = $body;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string|array
|
||||
* @since 1.2.4
|
||||
*/
|
||||
public function get_reply_to() {
|
||||
return $this->reply_to;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string|array $reply_to
|
||||
* @since 1.2.4
|
||||
*/
|
||||
public function set_reply_to($reply_to) {
|
||||
$this->reply_to = $reply_to;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $email
|
||||
* @param string $name
|
||||
* @since 1.2.10
|
||||
*/
|
||||
public function add_cc($email, $name = '') {
|
||||
$this->cc[ $email ] = $name;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
* @since 1.2.10
|
||||
*/
|
||||
public function get_cc() {
|
||||
return $this->cc;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $email
|
||||
* @param string $name
|
||||
* @since 1.2.10
|
||||
*/
|
||||
public function add_bcc($email, $name = '') {
|
||||
$this->bcc[ $email ] = $name;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
* @since 1.2.10
|
||||
*/
|
||||
public function get_bcc() {
|
||||
return $this->bcc;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
<?php if (!defined('FW')) die('Forbidden');
|
||||
|
||||
abstract class FW_Ext_Mailer_Send_Method
|
||||
{
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
abstract public function get_id();
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
abstract public function get_title();
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
abstract public function get_settings_options();
|
||||
|
||||
/**
|
||||
* @param array $values
|
||||
* @return array|WP_Error
|
||||
*/
|
||||
abstract public function prepare_settings_options_values($values);
|
||||
|
||||
/**
|
||||
* @param array $settings_options_values
|
||||
* @param FW_Ext_Mailer_Email $email
|
||||
* @param array $data
|
||||
* @return bool|WP_Error
|
||||
*/
|
||||
abstract public function send(FW_Ext_Mailer_Email $email, $settings_options_values, $data = array());
|
||||
}
|
||||
@@ -0,0 +1,215 @@
|
||||
<?php if ( ! defined( 'FW' ) ) {
|
||||
die( 'Forbidden' );
|
||||
}
|
||||
|
||||
class FW_Option_Type_Mailer extends FW_Option_Type {
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
public function _init() {
|
||||
add_action('wp_ajax_fw_ext_mailer_test_connection', array($this, '_action_ajax_test_connection'));
|
||||
}
|
||||
|
||||
public function get_type() {
|
||||
return 'mailer';
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
public function _get_backend_width_type() {
|
||||
return 'full';
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
protected function _get_defaults() {
|
||||
return array(
|
||||
'label' => false,
|
||||
'value' => array(),
|
||||
'fw-storage' => array(
|
||||
'type' => 'wp-option',
|
||||
'wp-option' => 'fw_ext_settings_options:mailer',
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
private function get_inner_options() {
|
||||
$methods_choices = array();
|
||||
$methods_options = array();
|
||||
|
||||
foreach (fw_ext('mailer')->get_send_methods() as $method) {
|
||||
/**
|
||||
* @var FW_Ext_Mailer_Send_Method $method
|
||||
*/
|
||||
|
||||
$methods_choices[ $method->get_id() ] = $method->get_title();
|
||||
|
||||
$settings_options = $method->get_settings_options();
|
||||
|
||||
if (!empty($settings_options)) {
|
||||
$methods_options['method-' . $method->get_id()] = array(
|
||||
'type' => 'group',
|
||||
'attr' => array(
|
||||
'data-method' => $method->get_id()
|
||||
),
|
||||
'options' => array(
|
||||
$method->get_id() => array(
|
||||
'label' => false,
|
||||
'desc' => false,
|
||||
'type' => 'multi',
|
||||
'inner-options' => $settings_options,
|
||||
),
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
unset($settings_options);
|
||||
|
||||
return array(
|
||||
'method' => array(
|
||||
'label' => __( 'Send Method', 'fw' ),
|
||||
'desc' => __( 'Select the send form method', 'fw' ),
|
||||
'type' => 'short-select',
|
||||
'attr' => array(
|
||||
'data-select-method' => '~'
|
||||
),
|
||||
'choices' => $methods_choices
|
||||
),
|
||||
$methods_options,
|
||||
'general' => array(
|
||||
'label' => false,
|
||||
'desc' => false,
|
||||
'type' => 'multi',
|
||||
'inner-options' => array(
|
||||
'from-group' => array(
|
||||
'type' => 'group',
|
||||
'options' => array(
|
||||
'from_name' => array(
|
||||
'label' => __( 'From Name', 'fw' ),
|
||||
'desc' => __( "The name you'll see in the From filed in your email client.", 'fw' ),
|
||||
'type' => 'text',
|
||||
'value' => '',
|
||||
),
|
||||
)
|
||||
),
|
||||
'from_address' => array(
|
||||
'label' => __( 'From Address', 'fw' ),
|
||||
'desc' => __( 'The form will look like was sent from this email address.', 'fw' ),
|
||||
'type' => 'text',
|
||||
'value' => '',
|
||||
)
|
||||
),
|
||||
),
|
||||
'test-connection' => array(
|
||||
'label' => false,
|
||||
'desc' => false,
|
||||
'type' => 'multi',
|
||||
'inner-options' => array(
|
||||
'test-connection' => array(
|
||||
'type' => 'html-fixed',
|
||||
'attr' => array(
|
||||
'class' => 'test-connection-wrapper'
|
||||
),
|
||||
'html' =>
|
||||
'<div class="test-connection">'.
|
||||
/**/'<div>'.
|
||||
/**//**/'<div>'.
|
||||
/**//**//**/'<input type="email" placeholder="'. esc_attr__('Test email destination', 'fw') .'" style="width:100%;">'.
|
||||
/**//**/'</div>'.
|
||||
/**//**/'<div>'.
|
||||
/**//**//**/'<button class="button" type="button">'. esc_html__('Send a test email', 'fw') .'</button>'.
|
||||
/**//**/'</div>'.
|
||||
/**/'</div>'.
|
||||
'</div>'
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function _enqueue_static( $id, $option, $data ) {
|
||||
wp_enqueue_style(
|
||||
$this->get_type() . '-scripts',
|
||||
fw_ext( 'mailer' )->get_uri() . '/includes/option-type-mailer/static/css/style.css'
|
||||
);
|
||||
|
||||
wp_enqueue_script(
|
||||
$this->get_type() . '-scripts',
|
||||
fw_ext( 'mailer' )->get_uri() . '/includes/option-type-mailer/static/js/scripts.js',
|
||||
array( 'fw-events' ),
|
||||
fw()->manifest->get_version(), true
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
protected function _render( $id, $option, $data ) {
|
||||
if (empty($data['value'])) {
|
||||
$data['value'] = fw_db_option_storage_load($id, $option, $data['value']);
|
||||
}
|
||||
|
||||
$wrapper_attr = $option['attr'];
|
||||
unset($wrapper_attr['name'], $wrapper_attr['value']);
|
||||
|
||||
return
|
||||
'<div '. fw_attr_to_html($wrapper_attr) .'>'.
|
||||
fw()->backend->option_type( 'multi' )->render( $id, array(
|
||||
'inner-options' => $this->get_inner_options(),
|
||||
), $data ) .
|
||||
'</div>';
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* @param array $option
|
||||
* @param array|null|string $input_value
|
||||
*
|
||||
* @return array|bool|int|string
|
||||
*/
|
||||
protected function _get_value_from_input( $option, $input_value ) {
|
||||
return (is_array( $input_value ) && ! empty( $input_value ))
|
||||
? fw_get_options_values_from_input($this->get_inner_options(), $input_value)
|
||||
: $option['value'];
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
public function _action_ajax_test_connection() {
|
||||
if (!current_user_can('edit_posts')) {
|
||||
return wp_send_json_error(new WP_Error('forbidden', __('Forbidden', 'fw')));
|
||||
} elseif (!is_email($to = FW_Request::POST('to'))) {
|
||||
return wp_send_json_error(new WP_Error('forbidden', __('Invalid email', 'fw')));
|
||||
} elseif (!is_array($settings = FW_Request::POST('settings'))) {
|
||||
return wp_send_json_error(new WP_Error('forbidden', __('Invalid settings', 'fw')));
|
||||
}
|
||||
|
||||
/** @var FW_Extension_Mailer $ext */
|
||||
$ext = fw_ext('mailer');
|
||||
|
||||
$result = $ext->send(
|
||||
$to,
|
||||
__('Test Subject', 'fw'),
|
||||
'<strong>'. __('Test Message', 'fw') .'</strong>',
|
||||
array(),
|
||||
fw_get_options_values_from_input($this->get_inner_options(), $settings)
|
||||
);
|
||||
|
||||
if ($result['status']) {
|
||||
wp_send_json_success();
|
||||
} else {
|
||||
wp_send_json_error(new WP_Error('fail', $result['message']));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
FW_Option_Type::register( 'FW_Option_Type_Mailer' );
|
||||
@@ -0,0 +1,26 @@
|
||||
.fw-backend-option-type-mailer {
|
||||
padding: 0;
|
||||
}
|
||||
|
||||
/* test connection */
|
||||
|
||||
.fw-backend-option-type-mailer .test-connection {
|
||||
display: table;
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
.fw-backend-option-type-mailer .test-connection > div {
|
||||
display: table-row;
|
||||
}
|
||||
|
||||
.fw-backend-option-type-mailer .test-connection > div > div {
|
||||
display: table-cell;
|
||||
vertical-align: middle;
|
||||
}
|
||||
|
||||
.fw-backend-option-type-mailer .test-connection > div > div:last-child {
|
||||
width: 1px;
|
||||
padding-left: 10px;
|
||||
}
|
||||
|
||||
/* test connection */
|
||||
@@ -0,0 +1,79 @@
|
||||
"use strict";
|
||||
(function($, fwe){
|
||||
fwe.on('fw:options:init', function (data) {
|
||||
data.$elements.find('.fw-option-type-mailer:not(.initialized)')
|
||||
.each(function(){
|
||||
var $option = $(this);
|
||||
|
||||
$option.find('select[data-select-method]')
|
||||
.on('change', function(){
|
||||
$option.find('.fw-backend-options-group[data-method]').hide();
|
||||
$option.find('.fw-backend-options-group[data-method="'+ this.value +'"]').show();
|
||||
})
|
||||
.trigger('change');
|
||||
|
||||
$option.on('click', '.test-connection button', function(){
|
||||
var $to = $option.find('.test-connection input[type="email"]:first'),
|
||||
to = $.trim($to.val());
|
||||
|
||||
if (!to.length) {
|
||||
return $to.focus();
|
||||
}
|
||||
|
||||
var $button = $(this).attr('disabled', 'disabled');
|
||||
|
||||
{ // <input name="{prefix}...">
|
||||
var namePrefix = $option.find('.test-connection-wrapper input:first').attr('name');
|
||||
|
||||
namePrefix = namePrefix.split('][');
|
||||
namePrefix.pop();
|
||||
namePrefix.pop();
|
||||
namePrefix = namePrefix.join('][');
|
||||
namePrefix += ']';
|
||||
}
|
||||
|
||||
var vars = [
|
||||
{name: 'action', value: 'fw_ext_mailer_test_connection'},
|
||||
{name: 'to', value: to}
|
||||
];
|
||||
|
||||
$.each($option.find('[name^="'+ namePrefix +'"]').serializeArray(), function(i, v) {
|
||||
v.name = v.name.split(namePrefix);
|
||||
v.name.shift();
|
||||
v.name = v.name.join(namePrefix);
|
||||
v.name = 'settings'+ v.name;
|
||||
|
||||
vars.push(v);
|
||||
});
|
||||
|
||||
$.ajax({
|
||||
url: ajaxurl,
|
||||
data: vars,
|
||||
method: 'post',
|
||||
dataType: 'json'
|
||||
}).done(function (r) {
|
||||
if (r.success) {
|
||||
fw.soleModal.show(
|
||||
'fw-option-mailer',
|
||||
'<span style="font-size: 7em;">✔</span>',
|
||||
{}
|
||||
);
|
||||
} else {
|
||||
try {
|
||||
alert(r.data[0].message);
|
||||
} catch (e) {
|
||||
alert('Request failed');
|
||||
}
|
||||
}
|
||||
}).fail(function (jqXHR, textStatus, errorThrown) {
|
||||
alert('AJAX error: '+ String(errorThrown));
|
||||
}).always(function () {
|
||||
setTimeout(function () { // prevent user to click too often
|
||||
$button.removeAttr('disabled');
|
||||
}, 3000);
|
||||
});
|
||||
});
|
||||
})
|
||||
.addClass('initialized');
|
||||
});
|
||||
})(jQuery, fwEvents);
|
||||
@@ -0,0 +1,206 @@
|
||||
<?php if (!defined('FW')) die('Forbidden');
|
||||
|
||||
class FW_Ext_Mailer_Send_Method_SMTP extends FW_Ext_Mailer_Send_Method {
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function get_id() {
|
||||
return 'smtp';
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function get_title() {
|
||||
return 'SMTP';
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function get_settings_options() {
|
||||
return array(
|
||||
'host' => array(
|
||||
'label' => __( 'Server Address', 'fw' ),
|
||||
'desc' => __( 'Enter your email server', 'fw' ),
|
||||
'type' => 'text',
|
||||
'value' => '',
|
||||
),
|
||||
'username' => array(
|
||||
'label' => __( 'Username', 'fw' ),
|
||||
'desc' => __( 'Enter your username', 'fw' ),
|
||||
'type' => 'text',
|
||||
'value' => '',
|
||||
),
|
||||
'password' => array(
|
||||
'label' => __( 'Password', 'fw' ),
|
||||
'desc' => __( 'Enter your password', 'fw' ),
|
||||
'type' => 'password',
|
||||
'value' => '',
|
||||
),
|
||||
'secure' => array(
|
||||
'label' => __( 'Secure Connection', 'fw' ),
|
||||
'type' => 'radio',
|
||||
'inline' => true,
|
||||
'value' => 'no',
|
||||
'choices' => array(
|
||||
'no' => 'No',
|
||||
'ssl' => 'SSL',
|
||||
'tls' => 'TLS'
|
||||
)
|
||||
),
|
||||
'port' => array(
|
||||
'label' => __( 'Custom Port', 'fw' ),
|
||||
'desc' => __( 'Optional - SMTP port number to use.', 'fw' ),
|
||||
'help' => __( 'Leave blank for default (SMTP - 25, SMTPS - 465)', 'fw' ),
|
||||
'type' => 'text',
|
||||
'attr' => array(
|
||||
'maxlength' => 5,
|
||||
),
|
||||
'value' => '',
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $values
|
||||
* @return array|WP_Error
|
||||
*/
|
||||
public function prepare_settings_options_values($values) {
|
||||
$conf = array(
|
||||
'host' => trim($values['host']),
|
||||
'username' => trim($values['username']),
|
||||
'password' => trim($values['password']),
|
||||
'secure' => $values['secure'],
|
||||
'port' => trim($values['port'])
|
||||
);
|
||||
|
||||
if (empty($conf['username'])) {
|
||||
return new WP_Error(
|
||||
'empty_username',
|
||||
__('Username cannot be empty', 'fw')
|
||||
);
|
||||
}
|
||||
|
||||
if (empty($conf['password'])) {
|
||||
return new WP_Error(
|
||||
'empty_password',
|
||||
__('Password cannot be empty', 'fw')
|
||||
);
|
||||
}
|
||||
|
||||
if (!fw_is_valid_domain_name($conf['host'])) {
|
||||
return new WP_Error(
|
||||
'invalid_host',
|
||||
__('Invalid host', 'fw')
|
||||
);
|
||||
}
|
||||
|
||||
if (!in_array($conf['secure'], array('ssl', 'tls'))) {
|
||||
$conf['secure'] = false;
|
||||
}
|
||||
|
||||
// in case the port is missing or invalid
|
||||
if (empty($conf['port']) || !is_numeric($conf['port'])) {
|
||||
$conf['port'] = 25;
|
||||
|
||||
if ($conf['secure']) {
|
||||
if ($conf['secure'] === 'ssl') {
|
||||
$conf['port'] = 465;
|
||||
} elseif ($conf['secure'] === 'tls') {
|
||||
$conf['port'] = 587;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $conf;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $settings_options_values
|
||||
* @param FW_Ext_Mailer_Email $email
|
||||
* @param array $data
|
||||
* @return bool|WP_Error
|
||||
*/
|
||||
public function send(FW_Ext_Mailer_Email $email, $settings_options_values, $data = array()) {
|
||||
if (!class_exists('PHPMailer')) {
|
||||
require_once ABSPATH . WPINC . '/class-phpmailer.php';
|
||||
}
|
||||
|
||||
$config = self::prepare_settings_options_values($settings_options_values);
|
||||
|
||||
if (is_wp_error($config)) {
|
||||
return $config;
|
||||
}
|
||||
|
||||
$mailer = new PHPMailer();
|
||||
|
||||
$mailer->isSMTP();
|
||||
$mailer->IsHTML(true);
|
||||
$mailer->Host = $config['host'];
|
||||
$mailer->Port = $config['port'];
|
||||
$mailer->SMTPSecure = $config['secure'];
|
||||
$mailer->SMTPAuth = true;
|
||||
$mailer->Username = $config['username'];
|
||||
$mailer->Password = $config['password'];
|
||||
$mailer->CharSet = 'utf-8';
|
||||
|
||||
if (trim($email->get_from())) {
|
||||
$mailer->From = $email->get_from();
|
||||
|
||||
if (trim($email->get_from_name())) {
|
||||
$mailer->FromName = $email->get_from_name();
|
||||
}
|
||||
}
|
||||
|
||||
if (is_array($email->get_to())) {
|
||||
foreach ($email->get_to() as $_address) {
|
||||
$mailer->AddAddress($_address);
|
||||
}
|
||||
} else {
|
||||
$mailer->AddAddress($email->get_to());
|
||||
}
|
||||
|
||||
if (method_exists($email, 'get_reply_to') && $email->get_reply_to()) {
|
||||
if (is_array($email->get_reply_to())) {
|
||||
foreach ($email->get_reply_to() as $_address => $_name) {
|
||||
$mailer->addReplyTo($_address, $_name);
|
||||
}
|
||||
} else {
|
||||
$mailer->addReplyTo($email->get_reply_to());
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($email->get_cc() as $_address => $_name) {
|
||||
$mailer->addCC($_address, $_name);
|
||||
}
|
||||
foreach ($email->get_bcc() as $_address => $_name) {
|
||||
$mailer->addBCC($_address, $_name);
|
||||
}
|
||||
|
||||
$mailer->Subject = $email->get_subject();
|
||||
$mailer->Body = $email->get_body();
|
||||
|
||||
//$mailer->SMTPDebug = true;
|
||||
|
||||
try {
|
||||
return $mailer->send()
|
||||
? true
|
||||
: new WP_Error(
|
||||
'failed',
|
||||
__('Could not send the email', 'fw')
|
||||
);
|
||||
} catch (phpmailerException $e) {
|
||||
return new WP_Error(
|
||||
'failed',
|
||||
$e->errorMessage()
|
||||
);
|
||||
} catch (Exception $e) {
|
||||
return new WP_Error(
|
||||
'failed',
|
||||
$e->getMessage()
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
<?php if (!defined('FW')) die('Forbidden');
|
||||
|
||||
class FW_Ext_Mailer_Send_Method_WPMail extends FW_Ext_Mailer_Send_Method {
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function get_id() {
|
||||
return 'wpmail';
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function get_title() {
|
||||
return 'wp-mail';
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function get_settings_options() {
|
||||
return array();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $values
|
||||
* @return array|WP_Error
|
||||
*/
|
||||
public function prepare_settings_options_values($values) {
|
||||
return array();
|
||||
}
|
||||
|
||||
private function make_email_header($address, $name) {
|
||||
return (trim($name) ? ' '. htmlspecialchars($name, null, 'UTF-8') : '')
|
||||
.' <'. htmlspecialchars($address, null, 'UTF-8') .'>';
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $settings_options_values
|
||||
* @param FW_Ext_Mailer_Email $email
|
||||
* @param array $data
|
||||
* @return bool|WP_Error
|
||||
*/
|
||||
public function send(FW_Ext_Mailer_Email $email, $settings_options_values, $data = array()) {
|
||||
{
|
||||
$headers = array();
|
||||
|
||||
$headers[] = 'Content-type: text/html; charset=utf-8';
|
||||
|
||||
if (trim($email->get_from())) {
|
||||
$headers[] = 'From:'. $this->make_email_header($email->get_from(), $email->get_from_name());
|
||||
}
|
||||
|
||||
if (method_exists($email, 'get_reply_to') && $email->get_reply_to()) {
|
||||
if (is_array($email->get_reply_to())) {
|
||||
foreach ($email->get_reply_to() as $_address => $_name) {
|
||||
$headers[] = 'Reply-To:'. $this->make_email_header($_address, $_name);
|
||||
}
|
||||
} else {
|
||||
$headers[] = 'Reply-To:'. $this->make_email_header($email->get_reply_to(), '');
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($email->get_cc() as $_address => $_name) {
|
||||
$headers[] = 'Cc:'. $this->make_email_header($_address, $_name);
|
||||
}
|
||||
foreach ($email->get_bcc() as $_address => $_name) {
|
||||
$headers[] = 'Bcc:'. $this->make_email_header($_address, $_name);
|
||||
}
|
||||
}
|
||||
|
||||
$result = wp_mail(
|
||||
$email->get_to(),
|
||||
$email->get_subject(),
|
||||
$email->get_body(),
|
||||
$headers
|
||||
);
|
||||
|
||||
return $result
|
||||
? true
|
||||
: new WP_Error(
|
||||
'failed',
|
||||
__('Could not send the email', 'fw')
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
<?php if (!defined('FW')) die('Forbidden');
|
||||
|
||||
$manifest = array();
|
||||
|
||||
$manifest['name'] = __('Mailer', 'fw');
|
||||
$manifest['description'] = __('This extension will let you set some global email options and it is used by other extensions (like Forms) to send emails.', 'fw');
|
||||
$manifest['version'] = '1.2.12';
|
||||
$manifest['standalone'] = false;
|
||||
$manifest['display'] = false;
|
||||
$manifest['github_update'] = 'ThemeFuse/Unyson-Mailer-Extension';
|
||||
$manifest['github_repo'] = 'https://github.com/ThemeFuse/Unyson-Mailer-Extension';
|
||||
$manifest['uri'] = 'http://manual.unyson.io/en/latest/extension/forms/index.html#content';
|
||||
$manifest['author'] = 'ThemeFuse';
|
||||
$manifest['author_uri'] = 'http://themefuse.com/';
|
||||
Reference in New Issue
Block a user