admin管理员组文章数量:1427773
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 badges3 Answers
Reset to default 1I'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:
- You need to close your
if
statement with closing curly bracket}
- Why are you defining the variables but then using
$_POST
values in HTML? - You should ALWAYS get in habit of sanitizing ANY kind of user input ALWAYS
- 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
本文标签: phpwpmail not sending
版权声明:本文标题:php - wp_mail not sending 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745492272a2660654.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论