duanke1984 2015-07-29 04:55
浏览 34
已采纳

Phpmailer两次打电话给slim api

I am using Slim framework for creating my api. I am sending email using phpmailer. The mail is sending correctly. But my problem is that $mail->send() is firing my api twice due to which my response is wrong. Please tell me why $mail->send() is calling my api again in slim and how can I resolve it. Here is my code

$app->post('/requestPreview', function () use ($app) {
    //  $file = fopen('testphpmailer.txt',"w");
    verifyRequiredParams(array('fname', 'lname', 'email', 'event_code', 'app_version'));
    global $user_id, $condition;

    $response = array();

    $fname = $app->request->post('fname');
    $lname = $app->request->post('lname');
    $email = $app->request->post('email');
    $event_code = $app->request->post('event_code');
    $app_version = $app->request->post('app_version');
    //echo fwrite($file,"incoming vaiables: Firstname=".$fname);
    //echo fwrite($file,"incoming vaiables: Lastname=".$lname);
    //echo fwrite($file,"incoming vaiables: Email=".$email);
    //echo fwrite($file,"incoming vaiables: event_code=".$event_code);
    //echo fwrite($file,"incoming vaiables: app_version=".$app_version);

    $db = new DbHandler();
    //$db->sendmail($email);
    $res = $db->signup($fname, $lname, $email, $event_code, $app_version);

    //echo fwrite($file,"Result".$res['status']);
    if ($res['status'] == "one") {
        //echo fwrite($file,"Inside If".$res['status']);

        $password = $res['password'];

        $mail = new PHPMailer();
        $mail->IsSMTP();
        $mail->SMTPDebug = 1;
        $mail->Host = "smtp.gmail.com";
        $mail->SMTPAuth = true;
        $mail->IsHTML(true);
        $mail->Username = "abc@gmail.com";
        $mail->Password = "*******";
        $mail->SMTPSecure = "ssl";
        $mail->Port = 465;
        $mail->From = "abc@gmail.com";
        $mail->AddAddress("abc@gmail.com");
        $mail->Subject = "Welcome to www.abc.com .Your Account has been created and your password is:";
        $mail->Body = "Welcome to www.abc.com .Your Account has been created and your password is:";
        $mail->Send(); /*When this line runs then it it send the gmail and my api is called  and below mentioned response is never executed*/

        $response["event_code"] = $event_code;
        $response["email"] = $email;
        $response["fname"] = $fname;
        $response["lname"] = $lname;
        $response["CreateUser"] = false;
        echoRespnse(200, $response);

    } else {
        // echo fwrite($file,"Inside else = ".$res['status']);

        $response["error"] = "already in system";
        $response["fname"] = "null";
        $response["lname"] = "null";
        echoRespnse(200, $response);
    }
    //echo fwrite($file,"Hello World. Testing!");
    //fclose($file);
});
  • 写回答

1条回答 默认 最新

  • dongmin4052 2015-07-31 06:20
    关注

    Actually I was fetching the response from json. But when I fetched the response from Http urlconnection using asynchronous task and the my phpmailer problem was also resolved and everything is working fine now.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥15 一道python难题3
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试