0

I have a old plugin running that let people pick products and then sends a mail to the store that is selected. But for a while now it has stoped sending the mails. I guess something in the code is out of date or something?

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

if (!class_exists("BreadFormPlugin")) {
    class BreadFormPlugin
    {
        protected $fromEmail = "info@something.com";

        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" => "store1@something.com"),
                array("name" => "store 2", "email" => "store2@something.com"),
            );

            $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');
}
0

Many E-Mail-Hosters changed their rules to prevent spam. Often times, E-Mails that have one domain name in their "from"-Tag and come from a server with a different domain are rejected. So my guess is, maybe the emails are still sent, but not received.

You can check this by changing this line:

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

to something like this:

$my_domain = $_SERVER['HTTP_HOST'];
$headers = 'From: orders@'.$my_domain."\r\n".'Reply-To: '.$senderEmail."\r\n";

and check if the e-mails are sent and received correctly now. (By setting the Reply-To Header, the correct Address is inserted when clicking on "Reply" in the Mail Client.)

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.