如何使用Volley库在Android中使用phpmailer从localhost发送电子邮件?

时间:2016-05-17 21:49:15

标签: php android api android-volley phpmailer

我的应用中有一个登录和注册系统。我想添加“忘记密码”字段。如果任何用户忘记了密码,用户可以通过单击“忘记密码”按钮获取密码。如果注册用户单击“忘记密码”按钮,将使用代码将电子邮件发送到他给定的电子邮件地址。

  1. 这是我的Java代码:

           public class ForgotPassword extends AppCompatActivity implements View.OnClickListener {
    
        EditText et_email, et_name, et_dob;
        String email, name, dob;
        Button recoverPassword;
    
       @Override
           protected void onCreate(Bundle savedInstanceState) {
             super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_forgot_password);
    
    
    et_email = (EditText) findViewById(R.id.et_rp_email);
    et_name = (EditText) findViewById(R.id.et_rp_name);
    et_dob = (EditText) findViewById(R.id.et_rp_bd);
    
    recoverPassword = (Button)findViewById(R.id.recoverPassword);
    
    recoverPassword.setOnClickListener(this);
    
    
       }
    
          private void recoverPassword() {
    email = et_email.getText().toString().trim();
    name = et_name.getText().toString().trim();
    dob = et_dob.getText().toString().trim();
    
    
    
    
    StringRequest stringRequest = new StringRequest(Request.Method.POST, Config.FORGOTPASSWORD_URL,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    if(response.trim().equals("success")){
                        sendEmail();
    
                        SharedPreferences sharedPreferences = ForgotPassword.this.getSharedPreferences(Config.SHARED_PREF_NAME, Context.MODE_PRIVATE);
    
                        //Creating editor to store values to shared preferences
                        SharedPreferences.Editor editor = sharedPreferences.edit();
    
                        //Adding values to editor
                        editor.putBoolean(Config.LOGGEDIN_SHARED_PREF, true);
                        editor.putString(Config.EMAIL_SHARED_PREF, email);
    
    
                        //Saving values to editor
                        editor.commit();
    
    
                    }else{
                        Toast.makeText(ForgotPassword.this, response, Toast.LENGTH_LONG).show();
                    }
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Toast.makeText(ForgotPassword.this,error.toString(),Toast.LENGTH_LONG ).show();
                }
            }){
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            Map<String,String> map = new HashMap<String,String>();
            map.put(Config.KEY_EMAIL,email);
            map.put(Config.KEY_NAME,name);
            map.put(Config.KEY_DOB,dob);
            return map;
        }
    };
    
    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(stringRequest);
       }
    
    
          @Override
         public void onBackPressed() {
    
    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
    alertDialogBuilder.setMessage("Would you like to exit?");
    
    alertDialogBuilder.setPositiveButton("yes", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface arg0, int arg1) {
            Toast.makeText(ForgotPassword.this, "You clicked yes button", Toast.LENGTH_LONG).show();
            SharedPreferences preferences = getSharedPreferences(Config.SHARED_PREF_NAME, Context.MODE_PRIVATE);
            //Getting editor
            SharedPreferences.Editor editor = preferences.edit();
            editor.clear();
            editor.commit();
            Intent intent = new Intent(ForgotPassword.this, Login.class);
            startActivity(intent);
    
        }
    });
    
    alertDialogBuilder.setNegativeButton("No", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            Toast.makeText(ForgotPassword.this, "You clicked No button", Toast.LENGTH_LONG).show();
            //finish();
        }
    });
    
    AlertDialog alertDialog = alertDialogBuilder.create();
    alertDialog.show();
       }
    
       private void sendEmail(){
    
    Intent emailIntent = new Intent(Intent.ACTION_SENDTO);
    emailIntent.setData(Uri.parse("mailto:" + "recipient@example.com"));
    emailIntent.putExtra(Intent.EXTRA_SUBJECT, "My email's subject");
    emailIntent.putExtra(Intent.EXTRA_TEXT, "My email's body");
    
    try {
        startActivity(Intent.createChooser(emailIntent, "Send email using..."));
    } catch (android.content.ActivityNotFoundException ex) {
        Toast.makeText(ForgotPassword.this, "No email clients installed.", Toast.LENGTH_SHORT).show();
    }
    
      }
    
    
       @Override
      public void onClick(View v) {
    if (v == recoverPassword){
        recoverPassword();
    }
    
        }
    
         }
    
  2. 这是我的PHP代码:

      <?php 
      require 'PHPMailer/PHPMailerAutoload.php';
    
      if($_SERVER['REQUEST_METHOD']=='GET'){
      $Name = $_GET['Name'];
      $Email = $_GET['Email'];
      //$DOB = $_GET['DOB'];
      $Phone = $_GET['Phone'];
    
      $to      = 'nameyour174@gmail.com';
     $subject = 'Fake sendmail test';
       $message = 'If we can read this, it means that our fake Sendmail works';
     $headers = 'From: webmaster@sunkingbdltd.com';
    
    
           //Creating sql query
    
    
          $sql = "SELECT * FROM user WHERE Name='$Name' AND Email='$Email' AND Phone='$Phone'";
    
       //importing dbConnect.php script 
          require_once('dbConnect.php');
    
          //executing query
            $result = mysqli_query($con,$sql);
    
      //fetching result
       $check = mysqli_fetch_array($result);
    
          //if we got some result  
    
          if(isset($check)){
       //displaying success 
       echo "success";
    
      if(mail($Email, $subject, $message, $headers)) {
        echo 'Email sent successfully!';
        } else {
           die('Failure: Email was not sent!');
       }
    
         }else{
           //displaying failure
              echo "failure";
               }
            mysqli_close($con);
             }
    
       ?>
    

0 个答案:

没有答案