first commit

This commit is contained in:
Rachit Bhargava
2023-07-21 17:12:10 -04:00
parent d0fe47dde4
commit 5d0f0734d8
14003 changed files with 2829464 additions and 0 deletions

View File

@@ -0,0 +1,116 @@
<?php
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.
*
* @since 1.6.0
*/
class Api {
/**
* The Connection object.
*
* @since 3.7.0
*
* @var ConnectionInterface
*/
private $connection;
/**
* Contains mailer options, constants + DB values.
*
* @since 1.6.0
*
* @var array
*/
private $options;
/**
* API constructor that inits defaults and retrieves options.
*
* @since 1.6.0
*
* @param ConnectionInterface $connection The Connection object.
*/
public function __construct( $connection = null ) {
if ( ! is_null( $connection ) ) {
$this->connection = $connection;
} else {
$this->connection = wp_mail_smtp()->get_connections_manager()->get_primary_connection();
}
$this->options = $this->connection->get_options()->get_group( Options::SLUG );
}
/**
* Configure API key authorization: api-key.
*
* @since 1.6.0
*
* @return Configuration
*/
protected function get_api_config() {
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
*/
public function get_account_client() {
// Include the library.
require_once wp_mail_smtp()->plugin_path . '/vendor/autoload.php';
return new AccountApi( null, $this->get_api_config() );
}
/**
* Get the mailer client instance for Sender API.
*
* @since 1.6.0
*/
public function get_sender_client() {
// Include the library.
require_once wp_mail_smtp()->plugin_path . '/vendor/autoload.php';
return new SendersApi( null, $this->get_api_config() );
}
/**
* Get the mailer client instance for SMTP API.
*
* @since 1.6.0
*/
public function get_smtp_client() {
// Include the library.
require_once wp_mail_smtp()->plugin_path . '/vendor/autoload.php';
return new TransactionalEmailsApi( null, $this->get_api_config() );
}
/**
* Whether the mailer is ready to be used in API calls.
*
* @since 1.6.0
*
* @return bool
*/
public function is_ready() {
return ! empty( $this->options['api_key'] );
}
}

View File

@@ -0,0 +1,384 @@
<?php
namespace WPMailSMTP\Providers\Sendinblue;
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;
/**
* Class Mailer.
*
* @since 1.6.0
*/
class Mailer extends MailerAbstract {
/**
* Which response code from HTTP provider is considered to be successful?
*
* @since 1.6.0
*
* @var int
*/
protected $email_sent_code = 201;
/**
* URL to make an API request to.
* Not actually used, because we use a lib to make requests.
*
* @since 1.6.0
*
* @var string
*/
protected $url = 'https://api.sendinblue.com/v3';
/**
* The list of allowed attachment files extensions.
*
* @see https://developers.sendinblue.com/reference#sendTransacEmail_attachment__title
*
* @since 1.6.0
*
* @var array
*/
// @formatter:off
protected $allowed_attach_ext = array( 'xlsx', 'xls', 'ods', 'docx', 'docm', 'doc', 'csv', 'pdf', 'txt', 'gif', 'jpg', 'jpeg', 'png', 'tif', 'tiff', 'rtf', 'bmp', 'cgm', 'css', 'shtml', 'html', 'htm', 'zip', 'xml', 'ppt', 'pptx', 'tar', 'ez', 'ics', 'mobi', 'msg', 'pub', 'eps', 'odt', 'mp3', 'm4a', 'm4v', 'wma', 'ogg', 'flac', 'wav', 'aif', 'aifc', 'aiff', 'mp4', 'mov', 'avi', 'mkv', 'mpeg', 'mpg', 'wmv' );
// @formatter:on
/**
* @inheritDoc
*
* @since 1.6.0
*/
public function set_header( $name, $value ) {
$name = sanitize_text_field( $name );
$this->body['headers'][ $name ] = WP::sanitize_value( $value );
}
/**
* Set the From information for an email.
*
* @since 1.6.0
*
* @param string $email
* @param string $name
*/
public function set_from( $email, $name ) {
if ( ! filter_var( $email, FILTER_VALIDATE_EMAIL ) ) {
return;
}
$this->body['sender'] = array(
'email' => $email,
'name' => ! empty( $name ) ? WP::sanitize_value( $name ) : '',
);
}
/**
* Set email recipients: to, cc, bcc.
*
* @since 1.6.0
*
* @param array $recipients
*/
public function set_recipients( $recipients ) {
if ( empty( $recipients ) ) {
return;
}
// Allow for now only these recipient types.
$default = array( 'to', 'cc', 'bcc' );
$data = array();
foreach ( $recipients as $type => $emails ) {
if (
! in_array( $type, $default, true ) ||
empty( $emails ) ||
! is_array( $emails )
) {
continue;
}
$data[ $type ] = array();
// Iterate over all emails for each type.
// There might be multiple cc/to/bcc emails.
foreach ( $emails as $email ) {
$holder = array();
$addr = isset( $email[0] ) ? $email[0] : false;
$name = isset( $email[1] ) ? $email[1] : false;
if ( ! filter_var( $addr, FILTER_VALIDATE_EMAIL ) ) {
continue;
}
$holder['email'] = $addr;
if ( ! empty( $name ) ) {
$holder['name'] = $name;
}
array_push( $data[ $type ], $holder );
}
}
foreach ( $data as $type => $type_recipients ) {
$this->body[ $type ] = $type_recipients;
}
}
/**
* @inheritDoc
*
* @since 1.6.0
*/
public function set_subject( $subject ) {
$this->body['subject'] = $subject;
}
/**
* Set email content.
*
* @since 1.6.0
*
* @param string|array $content
*/
public function set_content( $content ) {
if ( empty( $content ) ) {
return;
}
if ( is_array( $content ) ) {
if ( ! empty( $content['text'] ) ) {
$this->body['textContent'] = $content['text'];
}
if ( ! empty( $content['html'] ) ) {
$this->body['htmlContent'] = $content['html'];
}
} else {
if ( $this->phpmailer->ContentType === 'text/plain' ) {
$this->body['textContent'] = $content;
} else {
$this->body['htmlContent'] = $content;
}
}
}
/**
* Doesn't support this.
*
* @since 1.6.0
*
* @param string $email
*/
public function set_return_path( $email ) {
}
/**
* Set the Reply To headers if not set already.
*
* @since 1.6.0
*
* @param array $emails
*/
public function set_reply_to( $emails ) {
if ( empty( $emails ) ) {
return;
}
$data = array();
foreach ( $emails as $user ) {
$holder = array();
$addr = isset( $user[0] ) ? $user[0] : false;
$name = isset( $user[1] ) ? $user[1] : false;
if ( ! filter_var( $addr, FILTER_VALIDATE_EMAIL ) ) {
continue;
}
$holder['email'] = $addr;
if ( ! empty( $name ) ) {
$holder['name'] = $name;
}
$data[] = $holder;
}
if ( ! empty( $data ) ) {
$this->body['replyTo'] = $data[0];
}
}
/**
* Set attachments for an email.
*
* @since 1.6.0
*
* @param array $attachments The array of attachments data.
*/
public function set_attachments( $attachments ) {
if ( empty( $attachments ) ) {
return;
}
foreach ( $attachments as $attachment ) {
$ext = pathinfo( $attachment[1], PATHINFO_EXTENSION );
if ( ! in_array( $ext, $this->allowed_attach_ext, true ) ) {
continue;
}
$file = $this->get_attachment_file_content( $attachment );
if ( $file === false ) {
continue;
}
$this->body['attachment'][] = [
'name' => $this->get_attachment_file_name( $attachment ),
'content' => base64_encode( $file ), // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode
];
}
}
/**
* Get the email body.
*
* @since 1.6.0
*
* @return SendSmtpEmail
*/
public function get_body() {
/**
* Filters Sendinblue email body.
*
* @since 3.5.0
*
* @param array $body Email body.
*/
$body = apply_filters( 'wp_mail_smtp_providers_sendinblue_mailer_get_body', $this->body );
return new SendSmtpEmail( $body );
}
/**
* Use a library to send emails.
*
* @since 1.6.0
*/
public function send() {
try {
$api = new Api( $this->connection );
$response = $api->get_smtp_client()->sendTransacEmail( $this->get_body() );
DebugEvents::add_debug(
esc_html__( 'An email request was sent to the Sendinblue 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();
}
}
/**
* 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
*
* @param CreateSmtpEmail $response The Sendinblue Email object.
*/
protected function process_response( $response ) {
$this->response = $response;
if (
is_a( $response, 'WPMailSMTP\Vendor\SendinBlue\Client\Model\CreateSmtpEmail' ) &&
method_exists( $response, 'getMessageId' )
) {
$this->phpmailer->MessageID = $response->getMessageId();
$this->verify_sent_status = true;
}
}
/**
* Check whether the email was sent.
*
* @since 1.6.0
*
* @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', $is_sent, $this->mailer );
}
/**
* @inheritdoc
*
* @since 1.6.0
*/
public function get_debug_info() {
$mailjet_text[] = '<strong>API Key:</strong> ' . ( $this->is_mailer_complete() ? 'Yes' : 'No' );
return implode( '<br>', $mailjet_text );
}
/**
* @inheritdoc
*
* @since 1.6.0
*/
public function is_mailer_complete() {
$options = $this->connection_options->get_group( $this->mailer );
// API key is the only required option.
if ( ! empty( $options['api_key'] ) ) {
return true;
}
return false;
}
}

View File

@@ -0,0 +1,175 @@
<?php
namespace WPMailSMTP\Providers\Sendinblue;
use WPMailSMTP\ConnectionInterface;
use WPMailSMTP\Providers\OptionsAbstract;
/**
* Class Options.
*
* @since 1.6.0
*/
class Options extends OptionsAbstract {
/**
* Mailer slug.
*
* @since 1.6.0
*/
const SLUG = 'sendinblue';
/**
* Options constructor.
*
* @since 1.6.0
* @since 2.3.0 Added supports parameter.
*
* @param ConnectionInterface $connection The Connection object.
*/
public function __construct( $connection = null ) {
if ( is_null( $connection ) ) {
$connection = wp_mail_smtp()->get_connections_manager()->get_primary_connection();
}
$description = sprintf(
wp_kses( /* translators: %1$s - URL to sendinblue.com site. */
__( '<strong><a href="%1$s" target="_blank" rel="noopener noreferrer">Sendinblue</a> is one of our recommended mailers.</strong> It\'s a transactional email provider with scalable price plans, so it\'s suitable for any size of business.<br><br>If you\'re just starting out, you can use Sendinblue\'s free plan to send up to 300 emails a day. You don\'t need to use a credit card to try it out. When you\'re ready, you can upgrade to a higher plan to increase your sending limits.', 'wp-mail-smtp' ) .
'<br><br>' .
/* translators: %2$s - URL to wpmailsmtp.com doc. */
__( 'To get started, read our <a href="%2$s" target="_blank" rel="noopener noreferrer">Sendinblue documentation</a>.', 'wp-mail-smtp' ),
[
'strong' => true,
'br' => true,
'a' => [
'href' => true,
'rel' => true,
'target' => true,
],
]
),
'https://wpmailsmtp.com/go/sendinblue/',
esc_url( wp_mail_smtp()->get_utm_url( 'https://wpmailsmtp.com/docs/how-to-set-up-the-sendinblue-mailer-in-wp-mail-smtp/', 'Sendinblue documentation' ) )
);
$api_key = $connection->get_options()->get( self::SLUG, 'api_key' );
if ( empty( $api_key ) ) {
$description .= sprintf(
'</p><p class="buttonned"><a href="%1$s" target="_blank" rel="noopener noreferrer" class="wp-mail-smtp-btn wp-mail-smtp-btn-md wp-mail-smtp-btn-blueish">%2$s</a></p>',
'https://wpmailsmtp.com/go/sendinblue/',
esc_html__( 'Get Sendinblue Now (Free)', 'wp-mail-smtp' )
);
}
$description .= '<p class="wp-mail-smtp-tooltip">' .
esc_html__( 'Transparency and Disclosure', 'wp-mail-smtp' ) .
'<span class="wp-mail-smtp-tooltip-text">' .
esc_html__( 'We believe in full transparency. The Sendinblue links above are tracking links as part of our partnership with Sendinblue. We can recommend just about any SMTP service, but we only recommend products that we believe will add value to our users.', 'wp-mail-smtp' ) .
'</span></p>';
parent::__construct(
[
'logo_url' => wp_mail_smtp()->assets_url . '/images/providers/sendinblue.svg',
'slug' => self::SLUG,
'title' => esc_html__( 'Sendinblue', 'wp-mail-smtp' ),
'php' => '5.6',
'description' => $description,
'supports' => [
'from_email' => true,
'from_name' => true,
'return_path' => false,
'from_email_force' => true,
'from_name_force' => true,
],
'recommended' => true,
],
$connection
);
}
/**
* Output the mailer provider options.
*
* @since 1.6.0
*/
public function display_options() {
// Do not display options if PHP version is not correct.
if ( ! $this->is_php_correct() ) {
$this->display_php_warning();
return;
}
?>
<!-- API Key -->
<div id="wp-mail-smtp-setting-row-<?php echo esc_attr( $this->get_slug() ); ?>-client_id"
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( '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' ) ) : ?>
<input type="text" disabled value="****************************************"
id="wp-mail-smtp-setting-<?php echo esc_attr( $this->get_slug() ); ?>-api_key"
/>
<?php $this->display_const_set_message( 'WPMS_SENDINBLUE_API_KEY' ); ?>
<?php else : ?>
<input type="password" spellcheck="false"
name="wp-mail-smtp[<?php echo esc_attr( $this->get_slug() ); ?>][api_key]"
value="<?php echo esc_attr( $this->connection_options->get( $this->get_slug(), 'api_key' ) ); ?>"
id="wp-mail-smtp-setting-<?php echo esc_attr( $this->get_slug() ); ?>-api_key"
/>
<?php endif; ?>
<p class="desc">
<?php
printf( /* translators: %s - link to get an API Key. */
esc_html__( 'Follow this link to get an API Key: %s.', 'wp-mail-smtp' ),
'<a href="https://account.sendinblue.com/advanced/api" target="_blank" rel="noopener noreferrer">' .
esc_html__( 'Get v3 API Key', 'wp-mail-smtp' ) .
'</a>'
);
?>
</p>
</div>
</div>
<!-- Sending Domain -->
<div id="wp-mail-smtp-setting-row-<?php echo esc_attr( $this->get_slug() ); ?>-domain" 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() ); ?>-domain"><?php esc_html_e( 'Sending Domain', 'wp-mail-smtp' ); ?></label>
</div>
<div class="wp-mail-smtp-setting-field">
<input name="wp-mail-smtp[<?php echo esc_attr( $this->get_slug() ); ?>][domain]" type="text"
value="<?php echo esc_attr( $this->connection_options->get( $this->get_slug(), 'domain' ) ); ?>"
<?php echo $this->connection_options->is_const_defined( $this->get_slug(), 'domain' ) ? 'disabled' : ''; ?>
id="wp-mail-smtp-setting-<?php echo esc_attr( $this->get_slug() ); ?>-domain" spellcheck="false"
/>
<p class="desc">
<?php
printf(
wp_kses(
/* translators: %s - URL to Sendinblue documentation on wpmailsmtp.com */
__( 'Please input the sending domain/subdomain you configured in your Sendinblue dashboard. More information can be found in our <a href="%s" target="_blank" rel="noopener noreferrer">Sendinblue documentation</a>.', 'wp-mail-smtp' ),
[
'br' => [],
'a' => [
'href' => [],
'rel' => [],
'target' => [],
],
]
),
esc_url( wp_mail_smtp()->get_utm_url( 'https://wpmailsmtp.com/docs/how-to-set-up-the-sendinblue-mailer-in-wp-mail-smtp/#setup-smtp', 'Sendinblue documentation' ) )
);
?>
</p>
</div>
</div>
<?php
}
}