一款自制的老插件停止发送邮件

时间:2017-09-07 作者:Woot99

我运行了一个旧插件,让人们挑选产品,然后向选定的商店发送邮件。但有一段时间,它停止了发送邮件。我猜代码里有什么东西过时了?

<?php
/*
    Plugin Name: BreadForm
    Description: 
    Version: 
    Author: 
    Author URI: 
*/

if (!class_exists("BreadFormPlugin")) {
    class BreadFormPlugin
    {
        protected $fromEmail = "[email protected]";

        protected $fromName = "something";

        function BreadFormPlugin()
        {
            $this->__construct();
        }

        function __construct()
        {
            add_shortcode(\'breadform\', array($this, \'shortcode_breadform\'));
        }

        function getItems()
        {
            // Price should be entered in "cents"
            $items = array(
                array("name" => "Product 1", "price" => 1050),
                array("name" => "Product 2", "price" => 525),
            );

            $new_array = array();
            foreach ($items as $item) {
                $item["hash"] = md5($item["name"]);
                $new_array[md5($item["name"])] = $item;
            }

            return $new_array;
        }

        function getShops()
        {
            $shops = array(
                array("name" => "store 1", "email" => "[email protected]"),
                array("name" => "store 2", "email" => "[email protected]"),
            );

            $new_array = array();
            foreach ($shops as $shop) {
                $shop["hash"] = md5($shop["name"]);
                $new_array[md5($shop["name"])] = $shop;
            }

            return $new_array;

        }

        function enqueue_scripts()
        {
            wp_register_script(\'breadformScript\', plugins_url(\'/breadform.js\', __FILE__), array("jquery"), \'1.0\');
            wp_register_script(\'styleSelect\', plugins_url(\'/jquery.styleSelect.min.js\', __FILE__), array("jquery"), \'1.0\');
            wp_register_script(\'jscrollpane\', plugins_url(\'/jscrollpane/jquery.jscrollpane.min.js\', __FILE__), array("jquery"), \'1.0\');
            wp_register_script(\'mousewheel\', plugins_url(\'/jscrollpane/jquery.mousewheel.js\', __FILE__), array("jquery"), \'1.0\');

            wp_enqueue_script(\'breadformScript\');
            wp_enqueue_script(\'styleSelect\');
            wp_enqueue_script(\'jscrollpane\');
            wp_enqueue_script(\'mousewheel\');

            wp_register_style(\'breadformStyle\', plugins_url(\'/breadform.css\', __FILE__), false, \'1.01\', \'all\');
            wp_register_style(\'jscrollpaneStyle\', plugins_url(\'/jscrollpane/jquery.jscrollpane.css\', __FILE__), false, \'1.0\', \'all\');

            wp_enqueue_style(\'breadformStyle\');
            wp_enqueue_style(\'jscrollpaneStyle\');

            // declare the URL to the file that handles the AJAX request (wp-admin/admin-ajax.php)
            wp_localize_script(\'breadformScript\', \'BreadForm\', array(\'ajaxurl\' => admin_url(\'admin-ajax.php\')));

        }

        function shortcode_breadform()
        {

            $shops = $this->getShops();
            $items = $this->getItems();

            $output = \'
            <script type="text/javascript">
                var breadformItems = \' . json_encode($items) . \';
            </script>
            <div id="breadformForm">
                <div id="breadformOrderRow">
                    <div id="breadformBoxItem">
                    <select name="breadformItem" id="breadformItem" class="breadformSelect">
                        <option value="">Bread</option>
                        \';
            foreach ($items as $item) {
                $output .= \'<option value="\' . md5($item[\'name\']) . \'">\' . $item[\'name\'] . \' - \' . number_format(($item[\'price\'] / 100), 2, \',\', \' \') . \' GBP</option>\';
            }
            $output .= \'
                    </select>
                    </div>
                    <div id="breadformBoxQty">
                    <select name="breadformQty" id="breadformQty" class="breadformSelectShort">
                        <option value="1">QTY</option>
                        \';
            for ($i = 1; $i < 20; $i++) {
                $output .= \'<option value="\' . $i . \'">\' . $i . \' pcs</option>\';
            }
            $output .= \'

                    </select>
                    </div>
                    <div id="breadformAdd">&nbsp;</div>
                </div>
                <div id="breadformCart">
                    <div id="breadformCartTop">
                        <div id="breadformSummary"><div id="breadformTitle">Order</div><div id="breadformAmountTitle">Sum:</div><div id="breadformAmount">0 GBP</div><br class="clear: both;"/></div>
                    </div>
                    <div id="breadformCartMain"><div id="breadformLoader"></div><div id="breadformCartContent"></div></div>
                    <div id="breadformCartBottom"></div>
                </div>
<div id="breadformShop">
                    <select name="breadformSelectedShop" id="breadformSelectedShop" class="breadformSelect">
                        <option value="">Shop</option>
                        \';
            foreach ($shops as $shop) {
                $output .= \'<option value="\' . md5($shop[\'name\']) . \'">\' . $shop[\'name\'] . \'</option>\';
            }
            $output .= \'
                    </select>
                </div>
                <div id="breadformDateRow">
                    <div id="breadformBoxDate">
                    <select name="breadformDate" id="breadformDate" class="breadformSelect">
                        <option value="">Date for pickup</option>

                        \';
            for ($i = 1; $i <= 14; $i++) {
                $output .= \'<option value="\' . date(\'Ymd\',time()+($i*86400)) . \'">\'.date_i18n(\'l, d F\',time()+($i*86400)).\'</option>\';
            }
            $output .= \'
                    </select>
                    </div>
                    <div id="breadformBoxTime">
                    <select name="breadformTime" id="breadformTime" class="breadformSelectShort">
                        <option value="">Time</option>
                        \';
            for ($i = 8; $i <= 18; $i++) {
                $output .= \'<option value="\' . $i . \'">\' . str_pad($i, 2, "0", STR_PAD_LEFT) . \':00</option>\';
            }
            $output .= \'

                    </select>
                    </div>
                </div>

                <div id="breadformCustomer">

                    <label for="breadformName">Name:</label>
                    <input type="breadformName" id="breadformName" value="" class="breadformInput noFocus"/>

                    <label for="breadformPhone">Phone:</label>
                    <input type="breadformPhone" id="breadformPhone" value="" class="breadformInput noFocus"/>

                    <label for="breadformEmail">E-mail:</label>
                    <input type="breadformEmail" id="breadformEmail" value="" class="breadformInput noFocus" />

                    <label for="breadformMessage">Message:</label>
                    <textarea name="breadformMessage" id="breadformMessage" class="noFocus"></textarea>

                    <div id="breadformSubmit" style="-moz-border-radius: 5px;
border-radius: 5px;"></div><div id="breadformSubmitLoading"></div>
                </div>

            </div><div id="breadformSuccess"><h1>Thanks for ordering</h1></div>\';
            return $output;
        }

        function ajax_callback()
        {
            global $BreadFormPlugin;
            $shops = $BreadFormPlugin->getShops();
            $items = $BreadFormPlugin->getItems();
            $message = "";
            if (isset($_SESSION[\'breadform_cart\']) === false) {
                $_SESSION[\'breadform_cart\'] = array(
                    \'total\' => 0,
                    \'items\' => array()
                );
            }


            if ($_POST[\'cart_action\'] == "add") {
                if (array_key_exists($_POST[\'cart_item\'], $items)) {
                    if (array_key_exists($_POST[\'cart_item\'], $_SESSION[\'breadform_cart\'][\'items\'])) {
                        $new_qty = intval($_POST[\'cart_item_qty\']) + intval($_SESSION[\'breadform_cart\'][\'items\'][$_POST[\'cart_item\']][\'qty\']);
                        $_SESSION[\'breadform_cart\'][\'items\'][$_POST[\'cart_item\']] = array(
                            \'hash\' => $items[$_POST[\'cart_item\']][\'hash\'],
                            \'item\' => $items[$_POST[\'cart_item\']][\'name\'],
                            \'qty\' => $new_qty,
                            \'total\' => (intval($items[$_POST[\'cart_item\']][\'price\']) * $new_qty),
                        );
                    } else {
                        $_SESSION[\'breadform_cart\'][\'items\'][$_POST[\'cart_item\']] = array(
                            \'hash\' => $items[$_POST[\'cart_item\']][\'hash\'],
                            \'item\' => $items[$_POST[\'cart_item\']][\'name\'],
                            \'qty\' => intval($_POST[\'cart_item_qty\']),
                            \'total\' => (intval($items[$_POST[\'cart_item\']][\'price\']) * intval($_POST[\'cart_item_qty\'])),
                        );
                    }
                    $_SESSION[\'breadform_cart\'][\'items\'][$_POST[\'cart_item\']][\'pretty_qty\'] = $_SESSION[\'breadform_cart\'][\'items\'][$_POST[\'cart_item\']][\'qty\'] . " pcs";
                    $_SESSION[\'breadform_cart\'][\'items\'][$_POST[\'cart_item\']][\'pretty_total\'] = number_format(($_SESSION[\'breadform_cart\'][\'items\'][$_POST[\'cart_item\']][\'total\'] / 100), 2, \',\', \' \') . " GBP";
                }
            } elseif ($_POST[\'cart_action\'] == "delete") {
                if (array_key_exists($_POST[\'cart_item\'], $_SESSION[\'breadform_cart\'][\'items\'])) {
                    unset($_SESSION[\'breadform_cart\'][\'items\'][$_POST[\'cart_item\']]);
                }

            } elseif ($_POST[\'cart_action\'] == "order") {
                $senderName = $_POST[\'senderName\'];
                $senderPhone = $_POST[\'senderPhone\'];
                $senderEmail = $_POST[\'senderEmail\'];
                $senderMessage = $_POST[\'senderMessage\'];
                $selectedShop = $_POST[\'selectedShop\'];
                $selectedDate = intval($_POST[\'selectedDate\']);
                $selectedTime = intval($_POST[\'selectedTime\']);


                if (strlen($senderName) < 3) {
                    $message = "Please fill in you name.";
                }
                if (is_email($senderEmail) == false) {
                    $message = "Please check the e-mail.";
                }


                if ($selectedTime < 4 || $selectedTime > 23) {
                    $message = "Something is wrong with the selected time";
                }

                if ($selectedDate < 20120101) {
                    $message = "Something is wrong with the date".$selectedDate;
                }


                if (array_key_exists($selectedShop, $shops) == FALSE) {
                    $message = "No shop is selected";
                }

                if (count($_SESSION[\'breadform_cart\'][\'items\']) < 1) {
                    $message = "You need to add something first.";
                }


                // Successful order
                if ($message == "") {

                    // Prepare cart
                    $cartMessage = "";
                    foreach ($_SESSION[\'breadform_cart\'][\'items\'] as $item) {
                        $cartMessage .= $item[\'pretty_qty\']."\\t\\t".$item[\'item\']." - ".$item[\'pretty_total\']."\\n";
                    }
                    $cartMessage .= "\\nTotal: ".$_SESSION[\'breadform_cart\'][\'total\'];


                    // Start preparing order e-mail (to the store)
                    $orderMessage = "


Name: ".$senderName."
Phone: ".$senderPhone."
Mail: ".$senderEmail."
Message: ".$senderMessage."
For collection: ".$selectedDate." - ".str_pad($selectedTime, 2, "0", STR_PAD_LEFT) . ":00



Order:

".$cartMessage;



                    // Start preparing  e-mail (to the store)
                    $confirmationMessage = " 
We have  received your order.

Namne: ".$senderName."
Phone: ".$senderPhone."
E-mail: ".$senderEmail."
Message: ".$senderMessage."
For collection: ".$selectedDate." - ".str_pad($selectedTime, 2, "0", STR_PAD_LEFT) . ":00

Your order:

".$cartMessage."

---

";

                    $headers = \'From: \'.$senderEmail.\' <\'.$senderName.\'>\' . "\\r\\n";

                    // Send email to store
                    wp_mail($shops[$selectedShop][\'email\'], \'Order from webpage\', $orderMessage, $headers);

                    // Send email to customer
                    // wp_mail($senderEmail, \'Tack för din beställning\', $confirmationMessage, $headers);

                    $message = "success";

                    $_SESSION[\'breadform_cart\'][\'items\'] = array();
                    $_SESSION[\'breadform_cart\'][\'total\'] = 0;

                }


            } elseif ($_POST[\'cart_action\'] == "clear") {
                $_SESSION[\'breadform_cart\'][\'items\'] = array();
                $_SESSION[\'breadform_cart\'][\'total\'] = 0;
            }


            $total_amount = 0;
            // summarize total amount
            foreach ($_SESSION[\'breadform_cart\'][\'items\'] as $item) {
                $total_amount += $item[\'total\'];
            }

            $_SESSION[\'breadform_cart\'][\'message\'] = $message;
            $_SESSION[\'breadform_cart\'][\'total\'] = number_format(($total_amount / 100), 2, \',\', \' \') . " GBP";
            echo json_encode($_SESSION[\'breadform_cart\']);
            die(); // this is required to return a proper result
        }

    }

}

if (class_exists("BreadFormPlugin")) {
    $BreadFormPlugin = new BreadFormPlugin();
}

//Actions and Filters
if (isset($BreadFormPlugin)) {
    //Actions
    add_action(\'wp_enqueue_scripts\', array("BreadFormPlugin", \'enqueue_scripts\'));

    add_action(\'wp_ajax_breadform\', array("BreadFormPlugin", \'ajax_callback\'));
    add_action(\'wp_ajax_nopriv_breadform\', array("BreadFormPlugin", \'ajax_callback\'));

    //Filters
}

if (!session_id()) {
    add_action(\'init\', \'session_start\');
}

1 个回复
SO网友:HU ist Sebastian

许多电子邮件寄宿者改变了他们的规则以防止垃圾邮件。通常情况下,在“发件人”标记中有一个域名并且来自不同域的服务器的电子邮件会被拒绝。所以我的猜测是,也许电子邮件仍然发送,但没有收到。

您可以通过更改此行来检查这一点:

$headers = \'From: \'.$senderEmail.\' <\'.$senderName.\'>\' . "\\r\\n";
对这样的事情:

$my_domain = $_SERVER[\'HTTP_HOST\'];
$headers = \'From: orders@\'.$my_domain."\\r\\n".\'Reply-To: \'.$senderEmail."\\r\\n";
并检查电子邮件现在是否正确发送和接收。(通过设置“回复到”标题,在邮件客户端中单击“回复”时会插入正确的地址。)

结束

相关推荐

Virtual Pages plugins

我很难让插件正常工作Virtual Pages (WordPress插件可简化虚拟页面的创建)我确实进行了编辑,根据查询创建了一个循环。add_action( \'gm_virtual_pages\', function( $controller ) { /* Creating virtuals pages for companies */ $args = array( \'post_type\' => array(\'companies\',), \'post_status\'