__( 'MailerSend', 'advanced-form-integration' ),
'tasks' => array(
'send_email' => __( 'Send Email', 'advanced-form-integration' ),
),
);
return $actions;
}
add_filter( 'adfoin_settings_tabs', 'adfoin_mailersend_settings_tab', 10, 1 );
function adfoin_mailersend_settings_tab( $providers ) {
$providers['mailersend'] = __( 'MailerSend', 'advanced-form-integration' );
return $providers;
}
add_action( 'adfoin_settings_view', 'adfoin_mailersend_settings_view', 10, 1 );
function adfoin_mailersend_settings_view( $current_tab ) {
if ( $current_tab !== 'mailersend' ) {
return;
}
if ( ! class_exists( 'ADFOIN_Account_Manager' ) ) {
require_once plugin_dir_path( __FILE__ ) . '../../includes/class-adfoin-account-manager.php';
}
$fields = array(
array(
'name' => 'apiToken',
'label' => __( 'API Token', 'advanced-form-integration' ),
'type' => 'text',
'required' => true,
'mask' => true,
'placeholder' => __( 'mlsn.…', 'advanced-form-integration' ),
'show_in_table' => true,
),
);
$instructions = sprintf(
'
- %s
- %s
- %s
',
sprintf(
/* translators: %s — link to MailerSend tokens page. */
esc_html__( 'Sign in to MailerSend and open %s.', 'advanced-form-integration' ),
'' . esc_html__( 'API Tokens', 'advanced-form-integration' ) . ''
),
esc_html__( 'Create a token with at least the "Email Send" permission. Tokens start with mlsn.', 'advanced-form-integration' ),
esc_html__( 'Paste the token in the form below. AFI sends every request to https://api.mailersend.com/v1/ with this token in the Authorization: Bearer header.', 'advanced-form-integration' )
);
ADFOIN_Account_Manager::render_settings_view( 'mailersend', __( 'MailerSend', 'advanced-form-integration' ), $fields, $instructions );
}
add_action( 'wp_ajax_adfoin_get_mailersend_credentials', 'adfoin_get_mailersend_credentials', 10, 0 );
function adfoin_get_mailersend_credentials() {
if ( ! class_exists( 'ADFOIN_Account_Manager' ) ) {
require_once plugin_dir_path( __FILE__ ) . '../../includes/class-adfoin-account-manager.php';
}
ADFOIN_Account_Manager::ajax_get_credentials_list( 'mailersend' );
}
add_action( 'wp_ajax_adfoin_save_mailersend_credentials', 'adfoin_save_mailersend_credentials', 10, 0 );
function adfoin_save_mailersend_credentials() {
if ( ! class_exists( 'ADFOIN_Account_Manager' ) ) {
require_once plugin_dir_path( __FILE__ ) . '../../includes/class-adfoin-account-manager.php';
}
ADFOIN_Account_Manager::ajax_save_credentials( 'mailersend', array( 'apiToken' ) );
}
/**
* Render ', esc_attr( $option['id'] ), esc_html( $option['title'] ) );
}
}
/**
* Surface the legacy single-option token as a "Default Account (Legacy)"
* record so users who set up MailerSend before the multi-account UI
* existed don't need to re-paste their API token.
*/
add_action( 'plugins_loaded', function () {
if ( class_exists( 'ADFOIN_Account_Manager' ) ) {
ADFOIN_Account_Manager::register_legacy_option_importer( 'mailersend', array(
'apiToken' => 'adfoin_mailersend_api_token',
) );
}
}, 20 );
add_action( 'adfoin_action_fields', 'adfoin_mailersend_action_fields' );
function adfoin_mailersend_action_fields() {
?>
30,
'method' => $method,
'headers' => array(
'Authorization' => 'Bearer ' . $api_token,
'Content-Type' => 'application/json',
'Accept' => 'application/json',
),
);
if ( in_array( strtoupper( $method ), array( 'POST', 'PUT', 'PATCH', 'DELETE' ), true ) && ! empty( $data ) ) {
$args['body'] = wp_json_encode( $data );
}
$response = wp_remote_request( $url, $args );
if ( $record ) {
adfoin_add_to_log( $response, $url, $args, $record );
}
return $response;
}
add_action( 'adfoin_mailersend_job_queue', 'adfoin_mailersend_job_queue', 10, 1 );
function adfoin_mailersend_job_queue( $data ) {
adfoin_mailersend_send_data( $data['record'], $data['posted_data'] );
}
function adfoin_mailersend_send_data( $record, $posted_data ) {
if ( 'send_email' !== ( $record['task'] ?? '' ) ) {
return;
}
$record_data = json_decode( $record['data'], true );
if ( adfoin_check_conditional_logic( $record_data['action_data']['cl'] ?? array(), $posted_data ) ) {
return;
}
$field_data = isset( $record_data['field_data'] ) ? $record_data['field_data'] : array();
$cred_id = isset( $field_data['credId'] ) ? $field_data['credId'] : '';
if ( empty( $cred_id ) ) {
adfoin_add_to_log( new WP_Error( 'mailersend_missing_cred', __( 'No MailerSend account selected.', 'advanced-form-integration' ) ), '', array(), $record );
return;
}
$payload = adfoin_mailersend_build_email_payload( $field_data, $posted_data );
if ( is_wp_error( $payload ) ) {
adfoin_add_to_log( $payload, '', array(), $record );
return;
}
adfoin_mailersend_request( 'email', 'POST', $payload, $record, $cred_id );
}
/**
* Translate the mapped form fields into MailerSend's POST /v1/email
* envelope. Public so the Pro add-on can reuse it as the baseline
* before layering template_id / variables / cc / bcc / etc.
*
* Returns WP_Error when from / to / subject is missing — those are
* required by MailerSend and would otherwise 422.
*/
function adfoin_mailersend_build_email_payload( $field_data, $posted_data ) {
$get = function ( $key ) use ( $field_data, $posted_data ) {
if ( empty( $field_data[ $key ] ) ) {
return '';
}
$value = adfoin_get_parsed_values( $field_data[ $key ], $posted_data );
return is_string( $value ) ? trim( $value ) : '';
};
$from_email = $get( 'from_email' );
$from_name = $get( 'from_name' );
$to_email = $get( 'to_email' );
$to_name = $get( 'to_name' );
$subject = $get( 'subject' );
$text_body = $get( 'text' );
$html_body = $get( 'html' );
if ( ! $from_email || ! $to_email || ! $subject ) {
return new WP_Error( 'mailersend_missing_required', __( 'MailerSend send_email requires from_email, to_email, and subject.', 'advanced-form-integration' ) );
}
// MailerSend body fields — at least one of `text` / `html` /
// `template_id` is required. The build helper only enforces from /
// to / subject; the caller (free or Pro) layers in template_id
// when relevant.
$payload = array(
'from' => array_filter( array(
'email' => $from_email,
'name' => $from_name,
) ),
'to' => array(
array_filter( array(
'email' => $to_email,
'name' => $to_name,
) ),
),
'subject' => $subject,
);
if ( $text_body ) { $payload['text'] = $text_body; }
if ( $html_body ) { $payload['html'] = $html_body; }
return $payload;
}