mailhog / MailHog

Web and API based SMTP testing
MIT License
14.08k stars 1.07k forks source link

Mailhog not catching mail from WordPress install on Valet #140

Open brothman01 opened 7 years ago

brothman01 commented 7 years ago

I have two valet installs that should use mailhog services:

  1. On my personal desktop - works perfectly. Mailhog catches all outgoing mail.

  2. On my work laptop - cannot send mail. Mailhog does not catch any outgoing mail.

The Problem: I installed mailhog yesterday with Brew on my laptop, and the mailserver is accessible when mailhog is running, but when I try to send email from the WordPress, mailhog does not catch the outgoing mail and the email does not show up anywhere.

What I have tried:

  1. I updated brew and installed mailhog

  2. I set mailhog to run in the terminal everytime I want to use it ($ mailhog)

  3. I created a plugin with the following code:

    <?php
    add_action( 'phpmailer_init', function( $phpmailer ) {
        // Define that we are sending with SMTP
        $phpmailer->isSMTP();
    
        // The hostname of the mail server
        $phpmailer->Host = 'localhost';
    
        // Use SMTP authentication (true|false)
        $phpmailer->SMTPAuth = false;
    
        // SMTP port number
        // Mailhog normally run on port 1025
        $phpmailer->Port = WP_DEBUG ? '1025' : '25';
    
        // Username to use for SMTP authentication
        // $phpmailer->Username = 'yourusername';
    
        // Password to use for SMTP authentication
        // $phpmailer->Password = 'yourpassword';
    
        // The encryption system to use - ssl (deprecated) or tls
        // $phpmailer->SMTPSecure = 'tls';
    
        $phpmailer->From = 'admin@wp.dev';
        $phpmailer->FromName = 'WP DEV';
    }, 10, 1 );
  4. I turned on WP_DEBUG on that WordPress installation

  5. I downloaded & installed the plugin 'WP Mail SMTP' (this is what a Google Search said to do but it did not appear to do anything)

Question:
Am I missing a step? How do I make mailhog catch outgoing mail from my WordPress installation?

brothman01 commented 7 years ago

Any ideas?

ian-kent commented 7 years ago

Hi @brothman01

Could you give a bit more info please?

What do you mean by 'does not catch any outgoing mail' - does it make a successful connection to MailHog, and then fail to deliver the message (i.e. not in UI or API), or does it fail to even connect to MailHog, or does it appear to connect to MailHog and successful delivers but MailHog doesn't show any corresponding log output etc?

The only thing I can think of is some PHP configuration missing, or some other misconfiguration, which means emails sent by phpmailer aren't going to MailHog. If the wordpress installation is correctly pointing at MailHog, I'd expect at least a single line of log output saying the connection was successful.

Or does your work laptop have anything designed to intercept/monitor network traffic which could be interfering?

I'll leave this open for now, but it sounds like a Wordpress issue not a MailHog issue, so there's probably not a lot I can really do to help.

bishless commented 7 years ago

It’s working for me… though I went about it in a slightly different way.

NOTE: I'm testing on a WordPress site served up at <blahblahdomain>.dev and catching mail at localhost:8025.

  1. I updated brew and installed mailhog
  2. I set mailhog to run as a service brew services start mailhog.
  3. I was getting syntax warnings in my editor when I attempted to use the plugin code above so I tweaked it - Configure WordPress on Valet to use MailHog · GitHub.
  4. I activated the plugin and submitted to a Gravity Form that I had setup w/ email notifications.
  5. Mail was caught by MailHog. 👍
tyndyll commented 5 years ago

@brothman01 Is this still an issue?

brothman01 commented 5 years ago

yes, I stopped trying to use mailhog for a while but now I am at it again!

I used bishless's steps from above and made an MU plugin using his code but mailhog is still not catching mail that I have another WordPress plugin I wrote sending using the mail() function. Should I be using a different PHP function or something?

tyndyll commented 5 years ago

Do you have the full line from your PHP code where mail is being called?

And any relevant php.ini params? It’s been a while since I used PHP but we’ll try to get you runninh

brothman01 commented 5 years ago

lol sry but it's a lot of php :) anyway, the line is: mail( "someone@example.com", "My subject", $msg ); which is being called from an asynchronous request (I know this is not simplifying anything), a request that is fired from JQuery which calls the php function that contains this line.

I did not configure php.ini which may be the problem.. I just assumed that bishless's method of using a plugin would override the php.ini anyway so I configure my mail command ports with the code:

<?php
/**
 * @link
 * @since             1.0.0
 * @package           TODO
 *
 * @wordpress-plugin
 * Plugin Name:       Use MailHog
 * Description:       Configure WordPress on Valet to use MailHog
 * Version:           1.0.0
 * Tags: local, email
 */
add_action( 'phpmailer_init', 'bish_configMH', 10, 1 );
function bish_configMH( $phpmailer ) {
    // Define that we are sending with SMTP
    $phpmailer->isSMTP();
    // The hostname of the mailserver
    $phpmailer->Host = 'localhost';
    // Use SMTP authentication (true|false)
    $phpmailer->SMTPAuth = false;
    // SMTP port number
    // Mailhog normally run on port 1025
    $phpmailer->Port = WP_DEBUG ? '1025' : '25';
    // Username to use for SMTP authentication
    // $phpmailer->Username = 'yourusername';
    // Password to use for SMTP authentication
    // $phpmailer->Password = 'yourpassword';
    // The encryption system to use - ssl (deprecated) or tls
    // $phpmailer->SMTPSecure = 'tls';
    $phpmailer->From = 'site_adm@wp.local';
    $phpmailer->FromName = 'WP DEV';
}
?>
rdlmda commented 5 years ago

I'm having the same issue. Well, not using Valet, not on a mac, etc; but I can't seem to make Mailhog intercept my WordPress emails.

MatzeKitt commented 3 years ago

I had the same issue. For me, it was helpful to check whether the email gets send successfully from within WordPress. So I used the hook wp_mail_failed and checked for the proper error message:

add_action( 'wp_mail_failed', function( $error ) {
    die( '<pre>' . var_dump($error) );
} );

This way, I could find out that wordpress@localhost is no valid email address as from email for wp_mail().