Can't send mail from WordPress forms

Regarding logging, I did mention them earlier. To recap: I entered this in the site’s wp-config.php file:

define( 'WP_DEBUG', true );
define( 'WP_DEBUG_LOG', true );
define( 'WP_DEBUG_DISPLAY', false );
@ini_set( 'display_errors', 0 );

I got no indication of any problem in debug.log when trying to send a test message.

I then ran this in the site as a webpage:

<?php
require_once( $_SERVER['DOCUMENT_ROOT'] . '/wp-load.php' );

$to = 'me@mydomain.com';
$subject = 'Test Mail using WordPress from MyServer';
$message = 'Hello, this is a test email using the PHP mail() function in the root of a WordPress website on MyServer.';
$headers = 'From: webmaster@thesite.com' . "\r\n" .
    'Reply-To: me@mydomain.com' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

if(wp_mail($to, $subject, $message, $headers)) {
    echo "Email sent successfully";
} else {
    echo "Email sending failed";
}
?>

To which it simply indicated “Email sending failed” in my browser.

I did this test (as root) as well:

echo "Subject: Test Mail" | /usr/sbin/sendmail -v myemail@mydomain.com

Which worked and was received through my email.

In all of my failed test attempts I didn’t see any indications of an error or even an attempt to send anything from the web space.

The /var/log/mail.log showed my successful attempt using /usr/sbin/sendmail above and the usual ongoing barrage of attempts by spammers and hackers and bots but never gave me any indications of issues with all of my failed attempts using a form on the website or the PHP page indicated above.

The /home/username/logs/php_log never gave me any indications of issues with all of my failed attempts using a form on the website or the PHP page indicated above.

I checked the mail queue and it was empty.

Hope that clarifies better. I originally indicated I believe that PHP is not handing off the emails to the correct place but I’m unable to figure out where that is happening. Especially with no log output for guidance.

I really don’t have the time, but, curiosity got the best of me about WP’s email capabilities. I added a site with the install script and posted a comment. Debian 11 box.

EDIT: Seems the admin email account was set to ‘no’ for some reason. Enabled it, added a comment and got the alert email.

It can’t be in the mail queue if it isn’t showing up in the mail log.

If it’s not showing up in the mail log, no attempt was made to send mail via the local server.

I don’t know anything about PHP, but I know my WordPress installation running under PHP-FPM shows up in /home/<domainname>/logs/php_log. (I didn’t configure it, that’s just how it do in Virtualmin’s default configuration.)

You have to find the error. There’s no use trying to fix something when we don’t even know what’s wrong.

Ok I do
php mail will not error or work if the MTA is disabled therefore we need to be sure that

  1. the MTA is running
  2. what does vm do when mail for the domain is disabled

I have set up an instance to mimic, as close as I can, the OP’s system and to be fair the only way I can get this to stop sending mail via PHP is to stop postfix running, the OP openly admits mail is hosted elsewhere and perhaps there is some way postfix has failed (through misconfiguration).
As the OP has other boxes that ‘work’ with the same setup (I assume that to be the case, not been told otherwise) we need to know the difference in the setups between the working box and the failed box

OP is able to send mail. It’s only WordPress that isn’t.

I don’t understand the question, I don’t think? Virtualmin doesn’t do anything when mail is disabled. Mail is a feature, absent that feature there’s nothing for it to do.

Local users can send mail regardless of whether they can receive it on the server.

I get all system messages from the box, just don’t get anything sent from within a WordPress environment. I get indications things don’t work, e.g., when telling the login page I forgot password and choosing my user I get a message:
Error: The email could not be sent. Your site may not be correctly configured to send emails…
but cannot find anything in logs indicating same.

Just stumbled across Services > Configure SSL Website > Log Files. The file is set to:
/var/log/virtualmin/domain.com_error_log
and the “Error log level” is set to Emergencies.

Could this be related to why I wouldn’t be seeing log entries or is this strictly related to SSL?

EDIT: Even when I enabled email on a domain to test, it would not send mail from WordPress. Roundcube worked, but that’s not connected to WordPress (sorry for my ability to articulate the difference).

The VM install script grabs the domain owner as the default email.
Did you change this?

Is the user that is being used set up on the system as a user and set to receive email on the Debian 11 box you are using? Or on an outside address?

That’s all that was required when I set it up on my Debian 11 box. In my case the default account was set to not receive mail but I got errors.

My screwup told me that you must have a valid user on the box. Just enabling email on the domain isn’t enough. That user can then forward to whatever address you like.

It is not at all related to SSL. It’s logs from the VirtualHost that is configured to serve over HTTPS.

Mail does not need to be enabled. Local users can send mail. Always. Unless you go to some lengths to do otherwise. But, you’ve already tested and confirmed you can send mail. This is not a mail problem, it is a PHP or application problem. And, we need to see errors to know what that problem is.

That doesn’t matter.

Also doesn’t matter.

I agree 100%. It seems my inability to solve this is not knowing exactly what the issue is because I am unable to find any logging to examine.

I suspect this is something that must be set in PHP? Any ideas along that line?

Thats odd it’s the only way i can get it to fail with no error logging

Someplace out on the web I found an indication that these might be missing from my /etc/postfix/main.cf file, so per the instructions I added them to the bottom of the file and restarted postfix:
mail_owner = _postfix
setgid_group = _postdrop

Did a bunch of testing again and the problem did not go away.

However, when creating another website on the server I was presented with this error while Virtualmin was creating it:
Adding new SSL virtual website …
… Apache SSL website failed! : Failed to regenerate table /etc/postfix/sni_map: postmap: fatal: file /etc/postfix/main.cf: parameter mail_owner: unknown user name value: _postfix

I went back in and removed the two entries and restarted postfix again.

Now that website was giving me 403 errors so I removed the site and re-created it and it repeated that. So I removed it altogether, cleared my workstation cache, and the site still gives me a403 “Forbidden” error and its SSL certificate is still active. The site is not in my virtual servers list. Something was not removed. Any ideas on where I would go to remove whatever is making the system “think” the site still is there and serving up that error page?

EDIT: After reinstalling the website again on that same domain and regenerating an SSL certificate again the site appears to be functioning normally, so scrap my paragraph about the 403 errors…

Quite odd that no logs were recorded tbh. I could check out your wordpress setup as well as your postfix config for you if you want.

I don’t know how else to explain that it is not a mail problem. Postfix is not broken. You have sent mail, presumably as the same user you are trying to send mail from with your PHP script. You know mail works.

I sense your exasperation and have more than enough of it myself. I agree it is not a Postfix problem. Since the issue is the inability to send mail from any forms inside WordPress then I welcome better syntax to explain what is happening in order to reach a solution.

I’m just trying to help you not waste time on things that can’t possibly help (and might break something else).

We need to see errors. I don’t know why you’re not getting any logs, errors have to be going somewhere. Have you configured WordPress to log to the database? (I don’t know how you’d do that. My WordPress logs to the PHP log.)

You could try removing WordPress from the equation and just use the PHP mail function, just to get it to do something. I dunno.

That looks like something to explore.

But the errors are at least as important. I do not know how to make PHP generate them. I need to look more into what settings in the site’s PHP file allow that.

try this plugin

you did with this code in post #1