Come modifico i parametri PHPMailer per inviare le mail da un altro SMTP server

La funzione wp_mail() utilizza la classe PHPMailer per inviare le email attraverso il PHP. Attrverso l’action hook phpmailer_init è possibile modificare i parametri come hostname, porta, tipo di autenticazione, user e pass. Il codice va inserito nel file functions.php del tema.

function sg_send_smtp_email( $phpmailer ) {
	$phpmailer->isSMTP();
	$phpmailer->Host       = 'smtp.example.com';
	$phpmailer->SMTPAuth   = true;
	$phpmailer->Port       = '25';
	$phpmailer->Username   = 'user@example.com';
	$phpmailer->Password   = 'smtp password';
	$phpmailer->SMTPSecure = 'tls'; //ssl, starttls
	$phpmailer->From       = 'website@example.com';
	$phpmailer->FromName   = 'e.g Website Name';

	if(WP_DEBUG){
		add_action('wp_mail_failed', function ($error) {
			error_log($error->get_error_message());
		});
	}
}
add_action( 'phpmailer_init', 'sg_send_smtp_email' );

Rispondi