ice
ice

Reputation: 171

Send EditText value from android to PHP file and execute PHPmailer send() function

So I want to execute a PHP file which is in my server (I'm using XAMPP as my local server). My android application will get the input through the app then in my java code, it will execute the link that I have given. The problem is I don't know how to pass the input to my PHP file. My code only executes the link that I provided but never pass any value.

The code I use to execute the mailer.php file

String link = "http://192.168.1.3/api/roadtrip/mailer.php"; 
new updateData().execute(link);

My updateDate() class

public class updateData extends AsyncTask<String, String, String> {

    @Override
    protected String doInBackground(String... params) {
        HttpURLConnection conn = null;

        try {
            URL url;
            url = new URL(params[0]);
            conn = (HttpURLConnection) url.openConnection();
            if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
                InputStream is = conn.getInputStream();
            } else {
                InputStream err = conn.getErrorStream();
            }
            return "Done";
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (conn != null) {
                conn.disconnect();
            }
        }
        return null;
    }
}

My mailer.php file

require 'phpmailer/src/PHPMailer.php';
require 'phpmailer/src/SMTP.php';
require 'phpmailer/src/Exception.php';

$mail = new PHPMailer\PHPMailer\PHPMailer();

    //Enable SMTP debugging. 
    $mail->SMTPDebug = 2;                               
    //Set PHPMailer to use SMTP.
    $mail->isSMTP();      

    //Set SMTP host name                          
    $mail->Host = 'smtp.gmail.com';
    //Set this to true if SMTP host requires authentication to send email
    $mail->SMTPAuth = true;                          
    //Provide username and password     
    $mail->Username = "my email";                 
    $mail->Password = "email pass";                           
    //If SMTP requires TLS encryption then set it
    $mail->SMTPSecure = "ssl";                           
    //Set TCP port to connect to 
    $mail->Port = 465;    

    $mail->From = "[email protected]";
    $mail->FromName = "RoadTrip";

    $mail->addAddress($emailAddress);

    $mail->isHTML(true);

    $mail->Subject = "RoadTrip Account Verification";
    $mail->Body = "<i>Please click the link below to verify your account</i>";
    $mail->AltBody = "Insert Link here";
    $mail->send();

I want to send the 'emailAddress' from my android application to the php file then PHP mailer to send the email.

Upvotes: 0

Views: 171

Answers (1)

Synchro
Synchro

Reputation: 37810

You're missing some fundamentals here.

First of all, you need to submit the values you want to use - at the moment you're just hitting a static URL with no params, or no POST body.

At the receiving end (PHP), you would normally expect such params to turn up in the $_GET, $_POST, or $_REQUEST PHP superglobals.

So if you submitted the request to

http://192.168.1.3/api/roadtrip/[email protected]

You would expect $_GET['email'] to contain [email protected].

You need to figure out how to pass in your parameters correctly, and you probably want to use POST rather than GET for this kind of request, but that's rather too big a question for here that is far better covered in any basic HTTP documentation, so get reading!

Upvotes: 1

Related Questions