Skip to content

Commit

Permalink
fix resending HTML emails sending with wrong content-type
Browse files Browse the repository at this point in the history
  • Loading branch information
tobeyadr committed Dec 4, 2024
1 parent 09e8e3d commit 730cf3c
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 6 deletions.
2 changes: 1 addition & 1 deletion admin/views/log-preview.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
'view' => 'log',
'status' => 'quarantine',
'id' => $log_item_id
] ), 'release_email', '_mailhawk_nonce' );
] ), 'retry_email', '_mailhawk_nonce' );

$reject_url = wp_nonce_url( mailhawk_admin_page( [
'view' => 'log',
Expand Down
3 changes: 3 additions & 0 deletions db/email-log.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ public function get_columns() {
'from_address' => '%s',
'subject' => '%s',
'content' => '%s',
'altbody' => '%s',
'headers' => '%s',
'status' => '%s',
'error_code' => '%s',
Expand All @@ -165,6 +166,7 @@ public function get_column_defaults() {
'from_address' => '',
'subject' => '',
'content' => '',
'altbody' => '',
'headers' => '',
'status' => 'sent',
'error_code' => '',
Expand All @@ -188,6 +190,7 @@ public function create_table() {
subject mediumtext NOT NULL,
headers mediumtext NOT NULL,
content longtext NOT NULL,
altbody longtext NOT NULL,
status varchar(20) NOT NULL,
error_code varchar(30) NOT NULL,
error_message text NOT NULL,
Expand Down
18 changes: 17 additions & 1 deletion includes/classes/email-log-item.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use MailHawk\DB\DB;
use MailHawk\Hawk_Mailer;
use MailHawk\Plugin;
use PHPMailer\PHPMailer\PHPMailer;

class Email_Log_Item extends Base_Object {

Expand Down Expand Up @@ -69,12 +70,13 @@ public function retry() {
$headers = [];

foreach ( $this->headers as $header ) {
$headers[] = sprintf( "%s: %s\n", $header[0], $header[1] );
$headers[] = sprintf( "%s: %s", $header[0], $header[1] );
}

Hawk_Mailer::set_log_item_id( $this->get_id() );

add_action( 'wp_mail_failed', [ $this, 'catch_mail_error' ] );
add_action( 'phpmailer_init', [ $this, 'maybe_set_alt_body' ] );

// Mail this thing!
$result = mailhawk_mail( $this->recipients, $this->subject, $this->content, $headers );
Expand All @@ -87,9 +89,23 @@ public function retry() {
] );
}

remove_action( 'wp_mail_failed', [ $this, 'catch_mail_error' ] );
remove_action( 'phpmailer_init', [ $this, 'maybe_set_alt_body' ] );

return $result;
}

/**
* Set the AltBody if there is one
*
* @param PHPMailer $phpmailer
*
* @return void
*/
public function maybe_set_alt_body( PHPMailer &$phpmailer ) {
$phpmailer->AltBody = $this->altbody;
}

/**
* @return mixed|string
*/
Expand Down
5 changes: 5 additions & 0 deletions includes/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -867,6 +867,11 @@ function assess_risk( string $email_address ): int {
// The baseline risk is 0;
$risk = 0;

// Test quarantine system using + synatx
if ( str_contains( $email_address, '+test-quarantine@' ) ) {
return 999;
}

// It's the admin email, dw about it
if ( $email_address === get_option( 'admin_email' ) ){
return 0;
Expand Down
3 changes: 2 additions & 1 deletion includes/hawk-mailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public function send() {
$quarantine = false;

// If a log item ID is set, we're releasing the message
if ( ! self::$log_item_id ){
if ( ! self::$log_item_id ) {

// Check the risk factor of the recipients
foreach ( $recipients as $recipient ) {
Expand Down Expand Up @@ -105,6 +105,7 @@ public function send() {
'from_address' => $this->From,
'subject' => $this->Subject,
'content' => $this->Body,
'altbody' => $this->AltBody,
'headers' => $headers,
'error_code' => '',
'error_message' => '',
Expand Down
10 changes: 9 additions & 1 deletion includes/updater.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ protected function get_updater_name() {
protected function get_available_updates() {
return [
'1.0.1.1',
'1.1.1'
'1.1.1',
'1.3.1'
];
}

Expand All @@ -40,5 +41,12 @@ public function version_1_1_1(){
] );

}

/**
* Add the altbody column
*/
public function version_1_3_1(){
Plugin::instance()->log->create_table();
}
}

4 changes: 2 additions & 2 deletions mailhawk.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Plugin Name: MailHawk
* Plugin URI: https://mailhawk.io
* Description: Send better email that will reach the inbox with MailHawk.
* Version: 1.3
* Version: 1.3.1
* Author: MailHawk Inc.
* Author URI: http://mailhawk.io
* License: GPLv3
Expand All @@ -22,7 +22,7 @@

if ( ! defined( 'ABSPATH' ) ) exit;

define( 'MAILHAWK_VERSION', '1.3' );
define( 'MAILHAWK_VERSION', '1.3.1' );
define( 'MAILHAWK_PREVIOUS_STABLE_VERSION', '1.3' );
define( 'MAILHAWK_LICENSE_SERVER_URL', 'https://mailhawk.io' );

Expand Down

0 comments on commit 730cf3c

Please sign in to comment.