Initial commit
This commit is contained in:
@ -0,0 +1,93 @@
|
||||
<?php
|
||||
|
||||
namespace WPMailSMTP\Providers\Sendinblue;
|
||||
|
||||
/**
|
||||
* Class Api is a wrapper for Sendinblue library with handy methods.
|
||||
*
|
||||
* @since 1.6.0
|
||||
*/
|
||||
class Api {
|
||||
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
public function __construct() {
|
||||
|
||||
$this->options = \WPMailSMTP\Options::init()->get_group( Options::SLUG );
|
||||
}
|
||||
|
||||
/**
|
||||
* Configure API key authorization: api-key.
|
||||
*
|
||||
* @since 1.6.0
|
||||
*
|
||||
* @return \SendinBlue\Client\Configuration
|
||||
*/
|
||||
protected function get_api_config() {
|
||||
|
||||
return \SendinBlue\Client\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 \SendinBlue\Client\Api\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 \SendinBlue\Client\Api\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 \SendinBlue\Client\Api\SMTPApi( 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'] );
|
||||
}
|
||||
}
|
@ -0,0 +1,393 @@
|
||||
<?php
|
||||
|
||||
namespace WPMailSMTP\Providers\Sendinblue;
|
||||
|
||||
use WPMailSMTP\Debug;
|
||||
use WPMailSMTP\Providers\MailerAbstract;
|
||||
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
|
||||
|
||||
/**
|
||||
* Mailer constructor.
|
||||
*
|
||||
* @since 1.6.0
|
||||
*
|
||||
* @param \WPMailSMTP\MailCatcher $phpmailer
|
||||
*/
|
||||
public function __construct( $phpmailer ) {
|
||||
|
||||
parent::__construct( $phpmailer );
|
||||
|
||||
if ( ! $this->is_php_compatible() ) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @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
|
||||
*/
|
||||
public function set_attachments( $attachments ) {
|
||||
|
||||
if ( empty( $attachments ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
foreach ( $attachments as $attachment ) {
|
||||
$file = false;
|
||||
|
||||
/*
|
||||
* We are not using WP_Filesystem API as we can't reliably work with it.
|
||||
* It is not always available, same as credentials for FTP.
|
||||
*/
|
||||
try {
|
||||
if ( is_file( $attachment[0] ) && is_readable( $attachment[0] ) ) {
|
||||
$ext = pathinfo( $attachment[0], PATHINFO_EXTENSION );
|
||||
|
||||
if ( in_array( $ext, $this->allowed_attach_ext, true ) ) {
|
||||
$file = file_get_contents( $attachment[0] ); // phpcs:ignore
|
||||
}
|
||||
}
|
||||
}
|
||||
catch ( \Exception $e ) {
|
||||
$file = false;
|
||||
}
|
||||
|
||||
if ( $file === false ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$this->body['attachment'][] = array(
|
||||
'name' => $attachment[2],
|
||||
'content' => base64_encode( $file ),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*
|
||||
* @since 1.6.0
|
||||
*
|
||||
* @return \SendinBlue\Client\Model\SendSmtpEmail
|
||||
*/
|
||||
public function get_body() {
|
||||
|
||||
return new \SendinBlue\Client\Model\SendSmtpEmail( $this->body );
|
||||
}
|
||||
|
||||
/**
|
||||
* Use a library to send emails.
|
||||
*
|
||||
* @since 1.6.0
|
||||
*/
|
||||
public function send() {
|
||||
|
||||
try {
|
||||
$api = new Api();
|
||||
|
||||
$response = $api->get_smtp_client()->sendTransacEmail( $this->get_body() );
|
||||
|
||||
$this->process_response( $response );
|
||||
}
|
||||
catch ( \SendinBlue\Client\ApiException $e ) {
|
||||
$error = json_decode( $e->getResponseBody() );
|
||||
if ( json_last_error() === JSON_ERROR_NONE ) {
|
||||
Debug::set(
|
||||
'Mailer: Sendinblue' . "\r\n" .
|
||||
'[' . sanitize_key( $error->code ) . ']: ' . esc_html( $error->message )
|
||||
);
|
||||
}
|
||||
}
|
||||
catch ( \Exception $e ) {
|
||||
Debug::set(
|
||||
'Mailer: Sendinblue' . "\r\n" .
|
||||
$e->getMessage()
|
||||
);
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 \SendinBlue\Client\Model\CreateSmtpEmail $response
|
||||
*/
|
||||
protected function process_response( $response ) {
|
||||
|
||||
$this->response = $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether the email was sent.
|
||||
*
|
||||
* @since 1.6.0
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function is_email_sent() {
|
||||
|
||||
$is_sent = false;
|
||||
|
||||
if ( $this->response instanceof \SendinBlue\Client\Model\CreateSmtpEmail ) {
|
||||
$is_sent = $this->response->valid();
|
||||
}
|
||||
|
||||
// Clear debug messages if email is successfully sent.
|
||||
if ( $is_sent ) {
|
||||
Debug::clear();
|
||||
}
|
||||
|
||||
return $is_sent;
|
||||
}
|
||||
|
||||
/**
|
||||
* @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->options->get_group( $this->mailer );
|
||||
|
||||
// API key is the only required option.
|
||||
if ( ! empty( $options['api_key'] ) ) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
@ -0,0 +1,119 @@
|
||||
<?php
|
||||
|
||||
namespace WPMailSMTP\Providers\Sendinblue;
|
||||
|
||||
use WPMailSMTP\Providers\OptionsAbstract;
|
||||
use WPMailSMTP\Options as PluginOptions;
|
||||
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
public function __construct() {
|
||||
|
||||
$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 a recommended transactional email service.</strong> Founded in 2012, they serve 80,000+ growing companies around the world and send over 30 million emails each day. They understand that transactional emails are the heart of your customer relationships. Their email deliverability experts are constantly at work optimizing the reliability and speed of their SMTP infrastructure. Sendinblue provides users 300 free emails per day.', 'wp-mail-smtp' ) .
|
||||
'<br><br>' .
|
||||
/* translators: %2$s - URL to wpmailsmtp.com doc. */
|
||||
__( 'Read our <a href="%2$s" target="_blank" rel="noopener noreferrer">Sendinblue documentation</a> to learn how to configure Sendinblue and improve your email deliverability.', 'wp-mail-smtp' ),
|
||||
array(
|
||||
'br' => true,
|
||||
'strong' => true,
|
||||
'a' => array(
|
||||
'href' => true,
|
||||
'rel' => true,
|
||||
'target' => true,
|
||||
),
|
||||
)
|
||||
),
|
||||
'https://wpmailsmtp.com/go/sendinblue/',
|
||||
'https://wpmailsmtp.com/docs/how-to-set-up-the-sendinblue-mailer-in-wp-mail-smtp'
|
||||
);
|
||||
|
||||
$api_key = PluginOptions::init()->get( self::SLUG, 'api_key' );
|
||||
|
||||
if ( empty( $api_key ) ) {
|
||||
$description .= '</p><p class="buttonned"><a href="https://wpmailsmtp.com/go/sendinblue/" target="_blank" rel="noopener noreferrer" class="wp-mail-smtp-btn wp-mail-smtp-btn-md wp-mail-smtp-btn-blueish">' .
|
||||
esc_html__( 'Get Sendinblue Now (Free)', 'wp-mail-smtp' ) .
|
||||
'</a></p>';
|
||||
}
|
||||
|
||||
parent::__construct(
|
||||
array(
|
||||
'logo_url' => wp_mail_smtp()->assets_url . '/images/providers/sendinblue.svg',
|
||||
'slug' => self::SLUG,
|
||||
'title' => esc_html__( 'Sendinblue', 'wp-mail-smtp' ),
|
||||
'description' => $description,
|
||||
'recommended' => true,
|
||||
'php' => '5.6',
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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->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->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 - sendinblue.com 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>
|
||||
|
||||
<?php
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user