Easy SMTP email settings for WordPress

wp-mail-function

If you are facing problems with wp_mail function in sending emails, the solution is to use an SMTP server rather than relying on the webserver.

WordPress’s email function wp_mail is essentially a wrapper for phpmailer, a popular email class for PHP. WordPress has a little known action hook when phpmailer is initialized, phpmailer_init. This allows you to establish the phpmailer instance as using SMTP.

Here is a code snippet example with comments for each setting to configure WordPress to sent SMTP email:

/**
 * This function will connect wp_mail to your authenticated
 * SMTP server. This improves reliability of wp_mail, and 
 * avoids many potential problems.
 */
 
add_action( 'phpmailer_init', 'send_smtp_email' );

function send_smtp_email( $phpmailer ) {

	// Define that we are sending with SMTP
	$phpmailer->isSMTP();

	// The hostname of the mail server
	$phpmailer->Host = "smtp.example.com";

	// Use SMTP authentication (true|false)
	$phpmailer->SMTPAuth = true;

	// SMTP port number - likely to be 25, 465 or 587
	$phpmailer->Port = "587";

	// Username to use for SMTP authentication
	$phpmailer->Username = "yourusername";

	// Password to use for SMTP authentication
	$phpmailer->Password = "yourpassword";

	// Encryption system to use - ssl or tls
	$phpmailer->SMTPSecure = "tls";

	$phpmailer->From = "you@yourdomail.com";
	$phpmailer->FromName = "Your Name";
}

To use this snippet, you will need to adjust the settings according to your email service requirements. Check with your host.

The snippet, once configured, can be added to your theme’s functions.php file.

Introduction to WordPress Hooks

WordPress hooks enable us to literally hook into parts of the WordPress page life-cycle to retrieve, insert, or modify data, or they allow us to take certain actions behind the scenes.

There are 2 types of WordPress Hooks. They are 1) Action Hooks 2) Filter Hooks

Action Hooks:

Action hooks are designated points in the WordPress core, theme and plugin code where it is possible for outside resources (outside of the scope of where the hook is… either in the core, theme or plugin) to insert additional code and, there by, customize the code to do additional functions they may desire. An example of this is the commonly used wp_head action hook, used by many themes and plugins to inject additional CSS stylesheets, processing code or anything else they require to sit between the <head> and </head> tags of their WordPress theme’s XHTML structure. This is the reason for including wp_head(); in all WordPress themes.

For Example:

<?php
	add_action( 'wp_head', 'custom_actionhook_example' );
	function custom_actionhook_example () {
	   echo '<meta name="description" content="This is the meta description for this page." />' . "";
	} // End custom_actionhook_example()
?>

Filter Hooks:

Filter hooks are used to manipulate output. An example of this would be to add a line or text (or a hyperlink, or a signature sign-off—whatever you’d like) to the end of the content of each of your blog posts. Filter hooks can also be used for truncating text, changing formatting of content, or just about any other programming manipulation requirement (for example, adding to or overriding an array of values).

Custom code is added as a filter using the add_filter() function. The following code adds a sign-off to the end of each blog post, only when viewing the full blog post screen:

<?php
	add_filter( 'the_content', 'custom_filterhook_signoff' );
	function custom_filterhook_signoff ( $content ) {
		if ( is_single() ) {
			$content .= '<div class="sign-off">Th-th-th-th-th That\'s all, folks!</div>' . "";
		} // End IF Statement
		return $content;
	} // End custom_filterhook_signoff()
?>

Where can I learn more about action and filter hooks?