0

So I have a localhost WordPress server, and the function wp_mail workd perfectly and sends. When I put the exact same file in an actual WordPress website as a theme, it only shows me a blank screen, and doesn't send anything. Code:

<?php
if (isset($_POST['submit'])) {
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$company = $_POST['company'];
$to = "mail@server.com";


function wpse27856_set_content_type(){
return "text/html";
}
add_filter( 'wp_mail_content_type','wpse27856_set_content_type' );

$msg = '<html><body><h1>Contact Service</h1><table rules="all" style="border-color: #666;" cellpadding="10"><tr style="background: #E3E8EC;"><td><strong>First Name:</strong> </td><td>' . strip_tags($_POST['first_name']) . '</td></tr><tr><td><strong>Last Name:</strong> </td><td>' . strip_tags($_POST['last_name']) . '</td></tr><tr><td><strong>Email:</strong> </td><td>' . strip_tags($_POST['email']) . '</td></tr><tr><td><strong>Company:</strong> </td><td>' . strip_tags($_POST['company']) . '</td></tr><tr><td><strong>Message:</strong> </td><td>' . strip_tags($_POST['message']) . '</td></tr></body></html>';
wp_mail( $to, $subject, $msg );
?>

This is followed by a form where you input everything. Any help would be appreciated :)

1

I'd try a simple wp_mail() snippet that will test if the wp_mail process is working properly:

wp_mail( 'me@example.net', 'The subject', 'The message' ); 

I also saw a note in the wp_mail in WP Codex that some hosts will require a 'wordpress@yourdomain.com' mail account created before it will send out the message.

Take a look at the dev pages here https://developer.wordpress.org/reference/functions/wp_mail/ . Note that you may need to set up the wp_mail_from() value to specify the 'from' address.

Also, when you send the test to your own email, check your spam/trash folders for interception.

Plus, of course, sanitizing the input from your form fields.

  • I tried the wp_mail_from(), sending the simple wp_mail() snippet, but it still doesn't work. What happens is the bottom part of the page disappears, and this is because there is an error somewhere. – Francisco F. Feb 11 '17 at 5:19
1

The reason the screen goes blank is that there is an error. You need to turn on debugging output (such as setting WP_DEBUG to true) so you can get some insight into what the error is.

In looking at the code, the most likely issue is that you are missing a closing curly brace for your opening "if". Perhaps that was just a copy/paste error when asking your question?

<?php
if (isset($_POST['submit'])) {
    $first_name = $_POST['first_name'];
    $last_name = $_POST['last_name'];
    $email = $_POST['email'];
    $subject = $_POST['subject'];
    $message = $_POST['message'];
    $company = $_POST['company'];
    $to = "mail@server.com";


    function wpse27856_set_content_type(){
    return "text/html";
    }
    add_filter( 'wp_mail_content_type','wpse27856_set_content_type' );

    $msg = '<html><body><h1>Contact Service</h1><table rules="all" style="border-color: #666;" cellpadding="10"><tr style="background: #E3E8EC;"><td><strong>First Name:</strong> </td><td>' . strip_tags($_POST['first_name']) . '</td></tr><tr><td><strong>Last Name:</strong> </td><td>' . strip_tags($_POST['last_name']) . '</td></tr><tr><td><strong>Email:</strong> </td><td>' . strip_tags($_POST['email']) . '</td></tr><tr><td><strong>Company:</strong> </td><td>' . strip_tags($_POST['company']) . '</td></tr><tr><td><strong>Message:</strong> </td><td>' . strip_tags($_POST['message']) . '</td></tr></body></html>';
    wp_mail( $to, $subject, $msg );

} // this closes the "if" and was omitted in the original code.

?>
1

Lots of issues in your code:

  1. You need to close your if statement with closing curly bracket }
  2. Why are you defining the variables but then using $_POST values in HTML?
  3. You should ALWAYS get in habit of sanitizing ANY kind of user input ALWAYS
  4. While the filter does work, you can also set Content-Type in headers

This is what I would use instead:

if ( isset( $_POST['submit'] ) ) {

    $first_name = sanitize_text_field( $_POST['first_name'] );
    $last_name  = sanitize_text_field( $_POST['last_name'] );
    $email      = sanitize_text_field( $_POST['email'] );
    $subject    = sanitize_text_field( $_POST['subject'] );
    $message    = wp_kses( $_POST['message'], false );
    $company    = sanitize_text_field( $_POST['company'] );
    $to         = "mail@server.com";

    $msg = '<html><body><h1>Contact Service</h1><table rules="all" style="border-color: #666;" cellpadding="10"><tr style="background: #E3E8EC;"><td><strong>First Name:</strong> </td><td>' . $first_name . '</td></tr><tr><td><strong>Last Name:</strong> </td><td>' . $last_name . '</td></tr><tr><td><strong>Email:</strong> </td><td>' . $email . '</td></tr><tr><td><strong>Company:</strong> </td><td>' . $company . '</td></tr><tr><td><strong>Message:</strong> </td><td>' . $message . '</td></tr></body></html>';
    $sent = wp_mail( $to, $subject, $msg, array( 'content-type' => 'text/html') );

    if( $sent ){
        echo "Email Sent!";
    } else {
        echo "Email NOT Sent!";
    }
}

As butlerblog also mentioned, WSOD (White Screen of Death) means more than likely a 500 internal server error. To show on your page (which should be disabled for production sites)

More information can be found at the link below, or by contacting your hosting company: https://codex.wordpress.org/WP_DEBUG

Specifically, if you want to enable debug output on your site, you should have these values set in the wp-config.php file as defined in the documentation link above.

define( 'WP_DEBUG', true );
define( 'WP_DEBUG_DISPLAY', true );
define( 'WP_DEBUG_LOG', true );

You must have WP_DEBUG true in order for WP_DEBUG_DISPLAY to be used, which should be set to false to prevent PHP warnings output on frontend of site (for production), and true for debugging (to show on screen).

WP_DEBUG_LOG will create a file located at wp-content/debug.log with the error log output (when set to true)

That should tell you what the actual error is you're having

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.