diff --git a/src/Mailer.php b/src/Mailer.php index 348077d3f223534eeacab5092aeda7e99498ac1f..e1a239a736192ce455c34266997111124b514c45 100644 --- a/src/Mailer.php +++ b/src/Mailer.php @@ -75,51 +75,54 @@ class Mailer { /** @return void */ public function send() { foreach ( $this->get_emails() as $email ) { - add_filter( - 'wp_mail_from', - $from_cb = static function () use ( $email ) { - if ( ! empty( $email->get_from() ) ) { - return $email->get_from(); + if ( $email->get_is_enable() ) { + add_filter( + 'wp_mail_from', + $from_cb = static function ( $from ) use ( $email ) { + if ( ! empty( $email->get_from() ) ) { + return $email->get_from(); + } + + return $from; } - - return 'wordpress@siteurl.pl'; - } - ); - add_filter( - 'wp_mail_from_name', - $from_name_cb = static function () use ( $email ) { - if ( ! empty( $email->get_from_name() ) ) { - return $email->get_from_name(); + ); + add_filter( + 'wp_mail_from_name', + $from_name_cb = static function ( $from_name ) use ( $email ) { + if ( ! empty( $email->get_from_name() ) ) { + return $email->get_from_name(); + } + + return $from_name; } - - return 'wordpress'; - } - ); - add_action( 'wp_mail_failed', [ $this, 'catch_error' ] ); - - try { - $success = wp_mail( - $email->get_recipients(), - $email->get_subject(), - $this->get_email_template( $email ), - $email->get_headers(), - $email->get_attachments() ); - if ( ! $success ) { - throw new MailerException( 'Count not send the mail with wp_mail()' ); - } - } catch ( Exception $e ) { - if ( $e instanceof MailerException ) { - throw $e; - } + add_action( 'wp_mail_failed', [ $this, 'catch_error' ] ); + + try { + $success = wp_mail( + $email->get_recipients(), + $email->get_subject(), + $this->get_email_template( $email ), + $email->get_headers(), + $email->get_attachments() + ); + if ( ! $success ) { + throw new MailerException( 'Count not send the mail with wp_mail()' ); + } + } catch ( Exception $e ) { + if ( $e instanceof MailerException ) { + throw $e; + } - throw new MailerException( sprintf( 'wp_mail() failure. Original error: %s', $e->getMessage() ), 0, $e ); - } finally { - remove_action( 'wp_mail_failed', [ $this, 'catch_error' ], 99999 ); - remove_filter( 'wp_mail_from', $from_cb ); - remove_filter( 'wp_mail_from_name', $from_name_cb ); + throw new MailerException( sprintf( 'wp_mail() failure. Original error: %s', $e->getMessage() ), 0, $e ); + } finally { + remove_action( 'wp_mail_failed', [ $this, 'catch_error' ], 99999 ); + remove_filter( 'wp_mail_from', $from_cb ); + remove_filter( 'wp_mail_from_name', $from_name_cb ); + } } } + } /** @return void */