简体   繁体   中英

How To Send Email from An ionic App without using email composer plugin?

I am trying to send a E-Mail from my ionic app using Ajax call to my php code which i uploaded on my server.

Code for the Ajax call:

$scope.forget = function(){

      $http({

        method: 'POST',

        url: 'server_name/mail.php',

        data: { mailTo: 'name@gmail.com', msg: 'hello!', sub: 'hey' }
      }).then(function successCallback(response) {

          alert(response.value+"msg sent!");
        },
        function errorCallback(response) {

          alert(response.value+"error with sending msg");
        });
    };

mail.php

<?php 
    $_SERVER["PHP_SELF"];
    $to = $_POST["mailTo"];
    $txt = $_POST["msg"];
    $subject = $_POST["sub"];
    $headers = "From: emailadd@gmail.com";
    echo $subject;
    $flgchk = mail ($to, $subject, $txt, $headers); 

    echo $flgchk;
    if($flgchk){

      echo "<script>alert('Thank you for submiting your Details..!')</script>";
     }
    else{

      echo "<script>alert('Error Occured Please Resend Details..!')</script>"; 
    }

?>

But i am not able to send the details .It is going into error call back response and shows error with sending msg. Can i anyone helpme with this ? or Is there any other method by which i can send an email oon button click?Thank you !

Did you tried with typing your full URL (including HOST and protocols) to url property? I asked because it seems you're using ionic for hybrid app dev.

Make sure your PHP server also enabled CORS (cross origin resources sharing), if your app is hybrid mobile app OR hosted in another domain.

header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: POST, GET, OPTIONS');

Put this 2 line top of your PHP codes, that'll allow your server get request from cross-domain.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM