rebase code on oct-10-2023
This commit is contained in:
@@ -56,7 +56,7 @@ class Options extends OptionsAbstract {
|
||||
<!-- API Key -->
|
||||
<div id="wp-mail-smtp-setting-row-<?php echo esc_attr( $this->get_slug() ); ?>-api_key" class="wp-mail-smtp-setting-row wp-mail-smtp-setting-row-text wp-mail-smtp-clear">
|
||||
<div class="wp-mail-smtp-setting-label">
|
||||
<label for="wp-mail-smtp-setting-<?php echo esc_attr( $this->get_slug() ); ?>-api_key"><?php esc_html_e( 'Mailgun API Key', 'wp-mail-smtp' ); ?></label>
|
||||
<label for="wp-mail-smtp-setting-<?php echo esc_attr( $this->get_slug() ); ?>-api_key"><?php esc_html_e( 'Private API Key', 'wp-mail-smtp' ); ?></label>
|
||||
</div>
|
||||
<div class="wp-mail-smtp-setting-field">
|
||||
<?php if ( $this->connection_options->is_const_defined( $this->get_slug(), 'api_key' ) ) : ?>
|
||||
@@ -73,18 +73,12 @@ class Options extends OptionsAbstract {
|
||||
<?php endif; ?>
|
||||
<p class="desc">
|
||||
<?php
|
||||
echo wp_kses(
|
||||
sprintf( /* translators: %s - API key URL. */
|
||||
__( 'Follow this link to <a href="%s" target="_blank" rel="noopener noreferrer">get a Mailgun API Key</a>. Generate a key in the "Mailgun API Keys" section.', 'wp-mail-smtp' ),
|
||||
'https://app.mailgun.com/settings/api_security'
|
||||
),
|
||||
[
|
||||
'a' => [
|
||||
'href' => [],
|
||||
'rel' => [],
|
||||
'target' => [],
|
||||
],
|
||||
]
|
||||
printf(
|
||||
/* translators: %s - API key link. */
|
||||
esc_html__( 'Follow this link to get a Private API Key from Mailgun: %s.', 'wp-mail-smtp' ),
|
||||
'<a href="https://app.mailgun.com/app/account/security/api_keys" target="_blank" rel="noopener noreferrer">' .
|
||||
esc_html__( 'Get a Private API Key', 'wp-mail-smtp' ) .
|
||||
'</a>'
|
||||
);
|
||||
?>
|
||||
</p>
|
||||
@@ -107,7 +101,7 @@ class Options extends OptionsAbstract {
|
||||
printf(
|
||||
/* translators: %s - Domain Name link. */
|
||||
esc_html__( 'Follow this link to get a Domain Name from Mailgun: %s.', 'wp-mail-smtp' ),
|
||||
'<a href="https://app.mailgun.com/app/sending/domains" target="_blank" rel="noopener noreferrer">' .
|
||||
'<a href="https://app.mailgun.com/app/domains" target="_blank" rel="noopener noreferrer">' .
|
||||
esc_html__( 'Get a Domain Name', 'wp-mail-smtp' ) .
|
||||
'</a>'
|
||||
);
|
||||
|
||||
@@ -3,6 +3,10 @@
|
||||
namespace WPMailSMTP\Providers\Sendinblue;
|
||||
|
||||
use WPMailSMTP\ConnectionInterface;
|
||||
use WPMailSMTP\Vendor\SendinBlue\Client\Api\AccountApi;
|
||||
use WPMailSMTP\Vendor\SendinBlue\Client\Api\SendersApi;
|
||||
use WPMailSMTP\Vendor\SendinBlue\Client\Api\TransactionalEmailsApi;
|
||||
use WPMailSMTP\Vendor\SendinBlue\Client\Configuration;
|
||||
|
||||
/**
|
||||
* Class Api is a wrapper for Sendinblue library with handy methods.
|
||||
@@ -51,54 +55,51 @@ class Api {
|
||||
* Configure API key authorization: api-key.
|
||||
*
|
||||
* @since 1.6.0
|
||||
* @deprecated 3.9.0 We are no longer using the Sendinblue SDK.
|
||||
*
|
||||
* @return null
|
||||
* @return Configuration
|
||||
*/
|
||||
protected function get_api_config() {
|
||||
|
||||
_deprecated_function( __METHOD__, '3.9.0' );
|
||||
|
||||
return null;
|
||||
return Configuration::getDefaultConfiguration()->setApiKey( 'api-key', isset( $this->options['api_key'] ) ? $this->options['api_key'] : '' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the mailer client instance for Account API.
|
||||
*
|
||||
* @since 1.6.0
|
||||
* @deprecated 3.9.0 We are no longer using the Sendinblue SDK.
|
||||
*/
|
||||
public function get_account_client() {
|
||||
|
||||
_deprecated_function( __METHOD__, '3.9.0' );
|
||||
// Include the library.
|
||||
require_once wp_mail_smtp()->plugin_path . '/vendor/autoload.php';
|
||||
|
||||
return null;
|
||||
return new AccountApi( null, $this->get_api_config() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the mailer client instance for Sender API.
|
||||
*
|
||||
* @since 1.6.0
|
||||
* @deprecated 3.9.0 We are no longer using the Sendinblue SDK.
|
||||
*/
|
||||
public function get_sender_client() {
|
||||
|
||||
_deprecated_function( __METHOD__, '3.9.0' );
|
||||
// Include the library.
|
||||
require_once wp_mail_smtp()->plugin_path . '/vendor/autoload.php';
|
||||
|
||||
return null;
|
||||
return new SendersApi( null, $this->get_api_config() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the mailer client instance for SMTP API.
|
||||
*
|
||||
* @since 1.6.0
|
||||
* @deprecated 3.9.0 We are no longer using the Sendinblue SDK.
|
||||
*/
|
||||
public function get_smtp_client() {
|
||||
|
||||
_deprecated_function( __METHOD__, '3.9.0' );
|
||||
// Include the library.
|
||||
require_once wp_mail_smtp()->plugin_path . '/vendor/autoload.php';
|
||||
|
||||
return null;
|
||||
return new TransactionalEmailsApi( null, $this->get_api_config() );
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -2,10 +2,13 @@
|
||||
|
||||
namespace WPMailSMTP\Providers\Sendinblue;
|
||||
|
||||
use WPMailSMTP\ConnectionInterface;
|
||||
use WPMailSMTP\Admin\DebugEvents\DebugEvents;
|
||||
use WPMailSMTP\Helpers\Helpers;
|
||||
use WPMailSMTP\MailCatcherInterface;
|
||||
use WPMailSMTP\Providers\MailerAbstract;
|
||||
use WPMailSMTP\Vendor\SendinBlue\Client\ApiException;
|
||||
use WPMailSMTP\Vendor\SendinBlue\Client\Model\CreateSmtpEmail;
|
||||
use WPMailSMTP\Vendor\SendinBlue\Client\Model\SendSmtpEmail;
|
||||
use WPMailSMTP\WP;
|
||||
|
||||
/**
|
||||
@@ -24,41 +27,15 @@ class Mailer extends MailerAbstract {
|
||||
*/
|
||||
protected $email_sent_code = 201;
|
||||
|
||||
/**
|
||||
* Response code for scheduled email.
|
||||
*
|
||||
* @since 3.9.0
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
protected $email_scheduled_code = 202;
|
||||
|
||||
/**
|
||||
* URL to make an API request to.
|
||||
* Not actually used, because we use a lib to make requests.
|
||||
*
|
||||
* @since 1.6.0
|
||||
* @since 3.9.0 Update to use Brevo API.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $url = 'https://api.brevo.com/v3/smtp/email';
|
||||
|
||||
/**
|
||||
* Mailer constructor.
|
||||
*
|
||||
* @since 3.9.0
|
||||
*
|
||||
* @param MailCatcherInterface $phpmailer The MailCatcher object.
|
||||
* @param ConnectionInterface $connection The Connection object.
|
||||
*/
|
||||
public function __construct( $phpmailer, $connection = null ) {
|
||||
|
||||
parent::__construct( $phpmailer, $connection );
|
||||
|
||||
$this->set_header( 'api-key', $this->connection_options->get( $this->mailer, 'api_key' ) );
|
||||
$this->set_header( 'Accept', 'application/json' );
|
||||
$this->set_header( 'content-type', 'application/json' );
|
||||
}
|
||||
protected $url = 'https://api.sendinblue.com/v3';
|
||||
|
||||
/**
|
||||
* The list of allowed attachment files extensions.
|
||||
@@ -74,49 +51,15 @@ class Mailer extends MailerAbstract {
|
||||
// @formatter:on
|
||||
|
||||
/**
|
||||
* Redefine the way custom headers are processed for this mailer - they should be in body.
|
||||
* @inheritDoc
|
||||
*
|
||||
* @since 3.9.0
|
||||
*
|
||||
* @param array $headers List of key=>value pairs.
|
||||
* @since 1.6.0
|
||||
*/
|
||||
public function set_headers( $headers ) {
|
||||
|
||||
foreach ( $headers as $header ) {
|
||||
$name = isset( $header[0] ) ? $header[0] : false;
|
||||
$value = isset( $header[1] ) ? $header[1] : false;
|
||||
|
||||
$this->set_body_header( $name, $value );
|
||||
}
|
||||
|
||||
// Add custom PHPMailer-specific header.
|
||||
$this->set_body_header( 'X-Mailer', 'WPMailSMTP/Mailer/' . $this->mailer . ' ' . WPMS_PLUGIN_VER );
|
||||
}
|
||||
|
||||
/**
|
||||
* This mailer supports email-related custom headers inside a body of the message.
|
||||
*
|
||||
* @since 3.9.0
|
||||
*
|
||||
* @param string $name Key.
|
||||
* @param string $value Value.
|
||||
*/
|
||||
public function set_body_header( $name, $value ) {
|
||||
public function set_header( $name, $value ) {
|
||||
|
||||
$name = sanitize_text_field( $name );
|
||||
|
||||
if ( empty( $name ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$headers = isset( $this->body['headers'] ) ? (array) $this->body['headers'] : [];
|
||||
$headers[ $name ] = WP::sanitize_value( $value );
|
||||
|
||||
$this->set_body_param(
|
||||
[
|
||||
'headers' => $headers,
|
||||
]
|
||||
);
|
||||
$this->body['headers'][ $name ] = WP::sanitize_value( $value );
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -320,9 +263,8 @@ class Mailer extends MailerAbstract {
|
||||
* Get the email body.
|
||||
*
|
||||
* @since 1.6.0
|
||||
* @since 3.9.0 Returns email body array instead of `SendSmtpEmail` object.
|
||||
*
|
||||
* @return array
|
||||
* @return SendSmtpEmail
|
||||
*/
|
||||
public function get_body() {
|
||||
|
||||
@@ -333,110 +275,82 @@ class Mailer extends MailerAbstract {
|
||||
*
|
||||
* @param array $body Email body.
|
||||
*/
|
||||
return apply_filters( 'wp_mail_smtp_providers_sendinblue_mailer_get_body', $this->body );
|
||||
$body = apply_filters( 'wp_mail_smtp_providers_sendinblue_mailer_get_body', $this->body );
|
||||
|
||||
return new SendSmtpEmail( $body );
|
||||
}
|
||||
|
||||
/**
|
||||
* Send email.
|
||||
* Use a library to send emails.
|
||||
*
|
||||
* @since 1.6.0
|
||||
* @since 3.9.0 Use API instead of SDK to send email.
|
||||
*/
|
||||
public function send() {
|
||||
|
||||
$response = wp_safe_remote_post(
|
||||
$this->url,
|
||||
[
|
||||
'headers' => $this->get_headers(),
|
||||
'body' => wp_json_encode( $this->get_body() ),
|
||||
]
|
||||
);
|
||||
try {
|
||||
$api = new Api( $this->connection );
|
||||
|
||||
$this->process_response( $response );
|
||||
$response = $api->get_smtp_client()->sendTransacEmail( $this->get_body() );
|
||||
|
||||
DebugEvents::add_debug(
|
||||
esc_html__( 'An email request was sent to the Brevo API.', 'wp-mail-smtp' )
|
||||
);
|
||||
|
||||
$this->process_response( $response );
|
||||
} catch ( ApiException $e ) {
|
||||
$error = json_decode( $e->getResponseBody() );
|
||||
|
||||
if ( json_last_error() === JSON_ERROR_NONE && ! empty( $error ) ) {
|
||||
$message = Helpers::format_error_message( $error->message, $error->code );
|
||||
} else {
|
||||
$message = $e->getMessage();
|
||||
}
|
||||
|
||||
$this->error_message = $message;
|
||||
} catch ( \Exception $e ) {
|
||||
$this->error_message = $e->getMessage();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* We might need to do something after the email was sent to the API.
|
||||
* In this method we preprocess the response from the API.
|
||||
* Save response from the API to use it later.
|
||||
* All the actually response processing is done in send() method,
|
||||
* because SendinBlue throws exception if any error occurs.
|
||||
*
|
||||
* @since 1.6.0
|
||||
* @since 3.9.0 Expect a generic class object instead of `CreateSmtpEmail`.
|
||||
*
|
||||
* @param mixed $response Response from the API.
|
||||
* @param CreateSmtpEmail $response The Sendinblue Email object.
|
||||
*/
|
||||
protected function process_response( $response ) {
|
||||
|
||||
parent::process_response( $response );
|
||||
|
||||
if ( $this->has_message_id() ) {
|
||||
$this->phpmailer->MessageID = $this->response['body']->messageId;
|
||||
$this->verify_sent_status = true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a Sendinblue-specific response with a helpful error.
|
||||
*
|
||||
* @since 3.9.0
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function get_response_error() {
|
||||
|
||||
$error_text = [];
|
||||
|
||||
if ( ! empty( $this->error_message ) ) {
|
||||
$error_text[] = $this->error_message;
|
||||
}
|
||||
|
||||
if ( ! empty( $this->response ) ) {
|
||||
$body = wp_remote_retrieve_body( $this->response );
|
||||
|
||||
if ( ! empty( $body->message ) ) {
|
||||
$error_text[] = Helpers::format_error_message( $body->message, ! empty( $body->code ) ? $body->code : '' );
|
||||
} else {
|
||||
$error_text[] = WP::wp_remote_get_response_error_message( $this->response );
|
||||
}
|
||||
}
|
||||
|
||||
return implode( WP::EOL, array_map( 'esc_textarea', array_filter( $error_text ) ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether the response has `messageId` property.
|
||||
*
|
||||
* @since 3.9.0
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function has_message_id() {
|
||||
$this->response = $response;
|
||||
|
||||
if (
|
||||
! in_array(
|
||||
wp_remote_retrieve_response_code( $this->response ),
|
||||
[ $this->email_sent_code, $this->email_scheduled_code ],
|
||||
true
|
||||
) ||
|
||||
empty( $this->response['body']->messageId )
|
||||
is_a( $response, 'WPMailSMTP\Vendor\SendinBlue\Client\Model\CreateSmtpEmail' ) &&
|
||||
method_exists( $response, 'getMessageId' )
|
||||
) {
|
||||
return false;
|
||||
$this->phpmailer->MessageID = $response->getMessageId();
|
||||
$this->verify_sent_status = true;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether the email was sent.
|
||||
*
|
||||
* @since 1.6.0
|
||||
* @since 3.9.0 Check if `$this->response` has `messageId` property to check if the email was sent.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function is_email_sent() {
|
||||
|
||||
$is_sent = false;
|
||||
|
||||
if ( $this->response instanceof CreateSmtpEmail ) {
|
||||
$is_sent = $this->response->valid();
|
||||
}
|
||||
|
||||
/** This filter is documented in src/Providers/MailerAbstract.php. */
|
||||
return apply_filters( 'wp_mail_smtp_providers_mailer_is_email_sent', $this->has_message_id(), $this->mailer ); // phpcs:ignore WPForms.PHP.ValidateHooks.InvalidHookName
|
||||
return apply_filters( 'wp_mail_smtp_providers_mailer_is_email_sent', $is_sent, $this->mailer );
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user