How to Send Noptin Emails Through a Dedicated SMTP Service

How to Send Noptin Emails Through a Dedicated SMTP Service

By default, Noptin sends emails through WordPress’s normal wp_mail() function.

That works fine for most sites.

But if you’re already using an SMTP plugin (like WP Mail SMTP or FluentSMTP) for your transactional emails, you may not want Noptin newsletters and automations sharing that same connection. Transactional and marketing emails have different sending volumes, reputation requirements, and deliverability needs. Mixing them on one SMTP account can hurt both.

This guide explains how to route Noptin emails through a dedicated SMTP provider. This setup allows you to use a separate service without affecting the rest of your WordPress email settings.

When You’d Want This

  • Your site already uses an SMTP plugin for transactional emails (contact forms, WooCommerce orders, password resets, membership notifications).
  • You want Noptin newsletters, broadcasts, and automations to use a high-volume email service like Amazon SES, Mailgun, SendGrid, Brevo, or Postmark.
  • You want to keep your transactional email reputation separate from your marketing email reputation.
  • Your transactional SMTP account has sending limits that Noptin campaigns would hit.

How It Works

Noptin includes a filter called noptin_email_sending_function that runs before every email. You can use this filter to replace the default wp_mail function with a custom one. This custom function sends your Noptin emails directly via a dedicated SMTP provider.

The rest of WordPress (contact forms, WooCommerce, admin notifications) is never touched.

Step 1: Get Your SMTP Details

From your SMTP provider, collect these details:

SettingDescription
SMTP HostThe provider’s outbound mail server
SMTP PortUsually 587 (STARTTLS) or 465 (SSL)
Encryptiontls for port 587, ssl for port 465
UsernameYour SMTP username (not always your email address)
PasswordYour SMTP password or API key

Amazon SES example:

SettingValue
Hostemail-smtp.us-east-1.amazonaws.com
Port587
Encryptiontls
Username(your SES SMTP username — not your AWS access key)
Password(your SES SMTP password)

Replace us-east-1 with your actual AWS region. You can generate SES SMTP credentials from the SES console under SMTP settings. Note that SES SMTP credentials are separate from regular AWS IAM access keys.

Other providers:

ProviderHostPort
Mailgunsmtp.mailgun.org587
SendGridsmtp.sendgrid.net587
Brevosmtp-relay.brevo.com587
Postmarksmtp.postmarkapp.com587

Before sending with Amazon SES: Verify your sending domain or email address in the SES console. Unverified senders will be rejected. If your account is still in the SES sandbox, you can only send to verified recipient addresses until you request production access.

Before sending with Amazon SES:

Verify your sending domain or email address in the SES console. Unverified senders will be rejected. If your account is still in the SES sandbox, you can only send to verified recipient addresses until you request production access.

Step 2: Add Your SMTP Credentials to wp-config.php

Open wp-config.php and add the following constants above the line that reads:

/* That's all, stop editing! Happy publishing. */
// Noptin dedicated SMTP settings
define( 'NOPTIN_DEDICATED_SMTP_HOST',     'email-smtp.us-east-1.amazonaws.com' );
define( 'NOPTIN_DEDICATED_SMTP_PORT',     587 );
define( 'NOPTIN_DEDICATED_SMTP_SECURE',   'tls' );
define( 'NOPTIN_DEDICATED_SMTP_USERNAME', 'your-smtp-username' );
define( 'NOPTIN_DEDICATED_SMTP_PASSWORD', 'your-smtp-password' );

If your provider requires port 465 with SSL instead:

define( 'NOPTIN_DEDICATED_SMTP_PORT',   465 );
define( 'NOPTIN_DEDICATED_SMTP_SECURE', 'ssl' );

Storing credentials in wp-config.php keeps them out of the database and out of your theme or plugin files, which makes them harder to accidentally expose or overwrite during updates.

Step 3: Add the Sending Code

Next, add the following PHP code snippet to your site. This code uses Noptin filters to manage SMTP settings, headers, and attachments. It also handles email formatting, reply-to addresses, and error logging automatically.

<?php

/**
 * Replace Noptin's default wp_mail() sender with our own SMTP sender.
 *
 * This only affects emails sent by Noptin.
 */
add_filter( 'noptin_email_sending_function', 'my_noptin_use_dedicated_smtp', 10, 2 );

function my_noptin_use_dedicated_smtp( $sending_function, $email_args ) {
	unset( $email_args );

	// Do nothing if the SMTP constants have not been configured.
	if (
		! defined( 'NOPTIN_DEDICATED_SMTP_HOST' ) ||
		! defined( 'NOPTIN_DEDICATED_SMTP_USERNAME' ) ||
		! defined( 'NOPTIN_DEDICATED_SMTP_PASSWORD' )
	) {
		return $sending_function;
	}

	return 'my_noptin_send_via_dedicated_smtp';
}

/**
 * Sends a Noptin email through a dedicated SMTP provider.
 */
function my_noptin_send_via_dedicated_smtp( $to, $subject, $message, $headers = '', $attachments = array() ) {

	my_noptin_load_phpmailer();

	$recipients = array_filter( wp_parse_list( $to ), 'is_email' );

	if ( empty( $recipients ) ) {
		return false;
	}

	$parsed_headers = my_noptin_parse_email_headers( $headers );

	try {
		$mailer = new PHPMailer\PHPMailer\PHPMailer( true );

		$mailer->isSMTP();
		$mailer->Host       = NOPTIN_DEDICATED_SMTP_HOST;
		$mailer->SMTPAuth   = true;
		$mailer->Username   = NOPTIN_DEDICATED_SMTP_USERNAME;
		$mailer->Password   = NOPTIN_DEDICATED_SMTP_PASSWORD;
		$mailer->Port       = defined( 'NOPTIN_DEDICATED_SMTP_PORT' ) ? (int) NOPTIN_DEDICATED_SMTP_PORT : 587;
		$mailer->SMTPSecure = defined( 'NOPTIN_DEDICATED_SMTP_SECURE' ) ? NOPTIN_DEDICATED_SMTP_SECURE : 'tls';

		$mailer->CharSet = get_bloginfo( 'charset' );
		$mailer->Timeout = 30;

		$from_email = \Hizzle\Noptin\Emails\Sender::get_from_email();
		$from_name  = \Hizzle\Noptin\Emails\Sender::get_from_name();

		$mailer->setFrom( $from_email, $from_name, false );

		foreach ( $recipients as $recipient ) {
			$mailer->addAddress( $recipient );
		}

		$reply_to = \Hizzle\Noptin\Emails\Sender::get_reply_to();

		if ( is_email( $reply_to ) ) {
			$mailer->addReplyTo( $reply_to, $from_name );
		}

		$is_html = my_noptin_email_is_html( $parsed_headers );

		$mailer->Subject = $subject;
		$mailer->Body    = $message;

		if ( $is_html ) {
			$mailer->isHTML( true );
			$mailer->AltBody = wp_strip_all_tags(
				str_replace(
					array( '<br>', '<br/>', '<br />' ),
					"\n",
					$message
				)
			);
		} else {
			$mailer->isHTML( false );
		}

		my_noptin_add_custom_headers( $mailer, $parsed_headers );

		foreach ( (array) $attachments as $attachment ) {
			if ( is_string( $attachment ) && file_exists( $attachment ) && is_readable( $attachment ) ) {
				$mailer->addAttachment( $attachment );
			}
		}

		return $mailer->send();

	} catch ( Throwable $e ) {
		if ( function_exists( 'log_noptin_message' ) ) {
			log_noptin_message(
				sprintf(
					'Failed sending Noptin email via dedicated SMTP: %s',
					$e->getMessage()
				)
			);
		}

		return false;
	}
}

/**
 * Load WordPress' bundled PHPMailer classes.
 */
function my_noptin_load_phpmailer() {

	if ( class_exists( 'PHPMailer\PHPMailer\PHPMailer' ) ) {
		return;
	}

	require_once ABSPATH . WPINC . '/PHPMailer/PHPMailer.php';
	require_once ABSPATH . WPINC . '/PHPMailer/SMTP.php';
	require_once ABSPATH . WPINC . '/PHPMailer/Exception.php';
}

/**
 * Parse email headers into a normalized array.
 */
function my_noptin_parse_email_headers( $headers ) {

	$parsed = array();

	if ( empty( $headers ) ) {
		return $parsed;
	}

	if ( is_string( $headers ) ) {
		$headers = str_replace( "\r\n", "\n", $headers );
		$headers = explode( "\n", $headers );
	}

	foreach ( (array) $headers as $header ) {
		if ( false === strpos( $header, ':' ) ) {
			continue;
		}

		list( $name, $value ) = explode( ':', $header, 2 );

		$name  = strtolower( trim( $name ) );
		$value = trim( $value );

		if ( '' === $name || '' === $value ) {
			continue;
		}

		if ( ! isset( $parsed[ $name ] ) ) {
			$parsed[ $name ] = array();
		}

		$parsed[ $name ][] = $value;
	}

	return $parsed;
}

/**
 * Determine whether the email should be sent as HTML.
 */
function my_noptin_email_is_html( $parsed_headers ) {
	$content_type = \Hizzle\Noptin\Emails\Sender::get_content_type( 'text/plain' );

	if ( false !== stripos( $content_type, 'text/html' ) ) {
		return true;
	}

	if ( ! empty( $parsed_headers['content-type'][0] ) ) {
		return false !== stripos( $parsed_headers['content-type'][0], 'text/html' );
	}

	return false;
}

/**
 * Add custom headers generated by Noptin, such as List-Unsubscribe.
 */
function my_noptin_add_custom_headers( $mailer, $parsed_headers ) {

	$skip_headers = array(
		'to',
		'from',
		'subject',
		'reply-to',
		'content-type',
		'mime-version',
	);

	foreach ( $parsed_headers as $name => $values ) {
		if ( in_array( $name, $skip_headers, true ) ) {
			continue;
		}

		foreach ( $values as $value ) {
			$mailer->addCustomHeader( $name, $value );
		}
	}
}

Step 4: Test the Setup

Send a test email from Noptin (Newsletters → Send Test, or trigger an automation to yourself), then verify:

  1. The email arrives in the inbox.
  2. Sender name and sender email are correct.
  3. The reply-to address is correct.
  4. The email is sent through your dedicated SMTP provider.
  5. Other WordPress emails still go through your normal SMTP setup.

That last check is the most important one. If transactional emails are broken, double-check that your existing SMTP plugin is still active and that the constants you added to wp-config.php are syntactically correct PHP.

Troubleshooting

Emails fail to send entirely

First, confirm that your SMTP host, username, password, port, and encryption type are correct.

For Amazon SES, make sure you are using SMTP credentials, not your normal AWS access key and secret key. Amazon SES SMTP credentials are separate from regular AWS access keys.

Amazon SES Says the Sender Is Not Verified

Verify the domain or email address you are sending from.

Most SMTP services require you to verify a domain before it can be used as the sender address. If your account is still in the sandbox, you may also need to verify the recipient address.

Noptin emails fail but other WordPress emails still work

That usually means your normal SMTP plugin is working, but the dedicated Noptin SMTP constants are wrong.

Check these values in wp-config.php:

NOPTIN_DEDICATED_SMTP_HOST
NOPTIN_DEDICATED_SMTP_PORT
NOPTIN_DEDICATED_SMTP_SECURE
NOPTIN_DEDICATED_SMTP_USERNAME
NOPTIN_DEDICATED_SMTP_PASSWORD

Leave a Reply

Your email address will not be published. Required fields are marked *