Coding Stephan

WordPress mail with Gmail

At some point you would like your wordpress website to be able to send email. You’ll need it for:

  • Password recovery
  • New User notifications
  • Contact forms

By putting this code in your funtions.php file off your child-theme, you won’t need a plugin to configure the gmail settings.

add_action( 'phpmailer_init', 'gmail_phpmailer_init' );
function gmail_phpmailer_init( PHPMailer $phpmailer ) {
    $phpmailer->Host = 'smtp.gmail.com';
    $phpmailer->Port = 587; 
    $phpmailer->Username = 'pietje_puk@gmail.com'; // Full username (with @gmail.com)
    $phpmailer->Password = 'your_secret_password'; // Your own password
    $phpmailer->SMTPAuth = true; // Gmail requires authentication
    $phpmailer->SMTPSecure = 'tls'; //587 is the tls port, ssl can also be configured with 'ssl' and port  is another possible value

    $phpmailer->IsSMTP();
}

Without this code you also could use a plugin for the same thing, but who needs plugins 😛