PHP发送响应android POST请求的电子邮件

时间:2016-11-11 19:41:52

标签: php android email android-volley phpmailer

我正在尝试创建一个简单的PHP脚本,当获取带有来自Android客户端的电子邮件的帖子请求时,会向该电子邮件发送欢迎电子邮件,但我总是得到意外的响应代码500

php code

<?php
/**
* This example shows settings to use when sending via Google's Gmail servers.
*/
//SMTP needs accurate times, and the PHP time zone MUST be set
//This should be done in your php.ini, but this is how to do it if you don't 

have access to that
date_default_timezone_set('Etc/UTC');
require('PHPMailer/class.phpmailer.php');
$response = array("error" => TRUE);
//Create a new PHPMailer instance
$mail = new PHPMailer();
//Tell PHPMailer to use SMTP
$mail->isSMTP();
//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug = 2;
//Ask for HTML-friendly debug output
$mail->Debugoutput = 'html';
//Set the hostname of the mail server
$mail->Host = 'smtp.gmail.com';
// use
// $mail->Host = gethostbyname('smtp.gmail.com');
// if your network does not support SMTP over IPv6
//Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
$mail->Port = 587;
//Set the encryption system to use - ssl (deprecated) or tls
$mail->SMTPSecure = 'tls';
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
//Username to use for SMTP authentication - use full email address for gmail
$mail->Username = "foodies.doof@gmail.com";
//Password to use for SMTP authentication
$mail->Password = "*****";
//Set who the message is to be sent from
$mail->setFrom('foodies.doof@gmail.com', 'Mahmoud Nabegh');
//Set an alternative reply-to address
//$mail->addReplyTo('replyto@example.com', 'First Last');
//Set who the message is to be sent to
$mail->addAddress($_POST['email']);
//Set the subject line
$mail->Subject = 'PHPMailer GMail SMTP test';
//Read an HTML message body from an external file, convert referenced images to embedded,
//convert HTML into a basic plain-text alternative body
$content    = "<b>Welcome foodie, we hope your stomach will find its love :D</b>";
$mail->MsgHTML($content);
//Replace the plain text body with one created manually
$mail->AltBody = 'This is a plain-text message body';
//Attach an image file
//$mail->addAttachment('images/phpmailer_mini.png');
//send the message, check for errors
if(!$mail->Send()) 
echo "Problem sending email.";
else 
echo "email sent.";

?>

android代码

public void mail(View v)
{
    String tag_string_req = "req_login";
    final String email = inputEmail.getText().toString();

    pDialog.setMessage("Logging in ...");
    showDialog();
    //new StringRequest(the request method to use,the url to fetch the string at,Listener to receive the String response)
    //the POST method is an HTTP request method which submits data to be processed by the API
    StringRequest strReq = new StringRequest(Method.POST,
            AppConfig.URL_LOGIN, new Response.Listener<String>() {

        @Override
        public void onResponse(String response) {
            Log.e(TAG, "Login Response: " + response.toString());
            hideDialog();

        }
    }, new Response.ErrorListener() {
        //volleyerror in sending the request to the API
        // OnErrorResponse is called when some volley error such as no internet connection or if URL is invalid occur.

        @Override
        public void onErrorResponse(VolleyError error) {
            Log.e(TAG, "Login Error: " + error.getMessage());
            Toast.makeText(getApplicationContext(),
                    error.getMessage(), Toast.LENGTH_LONG).show();
            hideDialog();
        }
    }) {

        @Override
        protected Map<String, String> getParams() {
            // Posting parameters to login url
            Map<String, String> params = new HashMap<String, String>();
            params.put("email", email);
            return params;
        }

    };

    // Adding request to request queue
    AppController.getInstance().addToRequestQueue(strReq, tag_string_req);
}

0 个答案:

没有答案