Cakephp:在结账时向客户发送邮件时出错

时间:2016-06-26 08:26:08

标签: php cakephp smtp cakephp-2.0

我正在从事网上购物项目。我的本地项目工作得很好我可以结账并能够接收邮件但是当我试图在实时服务器上结帐时我收到错误:Mailer Error: SMTP connect() failed.Mailer Error: SMTP connect() failed.我正在使用CakePHP 2.7

这是我发送邮件的功能

public function send_mail($address,$subject,$body)
{
        App::import('Vendor', 'mail_class/PHPMailer');
        $username="viky.031290@gmail.com";
        $password="XXXXXXXX";


        $mail             = new PHPMailer();




        $mail->IsSMTP(); // telling the class to use SMTP
        $mail->Host       = "smtp.gmail.com"; // SMTP server
        $mail->SMTPDebug  = 0;                     // enables SMTP debug information (for testing)
                                                               // 1 = errors and messages
                                                               // 2 = messages only
        $mail->SMTPAuth   = true; 
        $mail->SMTPSecure = 'tls';                 // enable SMTP authentication
        $mail->Host       = "smtp.gmail.com"; // sets the SMTP server
        $mail->Port       = 587;                    // set the SMTP port for the GMAIL server
        $mail->Username   = $username; // SMTP account username
        $mail->Password   = $password;        // SMTP account password

        $mail->SetFrom($username, 'Orangemart');



        $mail->Subject    = $subject;



        $mail->MsgHTML($body);


        $mail->AddAddress($address, "");

        //$mail->AddAttachment("images/phpmailer.gif");      // attachment
        //$mail->AddAttachment("images/phpmailer_mini.gif"); // attachment

        if(!$mail->Send()) 
        {
             echo "Mailer Error: " . $mail->ErrorInfo;
        } 
        else 
        {
            echo "Message sent!";
        }
    }

这是我的结帐功能

public function checkout()
{
    $this->layout="checkout";

    $this->loadModel("Cart");
    $cartcount = $this->Cart->find('count',array(
                                            'conditions' => array('public_id' => $this->Session->read("id"))
                                        )
                                );
    $this->set('count', $cartcount);

    if(isset($_REQUEST["checkout"]))
    {
        $this->loadModel("Ordermaster");
        $this->loadModel("Cart");
        $this->loadModel("Orderdetail");
        $this->request->data["date"] = date('y-m-d');
        $this->request->data["publics_tbls_id"] = $this->Session->read("id");

        $return=$this->Ordermaster->save($this->request->data);
        //print_r($return);
        $this->Session->write("orderid",$return["Ordermaster"]["id"]);

        $dt=$this->Cart->find("all",array("conditions"=>array("public_id"=>$this->Session->read("id"))));

        $str="<div class='top' style='height:100px;width:500px;background-color:Orange;color:white'><center><h1 style='padding:25px;font-size:50px';>OrangeMart</h1></center></div>";

        $str.="<h3>Detail Order</h3>
    <ul>
        <li><b>Date:</b>".$return["Ordermaster"]["date"]."</li>
        <li><b>Order ID:</b>".$return["Ordermaster"]["id"]."</li>
    </ul>";
            $str.="<table border='2'>";
                    $str.="<tr height='50px'>
                                <td align='center' bgcolor='black'><font color='white'><b>Product Name</b></font></td>
                                <td align='center' bgcolor='black'><font color='white'><b>Qty</b></font></td>
                                <td align='center' bgcolor='black'><font color='white'><b>Price</b></font></td>
                            </tr>";
        $subtotal=0;
        foreach($dt as $row)
        {
                $this->request->data["ordermaster_id"]=$return["Ordermaster"]["id"];
                $this->request->data["product_tbls_id"]=$row["Cart"]["product_tbls_id"];
                $this->request->data["qty"]=$row["Cart"]["qty"];
                $this->request->data["psize"]=$row["Cart"]["psize"];
                $this->request->data["price"]=$row["product_tbls"]["price"];

                $this->Orderdetail->create();
                $this->Orderdetail->save($this->request->data);


                    $str.="<tr height='50px'>";
                            $str.=" <td align='center'>".$row["product_tbls"]["name"].(($row["Cart"]["psize"]!==Null)?' (Size '.$row["Cart"]["psize"].')':"")./*" (Size ".$row["Cart"]["psize"].")".*/"</td>
                                <td align='center'>".$row["Cart"]["qty"]."</td>
                                <td align='center'> Rs. ".$row["Cart"]["price"]."</td>";
                    $str.="</tr>";
            $subtotal=$subtotal+$row["Cart"]["price"];
        }
        $str.="<tr height='50px'><td colspan='2' align='center' bgcolor='black'><font color='white'><b>SubTotal</b></td><td align='center'><b>".' Rs. '.$subtotal."</b></font></td></tr>
            <tr height='50px'><td colspan='2' align='center' bgcolor='black'><font color='white'><b>Payment Method</b></td><td align='center'><b>"."Cash on Delevery"."</b></font></td></tr>";

        if($subtotal>2000)
        {
            $str.="<tr height='50px'><td colspan='2' align='center' bgcolor='black'><font color='white'><b>Shipping Cost</b></td><td align='center'><b>FREE</b></font></td></tr>";
            $str.="<tr height='50px'><td colspan='2' align='center' bgcolor='black'><font color='white'><b>Total</b></td><td align='center'><b> Rs. ".$subtotal."</b></font></td></tr>";
        }
        else
        {
            $str.="<tr height='50px'><td colspan='2' align='center' bgcolor='black'><font color='white'><b>Shipping Cost</b></td><td align='center'><b> Rs. 99</b></font></td></tr>";
            $str.="<tr height='50px'><td colspan='2' align='center' bgcolor='black'><font color='white'><b>Total</b></td><td align='center'><b> Rs. ".($subtotal+99)."</b></font></td></tr>";
        }

        $str.="</table>";

        $str.="</br><ul>
                        <li><b>Note:</b> ".$return["Ordermaster"]["note"]."</li>
                    </ul>";

        $str.="<h3>Customer details</h3>
            <ul>
                <li><b>Name:</b> ".$return["Ordermaster"]["name"]."</li>
                <li><b>Email:</b> ".$return["Ordermaster"]["email"]."</li>
                <li><b>Contact No:</b> ".$return["Ordermaster"]["contact"]."</li>
                <li><b>Address:</b> ".$return["Ordermaster"]["address"]."</li>
                <li><b>Pincode:</b> ".$return["Ordermaster"]["pincode"]."</li>
            </ul>";

        $this->send_mail($this->request->data["email"],"OrangeMart",$str);
        $this->send_mail("viky.031290@gmail.com","New Order",$str);

        $this->Cart->query("delete from cart_tbls where public_id=".$this->Session->read("id"));
        $this->redirect("../public/thankyou");
            //$this->Session->setFlash(__('My message.'), 'flash_notification');
            //$this->Session->setFlash('Successully added to cart!');

    }

    $this->loadModel("Addcat");
    $data = $this->Addcat->find('all');
    $this->set('cat', $data);

    $this->loadModel("Subcat");
    $data = $this->Subcat->query('select * from subcategory_tbls where sub_cat IN ("jeans","T-shirt","dresses","top")');
    $this->set('product1', $data);

}

0 个答案:

没有答案