在 Wordpress 中使用 wp_mail() 而不是 mail() 不起作用 [英] Using wp_mail() instead of mail() in Wordpress does not work

查看:17
本文介绍了在 Wordpress 中使用 wp_mail() 而不是 mail() 不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于在我的服务器上禁用了 PHP 邮件,它使集成主题的联系表单停止工作.

Since PHP mail has been disabled on my server it has stopped a theme integrated contact form from working.

该主题名为 Boldy,它有自己的 sendmail.php 文件,该文件使用 mail() 而不是 wp_mail().

The theme is called Boldy and it has its own sendmail.php file which uses mail() instead of wp_mail().

mail() 更改为 wp_mail() 不起作用,但我不知道为什么?

Changing mail() to wp_mail() does not work, but I'm not sure why?

<?php
if (isset($_POST['submit']))
{
    error_reporting(E_NOTICE);

    function valid_email($str)
    {
        return ( ! preg_match("/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix", $str)) ? FALSE : TRUE;
    }

    if ($_POST['name'] != '' && $_POST['email'] != '' && valid_email($_POST['email']) == TRUE && strlen($_POST['comment']) > 1)
    {
        $to = preg_replace("([\r\n])", "", $_POST['receiver']);
        $from = preg_replace("([\r\n])", "", $_POST['email']);
        $subject = "Website contact message from ".$_POST['name'];
        $message = $_POST['comment'];
        $match = "/(bcc:|cc:|content\-type:)/i";

        if (preg_match($match, $to) || preg_match($match, $from) || preg_match($match, $message))
        {
            die("Header injection detected.");
        }

        $headers = "From: ".$from."\r\n";
        $headers .= "Reply-to: ".$from."\r\n";

        if (mail($to, $subject, $message, $headers))
        {
            echo 1; //SUCCESS
        }
        else
        {
            echo 2; //FAILURE - server failure
        }
    }
    else
    {
        echo 3; //FAILURE - not valid email
    }

}
else
{
    die("Direct access not allowed!");
}
?>

推荐答案

在第 2 行插入:

    define('WP_USE_THEMES', false);
    require('../../../wp-load.php');

诀窍是最初编写的 sendmail.php 实际上并没有加载到 wordpress 设备中,因此没有定义 wp_mail.

The trick is that sendmail.php as originally written doesn't actually load in the wordpress gear, so wp_mail isn't defined.

第一行应该是可选的.我从 http://butlerblog.com/的示例代码中获取了它2012/09/23/testing-the-wp_mail-function/.

The first line should be optional. I took it from the sample code at http://butlerblog.com/2012/09/23/testing-the-wp_mail-function/.

这篇关于在 Wordpress 中使用 wp_mail() 而不是 mail() 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆