Mailjet Delivery for All WordPress Email
Delivers all WordPress emails through Mailjet's Send API v3.1, a reliable alternative to SMTP that also gives you Mailjet's delivery statistics.
Opens WPIDE on
Code
add_filter( 'pre_wp_mail', function ( $return, $atts ) {
if ( null !== $return || ! defined( 'WPIDE_MAILJET_KEY' ) || ! defined( 'WPIDE_MAILJET_SECRET' ) || ! empty( $atts['attachments'] ) ) {
return $return;
}
$address = function ( $value ) {
$value = trim( (string) $value );
if ( preg_match( '/^(.*)<([^>]+)>$/', $value, $m ) ) {
$name = trim( $m[1], " \"'" );
return $name ? array( 'Email' => trim( $m[2] ), 'Name' => $name ) : array( 'Email' => trim( $m[2] ) );
}
return array( 'Email' => $value );
};
$lines = is_array( $atts['headers'] ) ? $atts['headers'] : explode( "\n", str_replace( "\r\n", "\n", (string) $atts['headers'] ) );
$h = array( 'from' => '', 'reply-to' => array(), 'cc' => array(), 'bcc' => array(), 'content-type' => '' );
foreach ( $lines as $line ) {
if ( false === strpos( (string) $line, ':' ) ) {
continue;
}
list( $name, $value ) = array_map( 'trim', explode( ':', $line, 2 ) );
$name = strtolower( $name );
if ( in_array( $name, array( 'reply-to', 'cc', 'bcc' ), true ) ) {
$h[ $name ] = array_merge( $h[ $name ], array_filter( array_map( 'trim', explode( ',', $value ) ) ) );
} elseif ( isset( $h[ $name ] ) ) {
$h[ $name ] = $value;
}
}
$to = is_array( $atts['to'] ) ? $atts['to'] : explode( ',', (string) $atts['to'] );
$is_html = false !== stripos( $h['content-type'], 'text/html' ) || 'text/html' === apply_filters( 'wp_mail_content_type', 'text/plain' );
$message = array(
'From' => $h['from'] ? $address( $h['from'] ) : array( 'Email' => apply_filters( 'wp_mail_from', get_option( 'admin_email' ) ), 'Name' => apply_filters( 'wp_mail_from_name', 'WordPress' ) ),
'To' => array_map( $address, array_filter( array_map( 'trim', $to ) ) ),
'Subject' => (string) $atts['subject'],
);
$message[ $is_html ? 'HTMLPart' : 'TextPart' ] = (string) $atts['message'];
if ( $h['cc'] ) {
$message['Cc'] = array_map( $address, $h['cc'] );
}
if ( $h['bcc'] ) {
$message['Bcc'] = array_map( $address, $h['bcc'] );
}
if ( $h['reply-to'] ) {
$message['ReplyTo'] = $address( $h['reply-to'][0] );
}
$response = wp_remote_post( 'https://api.mailjet.com/v3.1/send', array(
'timeout' => 15,
'headers' => array( 'Authorization' => 'Basic ' . base64_encode( WPIDE_MAILJET_KEY . ':' . WPIDE_MAILJET_SECRET ), 'Content-Type' => 'application/json' ),
'body' => wp_json_encode( array( 'Messages' => array( $message ) ) ),
) );
$code = (int) wp_remote_retrieve_response_code( $response );
if ( $code >= 200 && $code < 300 ) {
do_action( 'wp_mail_succeeded', $atts );
return true;
}
$error = is_wp_error( $response ) ? $response->get_error_message() : 'Mailjet returned HTTP ' . $code . ': ' . wp_remote_retrieve_body( $response );
do_action( 'wp_mail_failed', new WP_Error( 'wp_mail_failed', $error, $atts ) );
return false;
}, 10, 2 );
Notes
Define WPIDE_MAILJET_KEY and WPIDE_MAILJET_SECRET in wp-config.php and send from a sender validated in Mailjet; emails with attachments fall back to the normal mailer.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.