Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hans2103/23d8d55666ff1b747a21c91a223889c9 to your computer and use it in GitHub Desktop.
Save hans2103/23d8d55666ff1b747a21c91a223889c9 to your computer and use it in GitHub Desktop.
Config Wordpress SMTP for MailHog - test your emails local
/*
* Add the MailHog to your wordpress projects
* By Khalid Ahmada
* MailHog @see https://github.com/mailhog/MailHog
*/
class WP_MAILHOG
{
function __construct()
{
// Config only on local
if ($this->isLocal()) {
$this->AddSMTP();
}
}
/**
* Config Your local rule
* default is check if the host is *.test or *.local
* @return bool
*/
private function isLocal()
{
if (defined('WP_HOME')) {
if (strpos(WP_HOME, '.test') !== false ||
strpos(WP_HOME, '.local') !== false
) {
return true;
}
}
return false;
}
/*
* Wordpress default hook to config php mail
*/
private function AddSMTP()
{
add_action('phpmailer_init', array($this, 'configEmailSMTP'));
}
/*
* Config MailTramp SMTP
*/
public function configEmailSMTP(PHPMailer $phpmailer)
{
$phpmailer->IsSMTP();
$phpmailer->Host='127.0.0.1';
$phpmailer->Port=1025;
$phpmailer->Username='';
$phpmailer->Password='';
$phpmailer->SMTPAuth=true;
}
}
new WP_MAILHOG();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment