Advertisement
Guest User

Untitled

a guest
Jul 25th, 2017
465
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.90 KB | None | 0 0
  1. <?php
  2. require 'database/connect.php';
  3. global $connect;
  4. date_default_timezone_set('Etc/UTC');
  5. require 'PHPMailer-master2/PHPMailerAutoload.php';
  6.  
  7. if ( isset($_POST['data1']) && isset($_POST['data2']))
  8. {
  9. $data1 = $_POST['data1'];
  10. $data2 = $_POST['data2'];
  11.  
  12. $sql = "SELECT * FROM table WHERE data1 = '$data1' AND data2='$data2'";
  13. $result = mysqli_query($connect, $sql);
  14. if ($result && mysqli_num_rows($result) > 0)
  15. {
  16. while ($row = mysqli_fetch_array($result)){
  17. }
  18.  
  19. $output = array('message' => '1');
  20. echo json_encode($output);
  21. $add = "INSERT INTO table (data1, data2)
  22. VALUES ('$data1','$data2')
  23. ";
  24. $run = mysqli_query($connect,$add);
  25. $mail = new PHPMailer;
  26. $mail->isSMTP();
  27. $mail->Host = 'smtp.gmail.com';
  28. $mail->SMTPAuth = true;
  29. $mail->Username = 'gmail.com';
  30. $mail->Password = '******';
  31. $mail->SMTPSecure = 'tls';
  32. $mail->Port = 587;
  33. $mail->setFrom('sender@mail.com', 'sender');
  34. $mail->addAddress('receiver@mail.com','receiver');
  35. $mail->isHTML(true);
  36. $mail->Subject = 'Test';
  37. $mail->Body = 'Test';
  38. $mail->AltBody = 'Test';
  39. if(!$mail->send()) {
  40. echo json_encode([
  41. 'status' => false,
  42. 'message' => 'Message could not be sent. Error: ' . $mail->ErrorInfo
  43. ]);
  44. } else {
  45. $status = array();
  46. $status[] = array('status' => '1');
  47. }
  48.  
  49. $output = array('message' => '1', 'status' => $status);
  50. echo json_encode($output);
  51. exit();
  52. // End sending email
  53. exit();
  54.  
  55. mysqli_free_result($result);
  56. }
  57. else {}
  58. }
  59. ?>
  60.  
  61. @IBAction func sendApplyMovement(_ sender: Any) {
  62. let url = URL(string: "http://localhost/send.php")
  63. let session = URLSession.shared
  64. let request = NSMutableURLRequest(url: url! as URL)
  65. request.httpMethod = "POST"
  66. let valueToSend = "data1=&data2"
  67. request.httpBody = valueToSend.data(using: String.Encoding.utf8)
  68.  
  69. let myAlert = UIAlertController(title: "Confirm", message: "Sure ?", preferredStyle: UIAlertControllerStyle.alert)
  70. let cancel = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.default, handler: nil)
  71. let okaction = UIAlertAction(title: "Yes", style: UIAlertActionStyle.default, handler:
  72. {
  73. action in
  74.  
  75. let task = session.dataTask(with: request as URLRequest, completionHandler: {
  76. (data, response, error) in
  77. if error != nil {
  78. return
  79. }
  80. else {
  81. do {
  82. if let json = try JSONSerialization.jsonObject(with: data!) as? [String: String]
  83. {
  84. DispatchQueue.main.async {
  85. let message = Int(json["message"]!)
  86. let status = Int(json["status"]!)
  87.  
  88. if(message == 1){
  89. if(status == 1){
  90. print("Success")
  91. let myViewController:ViewController = self.storyboard!.instantiateViewController(withIdentifier: "ViewController") as! ViewController
  92. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  93. let navigationController = UINavigationController.init(rootViewController: myViewController)
  94. appDelegate.window?.rootViewController = navigationController
  95. appDelegate.window?.makeKeyAndVisible()
  96.  
  97. let myAlert = UIAlertController(title: "Success!", message: "Sent !", preferredStyle: UIAlertControllerStyle.alert)
  98. myAlert.addAction(UIAlertAction(title: "Okay", style: UIAlertActionStyle.default, handler: nil))
  99. navigationController.present(myAlert, animated: true, completion: nil)
  100. return
  101. }
  102.  
  103. }
  104. else {return}
  105. }
  106. }
  107. }
  108. catch let parseError { print("Parse error: (parseError.localizedDescription)") }
  109. }
  110. })
  111. task.resume()
  112.  
  113. }
  114. )
  115. myAlert.addAction(okaction)
  116. myAlert.addAction(cancel)
  117. self.present(myAlert, animated: true, completion: nil)
  118. }
  119. }
  120.  
  121. if let json = try JSONSerialization.jsonObject(with: data!)
  122.  
  123. if(!$mail->send())
  124. {
  125. echo 'Message could not be sent.';
  126. echo 'Mailer Error: ' . $mail->ErrorInfo;
  127. }
  128. else
  129. {
  130. echo 'Message has been sent';
  131. }
  132.  
  133. if(!$mail->send()) {
  134. echo json_encode([
  135. 'status' => false,
  136. 'message' => 'Message could not be sent. Error: ' . $mail->ErrorInfo
  137. ]);
  138. } else {
  139. echo json_encode([
  140. 'status' => true,
  141. 'message' => 'Message sent'
  142. ]);
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement