php - wp_mail not sending

So I have a localhost WordPress server, and the function wp_mail workd perfectly and sends. When I put the exact same fi

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 = "[email protected]";


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 :)

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 = "[email protected]";


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 :)

Share Improve this question asked Feb 11, 2017 at 2:35 Francisco F.Francisco F. 131 silver badge5 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 1

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

wp_mail( '[email protected]', 'The subject', 'The message' ); 

I also saw a note in the wp_mail in WP Codex that some hosts will require a '[email protected]' mail account created before it will send out the message.

Take a look at the dev pages here https://developer.wordpress/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.

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 = "[email protected]";


    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.

?>

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         = "[email protected]";

    $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/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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745492272a4630038.html

相关推荐

  • php - wp_mail not sending

    So I have a localhost WordPress server, and the function wp_mail workd perfectly and sends. When I put the exact same fi

    2小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信