-
-
Save hans2103/23d8d55666ff1b747a21c91a223889c9 to your computer and use it in GitHub Desktop.
Config Wordpress SMTP for MailHog - test your emails local
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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