I need a way to query the sender's email address before delivery is attempted, and then set wp_mail arguments depending on the results.

So something like the following:

  • If sender_email contains @domain.com then continue
  • Else, if sender_email doesn't contain @domain.com, update wp_mail headers before sending

Use case is basically this:

I'm authenticating domain email using Postmark, so email will only send successfully if the sender's address matches an authenticated domain in my Postmark account.

What I want to do is create a check before it tries to send through Postmark, and if it's not an authenticated email address then send using wp_mail (WordPress's default mailing system) instead.

Or, put another way, ONLY send email through Postmark if it matches a specific domain or email address (which we can set manually).

  • Are you using some plugin to connect your WP site with Postmark or have you written custom code for it? The first things that come to my mind are the wp_mail filter and phpmailer_init action that you could perhaps take a look at. Aug 2, 2019 at 21:59
  • Hey @AnttiKoskinen! I'm using Postmark's own plugin, which links with their API. But according to them I can override Postmark's sending by setting wp_mail headers. I just need to run an "if" check to check the sender's email before I do.
    – jongoeson
    Aug 2, 2019 at 22:43

1 Answer 1


Perhpaps wp_mailwould be a suitable filter for this case as it is the first one to fire in wp_mail(). So perhaps something as simple as this might work,

// For reference:
// $atts = array(
//   'to' => string, 
//   'subject' => string, 
//   'message' => string, 
//   'headers' => string or array (I think), 
//   'attachments' => array,
// );
function filter_wp_mail( $atts ) {
  if ( false === strpos( '@domain.com', $atts['to'] ) ) {
    $atts['headers'] .= "Postmark: not valid email\n";
  return $atts;
add_filter( 'wp_mail', 'filter_wp_mail' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.