Skip to content
Snippets Groups Projects
Commit 43426991 authored by Piotr Potrebka's avatar Piotr Potrebka
Browse files

feat: mail from

parent ca4eb5a7
No related branches found
No related tags found
1 merge request!2Devel
Pipeline #153494 passed with stages
in 1 minute and 7 seconds
......@@ -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 */
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment