Action uncanny-learndash-toolkit

phpmailer_init

Fires when PHPMailer is initialized, allowing modification before email sending.

add_action( 'phpmailer_init', $callback, 10, 1 );

Description

Fires when PHPMailer is initialized. This hook allows developers to modify PHPMailer's settings, such as SMTP credentials or custom mail headers, before emails are sent. Pass the PHPMailer instance to customize it.


Usage

add_action( 'phpmailer_init', 'your_function_name', 10, 1 );

Parameters

$this (mixed)
This parameter is an instance of the PHPMailer class itself, allowing direct manipulation of its properties and methods.

Examples

/**
 * Example function to modify PHPMailer settings during initialization.
 * This hook allows you to change various PHPMailer properties before an email is sent.
 * For instance, you might want to set a default sender name or enable debugging.
 *
 * @param PHPMailer $phpmailer The PHPMailer object instance.
 */
function my_custom_phpmailer_init( PHPMailer $phpmailer ) {
	// Set a custom default sender name.
	// This will be used if no 'From' name is explicitly set for an email.
	$phpmailer->FromName = 'My Awesome WordPress Site';

	// Enable SMTP debug output for troubleshooting if needed.
	// Set to 1 for client/server messages, 2 for client/server messages and data.
	// It's generally recommended to disable this in production.
	// $phpmailer->SMTPDebug = 1;

	// You can also override other PHPMailer properties here, for example:
	// $phpmailer->Host = 'smtp.example.com';
	// $phpmailer->Port = 587;
	// $phpmailer->SMTPSecure = 'tls';
	// $phpmailer->SMTPAuth = true;
	// $phpmailer->Username = 'your_smtp_username';
	// $phpmailer->Password = 'your_smtp_password';
}

// Hook into the 'phpmailer_init' action.
// The second parameter '1' indicates that the function expects one argument.
add_action( 'phpmailer_init', 'my_custom_phpmailer_init', 1 );

Placement

This code should be placed in the functions.php file of your active theme, a custom plugin, or using a code snippets plugin.


Source Code

src/includes/class.DisableEmailsPHPMailerMock.php:199

if ( $plugin->options['wp_mail_content_type'] ) {
			$this->phpmailer->ContentType = apply_filters( 'wp_mail_content_type', $this->phpmailer->ContentType );
		}
		if ( $plugin->options['wp_mail_charset'] ) {
			$this->phpmailer->CharSet = apply_filters( 'wp_mail_charset', $this->phpmailer->CharSet );
		}
		if ( $plugin->options['phpmailer_init'] ) {
			do_action( 'phpmailer_init', $this->phpmailer );
		}

		return true;
	}

	/**
	 * set a different From address and potentially, name


Scroll to Top