我正在尝试通过 way2sms api 发送短信并通过 phpmailer api 发送邮件,但问题是我在运行 php 时收到 3 条短信和 3 封邮件。
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$status = $_POST['status'];
$uname = $_POST['uname'];
$dept = $_POST['dept'];
$ename = $_POST['ename'];
require_once('dbConnect.php');
$sql = "SELECT name,email FROM user WHERE mobile='" . $uname . "'";
$check = mysqli_fetch_row(mysqli_query($con, $sql));
$username = $check[0];
$email = $check[1];
require_once 'smsapi/way2sms-api.php';
sendWay2SMS('username', 'password', $uname,' Message');
require_once 'mailapi/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'username@gmail.com'; // SMTP username
$mail->Password = 'password'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->setFrom('username@gmail.com', 'Name');
$mail->addAddress($email, $username);
$mail->isHTML(true);
$mail->Subject = 'Subject';
$mail->Body = 'Message';
!$mail->send();
}
?>
让所有数据库和其他代码正常工作。当我只尝试使用下面的代码发送短信时,它只发送一条消息
<?php
require_once 'smsapi/way2sms-api.php';
sendWay2SMS('username', 'password', $uname,' Message');
?>
或者当我只尝试使用下面的代码发送电子邮件时,它只发送一封电子邮件
<?php
require_once 'mailapi/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'username@gmail.com'; // SMTP username
$mail->Password = 'password'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->setFrom('username@gmail.com', 'Name');
$mail->addAddress($email, $username);
$mail->isHTML(true);
$mail->Subject = 'Subject';
$mail->Body = 'Message';
!$mail->send();
?>
安卓代码
public void sendMessage(View view){
StringRequest stringRequest = new StringRequest(Request.Method.POST, UPLOAD_URL1,
new Response.Listener<String>() {
@Override
public void onResponse(String s) {
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
try {
Toast.makeText(EventDetailActivity.this, volleyError.getMessage().toString(), Toast.LENGTH_LONG).show();
}catch (Exception e){
}
}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
String Status = ebtn.getText().toString().trim().toLowerCase();
SharedPreferences sharedPreferences = getSharedPreferences(SharedPrefConfig.SHARED_PREF_NAME,Context.MODE_PRIVATE);
String uname = sharedPreferences.getString(SharedPrefConfig.USERNAME_SHARED_PREF, null);
Map<String,String> params = new Hashtable<String, String>();
params.put("status", Status);
params.put("uname", uname);
params.put("dept", dept);
params.put("ename", name);
return params;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}