Wordpress SMTP met Perfacilis Mail

  •  
  •  

Onderstaande instructies zorgen dat alle mail die jouw Wordpress website verstuurt via de ingestelde SMTP Server worden verzonden. Je kunt hier bijvoorbeeld jouw Gmail account instellen, of de Perfacilis Mail Service gebruiken.

SMTP instellingen in wp-config.php

Plaats onderstaande in wp-config.php .
Let op: dit moet voor de regel waar ABSPATH wordt gedefinieerd!

/**
* SMTP Settings
* @link https://www.perfacilis.com/faq/wordpress-smtp-met-perfacilis-mail.html
*/
define('SMTP_HOST', 'mail.perfacilis.com');
define('SMTP_PORT', '465');
define('SMTP_USER', 'ad@friet.pan');
define('SMTP_PASS', 'NotInRockYou');
define('SMTP_SECURE', 'ssl');

De waarde voor SMTP_SECURE kan ingesteld worden op: ' tls' voor STARTTLS , ' ssl' voor SMTPS of '' (leeg) voor geen versleuteling.
Voor Perfacilis Mail (en de meeste andere services) adviseren we de volgende combinaties:

  • Setting port 465 and secure to 'ssl' ;
  • Setting port 587 and secure to 'tls' .

PHPMailer via SMTP laten versturen

We'll create a very basic plugin to make PHPMailer use the credentials as defined above, by putting the snippet below into wp-content/plugins/smtp-settings/plugin.php . We advice to not put this into the functions.php of the current theme, for this will break when changing to a different theme.

Finally, the plugin can be activated trough the interface or trough the WP-CLI: wp plugin activate smtp-settings .

wp-content/plugins/smtp-settings/plugin.php
<?php
/*
Plugin Name: SMTP Settings
 Plugin URI: https://www.perfacilis.com/faq/wordpress-smtp-met-perfacilis-mail.html
Description: Custom SMTP settings for PHPMailer
Version: 1.0
Author: Ad Patat <ad@friet.pan>
*/

add_action('phpmailer_init', function(\PHPMailer\PHPMailer\PHPMailer $mailer) {
 $mailer->isSMTP();
 $mailer->Host = SMTP_HOST;
 $mailer->Port = SMTP_PORT;
 $mailer->SMTPSecure = SMTP_SECURE;

  if (SMTP_USER && SMTP_PASS) {
    $mailer->SMTPAuth = true;
    $mailer->Username = SMTP_USER;
    $mailer->Password = SMTP_PASS;
  }
});

SMTP instellingen testen

Als laatste kun je met deze uiterst lelijke (maar effectieve) manier heel simpel testen of alles werkt.
Voeg dit stuk code toe onder bovenstaand blok in je functions.php en open vervolgens jouw site.

Voeg aan de url '?testmail' toe en de mail wordt verzonden, dus als jouw homepage https://mijnsite.nl is, dan ga navigeer je nu naar https://mijnsite.nl?testmail . Werkt alles? Haal onderstaande regels dan maar weg.

if (filter_input(INPUT_GET, 'testmail', FILTER_VALIDATE_BOOL)) {
  echo 'Sending message!';
  var_dump( wp_mail('your@email.address', 'Test123', 'Body, it is now: ' . time()) );
  exit;
}

Sources