8

ホストとしてGmailアカウントを使用して単純なメールアプリケーションを開発しています.それは魅力のように機能しますが、send()関数が例外をスローすると問題が発生します. try catch ステートメントでは例外を処理できないことがわかりました。グローバル例外クラスを使用しても機能しません。この質問はどこかでも議論されています。

例えば ​​:

Symfony2 dev 環境コントローラーで swiftmailer 例外をキャッチする

また

https://groups.google.com/forum/#!topic/symfony2/SlEzg_PKwJw

しかし、彼らは有効な答えに達しませんでした。

私のコントローラー機能コードは次のとおりです。

    public function ajaxRegisterPublisherAction()
    {

//some irrelevant logic

     $returns=  json_encode(array("username"=>$username,"responseCode"=>$responseCode));


    try{
            $message = \Swift_Message::newInstance()
        ->setSubject('hello world')
        ->setFrom('jafarzadeh91@gmail.com')
        ->setTo('jafarzadeh991@yahoo.com')
        ->setBody(
            $this->renderView('AcmeSinamelkBundle:Default:email.txt.twig',array('name'=>$username,"password"=>$password))
        );
      $this->container->get("mailer")->send($message);

    }
   catch (Exception $e)
    {

    }



    return new \Symfony\Component\HttpFoundation\Response($returns,200,array('Content-Type'=>'application/json'));


    }

上記のコードから送信され、firebug コンソールで受信した応答は次のとおりです。

{"username":"xzdvxvvcvxcv","responseCode":200}<!DOCTYPE html>
<html>
    .
    .
Swift_TransportException: Connection could not be established with host smtp.gmail.com [Unable to find the socket transport "ssl" - did you forget to enable it when you configured PHP?]
    .
    .
</html>

カーネルがjsonオブジェクトの継続で例外を処理する理由がわからないため、髪をキャッチしますか?

この行にコメントすると:

 $this->container->get("mailer")->send($message);

例外は発生せず、クライアント側に有効な json があります (当然のことですが) or or even !に変更Exceptionしました。しかし、良い結果はありません。\Exception\Swift_TransportExceptionSwift_TransportException

4

2 に答える 2

3

$this->container->get("mailer")->send($message);スプールがオンになっている場合、その時点で電子メール メッセージは送信されません。http://symfony.com/doc/current/cookbook/email/spool.htmlを参照してください

のデフォルト設定がある場合spool: { type: memory }\Swift_TransportExceptionコントローラーが終了した後、カーネル終了フェーズ中に がスローされます。これを回避する 1 つの方法は、スプーリングをオフにすることです (ただし、ユーザーは電子メールが送信されるまで待たなければならない場合があります)。または、独自のイベント リスナーを作成して例外を処理することもできます。http://symfony.com/doc/current/cookbook/service_container/event_listener.html

于 2014-10-11T21:55:46.477 に答える
2

例外を送り返す前にイベントディスパッチャーを打ち負かす必要があるため、これらの種類のイベントをリッスンして黙らせますが、これは悪い方法だと思います

class SuchABadRequest implements \Swift_Events_TransportExceptionListener{

    /**
     * Invoked as a TransportException is thrown in the Transport system.
     *
     * @param \Swift_Events_TransportExceptionEvent $evt
     */
    public function exceptionThrown(\Swift_Events_TransportExceptionEvent $evt)
    {

        $evt->cancelBubble();

        try{
            throw $evt->getException();
        }catch(\Swift_TransportException $e){
            print_r($e->getMessage());
        }
    }
}

コントローラー内部

$mailer = $this->get('mailer');

$mailer->registerPlugin(new SuchABadRequest());
于 2013-10-08T15:00:37.047 に答える