Guest User

Untitled

a guest
Mar 15th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.30 KB | None | 0 0
  1. <!-- index.php -->
  2. <?php include("partials/header.php");
  3. include("functions/register.php");
  4. include("functions/invitefriend.php"); ?>
  5.  
  6. <!-- ...preceding html.... -->
  7.  
  8. <!-- Invite a friend form modal -->
  9. <div class="modal fade" tabindex="-1" role="dialog" id="emailFriend" aria-hidden="true">
  10. <div class="modal-dialog modal-dialog-centered" role="document">
  11. <div class="modal-content">
  12. <div class="modal-header">
  13. <h5 class="modal-title">Share with a friend or colleague via email: </h5>
  14. <button type="button" class="close" data-dismiss="modal" aria-label="Close">
  15. <span aria-hidden="true">&times;</span>
  16. </button>
  17. </div>
  18. <div class="modal-body">
  19. <form class="loginForm" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" id="formRefer">
  20. <div class="form-group">
  21. <label for="fromName">Your full name: </label> <span id="fromNameFeedback"></span>
  22. <input class="form-control" type="text" name="fromName" id="fromName" placeholder="Your full name" required autofocus="true">
  23. </div>
  24. <div class="form-group">
  25. <label for="fromEmail">Your email: </label> <span id="fromEmailFeedback"></span>
  26. <input class="form-control" type="email" name="fromEmail" id="fromEmail" placeholder="Your email" required>
  27. </div>
  28. <div class="form-group">
  29. <label for="toName">Your friend's name: </label> <span id="toNameFeedback"></span>
  30. <input class="form-control" type="text" name="toName" id="toName" placeholder="Your friend's name" required>
  31. </div>
  32. <div class="form-group">
  33. <label for="toEmail">Your friend's email: </label> <span id="toEmailFeedback"></span>
  34. <input class="form-control" type="email" name="toEmail" id="toEmail" placeholder="Your friend's email" required>
  35. </div>
  36. <div class="btn-group" data-toggle="buttons">
  37. <p>This person is a: </p>
  38. <label class="btn btn-info active">
  39. <input type="radio" name="relationship" id="friend" value="friend" autocomplete="off" checked>Friend
  40. </label>
  41. <label class="btn btn-info">
  42. <input type="radio" name="relationship" id="colleague" value="colleague" autocomplete="off">Colleague
  43. </label>
  44. </div>
  45. <div>
  46. <div id="message2" class="questions"><?php echo $message2; ?> </div>
  47. </div>
  48. <div>
  49. <div id="message3" class="questions"> </div>
  50. </div>
  51. <div class="modal-footer">
  52. <input class="btn btn-info loginlogin loginBtn" type="submit" name="send" id="referSubmitButton" value="Send" href="javascript:void(0)">
  53. <button type="button" class="btn btn-dark" data-dismiss="modal">Cancel</button>
  54. </div>
  55. </form>
  56. </div>
  57. </div>
  58. </div>
  59. </div>
  60. <?php include("partials/footer.php"); ?>
  61.  
  62. <?php
  63. //require PHPMailer and autoload
  64. use PHPMailerPHPMailerPHPMailer;
  65. require 'vendor/autoload.php';
  66.  
  67. //Invite a friend via email form handler
  68. $fromName = strip_tags($_POST["fromName"]);
  69. $fromEmail = strip_tags($_POST["fromEmail"]);
  70. $toName = strip_tags($_POST["toName"]);
  71. $toEmail = strip_tags($_POST["toEmail"]);
  72. // $emailMsg = $_POST["emailMsg"];
  73. $relationship = $_POST["relationship"];
  74. $message2 = "";
  75.  
  76.  
  77. if ($_SERVER["REQUEST_METHOD"] == "POST"){
  78. //Validate form
  79. $err = false;
  80. //fromEmail cannot be empty or invalid
  81. if(array_key_exists("fromEmail", $_POST) && PHPMailer::validateAddress($_POST["fromEmail"])){
  82. $fromEmail = strip_tags($_POST["fromEmail"]);
  83. } else {
  84. $err = true;
  85. echo "<h3>You must provide a valid email address for yourself.</h3>";
  86. }
  87. if(array_key_exists("toEmail", $_POST) && PHPMailer::validateAddress($_POST["toEmail"])){
  88. $toEmail = strip_tags($_POST["toEmail"]);
  89. } else {
  90. $err = true;
  91. echo "<h3>You must provide a valid email address for your friend or colleague.</h3>";
  92. }
  93. if(!$err){
  94. // Insert emails into db (new table for referral addresses)
  95. $query = "INSERT INTO invitees (`toName`, `toEmail`, `fromName`, `fromEmail`, `date`) VALUES ('".mysqli_real_escape_string($conn, $toName)."', '".mysqli_real_escape_string($conn, $toEmail)."', '".mysqli_real_escape_string($conn, $fromName)."', '".mysqli_real_escape_string($conn, $fromEmail)."', CURDATE())";
  96. $result = mysqli_query($conn, $query);
  97. //and send the email using PHPMailer
  98. $mail = new PHPMailer();
  99.  
  100. //Server settings
  101. $mail->SMTPDebug = 0;
  102. $mail->isSMTP();
  103. $mail->SMTPAuth = true;
  104. $mail->SMTPSecure = 'tls';
  105. $mail->Host = 'smtp.stackmail.com';
  106. $mail->Username = 'invite@heal-the-nation.com';
  107. $mail->Password = 'PMQP1RCngk';
  108. $mail->Port = 587;
  109.  
  110. //Recipients
  111. $mail->setFrom('invite@heal-the-nation.com', 'Heal the Nation');
  112. $mail->addAddress($toEmail); // Add a recipient
  113.  
  114. $mail->addReplyTo($fromEmail, $fromName);
  115. // $mail->addCC('cc@example.com');
  116.  
  117. //Add an image in the email body
  118. $mail->AddEmbeddedImage('./img/HTN_Logo.png', 'HTN_Logo');
  119.  
  120. $msg = ''; //Many, many lines of an html email
  121.  
  122. //Content
  123. $mail->isHTML(true);
  124. $mail->Subject = 'An invitation to learn about Heal the Nation: The Healthcare Workers' Network';
  125. $mail->Body = $msg;
  126. $mail->AltBody = strip_tags($msg);
  127.  
  128. if(!$mail->send()) {
  129. echo "<h3 class='bg-danger text-info'>Unable to send invitation. Please try again.</h3><br>";
  130. echo "Mailer Error: " . $mail->ErrorInfo;
  131. //exit();
  132. } else {
  133. echo "<h3 class='bg-success text-info'>Invitation sent!</h3>";
  134. //exit();
  135. }
  136. }
  137. };
  138.  
  139. ?>
  140.  
  141. $(document).ready(function(){
  142. var formRefer = $("#formRefer")
  143. var fromName = $("#fromName").val();
  144. var fromEmail = $("#fromEmail").val();
  145. var toName = $("#toName").val();
  146. var toEmail = $("#toEmail").val();
  147.  
  148. formRefer.submit(function(event){
  149.  
  150. event.preventDefault();
  151.  
  152. showSpinner();
  153. disableButton();
  154.  
  155. var fromName = $("#fromName").val();
  156. var fromEmail = $("#fromEmail").val();
  157. var toName = $("#toName").val();
  158. var toEmail = $("#toEmail").val();
  159.  
  160. var formData = $(formRefer).serialize();
  161. var url = $(formRefer).attr('action');
  162.  
  163. $.ajax({
  164. url:url,
  165. data:formData,
  166. type:'POST',
  167. dataType:"text",
  168. }).done(function(response){
  169. console.log(response);
  170. console.log(response.responseText);
  171.  
  172. if($.trim(response) == "<h3 class='bg-danger text-info'>Unable to send invitation. Please try again.</h3><br>"){
  173. console.log("The response says unable to send invite....");
  174. $("#message3").html(response);
  175. } else if($.trim(response) == "<h3 class='bg-success text-info'>Invitation sent!</h3>") {
  176. console.log("Success!!!");
  177. $("#message3").html(response);
  178. //Clear the form...
  179.  
  180. }).fail(function(data){
  181. console.log(data);
  182. console.log(data.responseText);
  183.  
  184. if (data.responseText !== '') {
  185. $("#message3").text(data.responseText);
  186. } else {
  187. $("#message3").text('Oops! An error occurred.');
  188. }
  189. }); //end ajax request
  190. }); //end formRefer.submit() function
  191. }); //end document.ready function
Add Comment
Please, Sign In to add comment