send Email with PHPMailer and ionic 3

2019-08-26 20:27发布

I'm building an application and one of the tasks in this app is sending is emails to the users,

this is my php code

<?php
require_once __DIR__ . '/connect.php';
require 'phpmailer/PHPMailerAutoload.php';

$response = array();
 $_POST['fristName']=" null";
 $_POST['lastName']=" null";
$_POST['email']=" null";
$_POST['phoneNumber']=" null";
 if($_POST['fristName']&&$_POST['email']&&$_POST['phoneNumber']){
 	
 	
$mail = new PHPMailer;
$mail->setFrom($_POST['email'], $_POST['fristName'].' '.$_POST['lastName']." - Application mobile");
$mail->addAddress('exemple.xx@xxxxx.com', 'Mobile Android');
$mail->Subject  = 'Email from : '.$_POST['email'];
 
            $message = utf8_encode(htmlentities("hi dear", ENT_QUOTES, "UTF-8"));


            $message .="<p>".utf8_encode(htmlentities($_POST['fristName'], ENT_QUOTES, "UTF-8"))."</p>"
			$message .="<p>".utf8_encode(htmlentities($_POST['lastName'], ENT_QUOTES, "UTF-8"))."</p>"
			$message .="<p>".utf8_encode(htmlentities($_POST['email'], ENT_QUOTES, "UTF-8"))."</p>"
			$message .="<p>".utf8_encode(htmlentities($_POST['phoneNumber'], ENT_QUOTES, "UTF-8"))."</p>"
            

            $message .="<br/><br/>Cordialement,";

$mail->Body  = $message;
$mail->IsHTML(true); 
if(!$mail->send()) {

   $response["success"] = 0;
    $response["message"] = "Email not sent";
} else {
    $response["success"] = 1;
    $response["message"] = "succes";
}

}

and this is my page contact.ts

sendEmail(){

      var headers = new Headers();
    headers.append("Accept", 'application/json');
    headers.append('Content-Type', 'application/json' );
    const requestOptions = new RequestOptions({ headers: headers });

    let postData = {
        fristName: this.fristName,
        lastName: this.lastName,
        email:this.Email,
        phoneNumber:this.phoneNumber,
    }

    this.http.post("http://www.android.transatour.ma/contact_script.php",postData, requestOptions)
      .subscribe(data => {
        console.log(data['_body']);
       }, error => {
        console.log(error);
      });

      //console.log(this.fristName+" "+this.lastName+" "+this.Email+" "+this.phoneNumber);  
  }

i had success to send the email but all field give me nothing

i have no idea what i'm doing wrong

this is an image to better understanding

1条回答
神经病院院长
2楼-- · 2019-08-26 21:02

Is your PHP script receive Data from the Ionic application, I think your post request is not sent any data along with the request your PHP script Accept form-data so you need to add your Ionic application data like this

 send(){
    var headers = new Headers();
    headers.append("Accept", "application/json");
    let options = new RequestOptions({ headers: headers });

    let body = new FormData();
    body.set("fristName",this.fristName);
    body.set('lastName', this.lastName);
    body.set("phoneNumber", this.phoneNumber);
    body.set("email", this.email);
    return this.http
      .post("http://www.android.transatour.ma/contact_script.php", body, options)
      .map(res => res.json());
}

Try it.

查看更多
登录 后发表回答