邮件功能在zend框架2中不起作用:无法看到错误

时间:2015-05-06 20:57:46

标签: ajax zend-framework2

我在这里有这个观点:

public function testAction() {
        if ($this->getRequest()->isPost()) {
            if ($this->getRequest()->getPost()) {

                $row = array(
                    "callmessage" => $this->getRequest()->getPost('callmessage'));


                $message = $row["callmessage"];

                if (strlen($message) >= 70) {
                    $message = wordwrap($msg, 70);
                }


                $to = 'john.doe@gmail.com';
                $headers = 'MIME-Version: 1.0' . "\n";
                $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\n";
                $from = "";

                $subject = " vous a écri sur servicesautismenb.com";
                $messageTosend = "<html>
                                            <head>
                                                    <title> vous a écri sur servicesautismenb.com</title>
                                            </head>
                                            <body>
                                                   <p>Message de :</p>
                                                   <p>Courriel :</p>
                                                   <p>" . $message . "</p>
                                            </body>
                                    </html>";


                if (mail($to, $subject, $messageTosend, $headers)) {
                    $data = array(
                        'result' => "success"
                    );
                } else {
                    $data = array(
                        'result' => "failed"
                    );
                }
            }
        }
        return $this->getResponse()->setContent(Json::encode($data));
    }

通过这个ajax代码调用:

    $.ajax({
                    url: '/test',
                    type: "POST",
                    data: {callmessage: message},
                    success: function(response) {
                        var el = JSON.parse(response);
                        alert(el.result);
                    }, 
                     error: function(xhr, textStatus, error) {
                        console.log(xhr.statusText);
                        console.log(textStatus);
                        console.log(error);
                    }
                });

我没有收到任何消息,无论是成功还是错误,电子邮件也没有被发送。

感谢您帮助我。

1 个答案:

答案 0 :(得分:0)

使用ZF2发送邮件非常简单。您有要设置的邮件对象,然后是要传输的传输。

任何发生的废话都可能发生在运输步骤。

        $part           = new \Zend\Mime\Part($your_html_message_here);
        $part->encoding = \Zend\Mime\Mime::ENCODING_QUOTEDPRINTABLE;
        $part->type     = "text/html";
        $body           = new \Zend\Mime\Message();
        $body->setParts([$part]);


        $mail       = new \Zend\Mail\Message();
        $mail->setBody( $body );
        $mail->setSubject( $subject );
        $mail->addFrom( 'sender@email.com', 'Sender Name' );
        $mail->addTo( 'to@recipient.com' );
        $mail->setEncoding("utf-8");

        try{
            $transport = new \Zend\Mail\Transport\Smtp();
            $transport->setOptions(
                new \Zend\Mail\Transport\SmtpOptions(
                    array(
                        'host'              => 'mailhost.com',
                        'connection_class'  => 'login',
                        'port'              => 587,
                        'connection_config' => array(
                            'username' => 'smtplogin@smtp.com',
                            'password' => 'smtp_password',
                        ),
                    )
                )
            );
            $transport->send( $mail );
        }
        catch( \Exception $x )
        {
            echo $x->getMessage();
            die( "mail exploded" );
        }

填写上面代码中的变量

(编辑:Google SMTP选项如此) new \Zend\Mail\Transport\SmtpOptions( array( 'host' => 'smtp.gmail.com', 'connection_class' => 'login', 'port' => 587, 'connection_config' => array( 'username' => 'your@google.com', 'password' => 'password123', 'ssl' => 'tls', ), ) )