Advertisement
Guest User

Untitled

a guest
Jul 2nd, 2018
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 9.63 KB | None | 0 0
  1. <?php
  2. ini_set('display_errors', 1);
  3. ini_set('display_startup_errors', 1);
  4. error_reporting(E_ALL);
  5. /**
  6.  * This example shows settings to use when sending via Google's Gmail servers.
  7.  * This uses traditional id & password authentication - look at the gmail_xoauth.phps
  8.  * example to see how to use XOAUTH2.
  9.  * The IMAP section shows how to save this message to the 'Sent Mail' folder using IMAP commands.
  10.  */
  11.  
  12. //Import PHPMailer classes into the global namespace
  13. use PHPMailer\PHPMailer\PHPMailer;
  14.  
  15. require '../../vendor/autoload.php';
  16.  
  17. // echo $_FILES["fileToUpload"]['name'][1];
  18. //                         die();
  19.  
  20. $employeeName = "";
  21. $employeeEmail = "";
  22. $employeeRecommendation = "";
  23. $friendsName = "";
  24. $friendsEmail = "";
  25. $friendsPhone = "";
  26. //get properties from form
  27. if (isset($_POST['name'])) {
  28.     $employeeName = $_POST['name'];
  29. }
  30. if (isset($_POST['email'])) {
  31.     $employeeEmail = $_POST['email'];
  32. }
  33. if (isset($_POST['recommendation'])) {
  34.     $employeeRecommendation = $_POST['recommendation'];
  35. }
  36. if (isset($_POST['friends_name'])) {
  37.     $friendsName = $_POST['friends_name'];
  38. }
  39. if (isset($_POST['friends_email'])) {
  40.     $friendsEmail = $_POST['friends_email'];
  41. }
  42. if (isset($_POST['friends_phone'])) {
  43.     $friendsPhone = $_POST['friends_phone'];
  44. }
  45. if (isset($_POST['language'])) {
  46.     $language = $_POST['language'];
  47. }
  48.  
  49. if ($employeeRecommendation == "") {
  50.     echo "recommendation badly formed";
  51.     die();
  52.     if (preg_match('/[^A-Za-ząčęėįšųūžĄČĘĖĮŠŲŪŽ]/', $employeeName)) {
  53.         echo "recommendation badly formed";
  54.         header('Location: formController.php');
  55.         die();
  56.     }
  57. }
  58.  
  59. if (preg_match("/^[a-zA-ZàáâäãåąčćęèéêëėįìíîïłńòóôöõøùúûüųūÿýżźñçčšžÀÁÂÄÃÅĄĆČĖĘÈÉÊËÌÍÎÏĮŁŃÒÓÔÖÕØÙÚÛÜŲŪŸÝŻŹÑßÇŒÆČŠŽ∂ð ,.'-]+$/u", $employeeName)) { //check if name is text only
  60.     if (filter_var($employeeEmail, FILTER_VALIDATE_EMAIL)) { //check if email is valid
  61.         if (preg_match("/^[a-zA-ZàáâäãåąčćęèéêëėįìíîïłńòóôöõøùúûüųūÿýżźñçčšžÀÁÂÄÃÅĄĆČĖĘÈÉÊËÌÍÎÏĮŁŃÒÓÔÖÕØÙÚÛÜŲŪŸÝŻŹÑßÇŒÆČŠŽ∂ð ,.'-]+$/u", $employeeName)) { //check if name is text only
  62.             if (filter_var($employeeEmail, FILTER_VALIDATE_EMAIL)) { // if email is valid
  63.                 if (strpos($friendsPhone, '+') !== false) { // check if valid phone
  64.  
  65.                     $mail = new PHPMailer;
  66.  
  67.                     if ($_FILES["fileToUpload"]["name"][0] != "") { //check if files set and validate it
  68.                         //check if types valid
  69.                         ///////////////////////////////////////
  70.                         $valid_types = ["application/msword", "application/pdf"];
  71.                         foreach ($_FILES['fileToUpload']['type'] as $file) {
  72.                             if (!in_array($file, $valid_types)) {
  73.                                 echo "Sorry only doc pdf and odt file types accepted.";
  74.                                 header('Location: formController.php');
  75.                                 die();
  76.                             }
  77.                         }
  78.                         //check if not too big
  79.                         /////////////////////////////////////
  80.                         $filesSize = 0;
  81.                         foreach ($_FILES["fileToUpload"]["size"] as $file) {
  82.                             $filesSize = $filesSize + $file;
  83.                         }
  84.                         if ($filesSize > 500000) {
  85.                             echo "Sorry, your file is too large.";
  86.                             header('Location: formController.php');
  87.                             die();
  88.                         }
  89.                         //////////////////////////////////////
  90.                         //Attach cv file(s)
  91.                         for ($i = 0; $i < count($_FILES["fileToUpload"]["name"]); $i++) {
  92.                             $mail->AddAttachment($_FILES['fileToUpload']['tmp_name'][$i],
  93.                                 $_FILES['fileToUpload']['name'][$i]);
  94.                         }
  95.                     }
  96.  
  97.                     $emailText = array( //more comfortable to pass to file if decided in the future
  98.                         'employee_name' => $employeeName,
  99.                         'employee_email' => $employeeEmail,
  100.                         'recommendation' => $employeeRecommendation,
  101.                         'friends_name' => $friendsName,
  102.                         'friends_email' => $friendsEmail,
  103.                         'friends_phone' => $friendsPhone,
  104.                     );
  105.  
  106.                     //Tell PHPMailer to use SMTP
  107.                     $mail->isSMTP();
  108.                     $mail->isHTML(); //set for sending from html
  109.  
  110.                     //Enable SMTP debugging
  111.                     // 0 = off (for production use)
  112.                     // 1 = client messages
  113.                     // 2 = client and server messages
  114.                     $mail->SMTPDebug = 0;
  115.  
  116.                     //Set the hostname of the mail server
  117.                     $mail->Host = 'smtp.gmail.com';
  118.                     // use
  119.                     // $mail->Host = gethostbyname('smtp.gmail.com');
  120.                     // if your network does not support SMTP over IPv6
  121.  
  122.                     //Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
  123.                     $mail->Port = 587;
  124.  
  125.                     //Set the encryption system to use - ssl (deprecated) or tls
  126.                     $mail->SMTPSecure = 'tls';
  127.  
  128.                     //Whether to use SMTP authentication
  129.                     $mail->SMTPAuth = true;
  130.  
  131.                     //Username to use for SMTP authentication - use full email address for gmail
  132.                     $mail->Username = "formsteltonika@gmail.com";
  133.  
  134.                     //Password to use for SMTP authentication
  135.                     $mail->Password = "2018KaunasTeltonika?";
  136.  
  137.                     //Set who the message is to be sent from
  138.                     $mail->setFrom('formsteltonika@gmail.com', 'Darbuotojo rekomendacija');
  139.  
  140.                     //Set an alternative reply-to address
  141.                     $mail->addReplyTo('formsteltonika@gmail.com', 'Darbuotojo rekomendacija');
  142.  
  143.                     //Set who the message is to be sent to
  144.                     $mail->addAddress('kestas3210@gmail.com', 'Čia HR vardas'); //set to hr email
  145.  
  146.                     $mail->Subject = $employeeName . ' ' . 'jums rekomenduoja darbuotoją'; // email subject
  147.                     //html formated email content
  148.                     $mail->Body = formEmail($language, $employeeName, $employeeEmail, $friendsName, $friendsEmail, $friendsPhone, $employeeRecommendation);
  149.  
  150.                     $mail->CharSet = 'UTF-8';
  151.                     if ($mail->send()) {
  152.                         $logMessege = date("Y-m-d H:i:s") . "|" . $emailText["employee_name"] . "|" . $emailText["employee_email"] . "|" . $emailText["recommendation"] . "|" . $emailText["friends_name"] . "|" . $emailText["friends_email"] . "|" . $emailText["friends_phone"];
  153.                         logToFile($logMessege);
  154.                         header('Location: /recommend-form/app/views/thankYou.html');
  155.                         die();
  156.                     } else {
  157.                         // mailer errors
  158.                         echo "Mailer Error: " . $mail->ErrorInfo; //log emailer errors if something wrong
  159.  
  160.                     }
  161.                 } else {
  162.                     echo 'Blogai įvestas draugo numeris';
  163.                     header('Location: formController.php');
  164.                     die();
  165.                 }
  166.             } else {
  167.                 echo 'Blogai įvestas draugo email';
  168.                 header('Location: formController.php');
  169.                 die();
  170.             }
  171.         } else {
  172.             'Blogai įvestas draugo vardas';
  173.             header('Location: formController.php');
  174.             die();
  175.         }
  176.     } else {
  177.         echo 'Blogai įvestas tavo email';
  178.         header('Location: formController.php');
  179.         die();
  180.     }
  181. } else {
  182.     echo 'Blogai įvesti duomenys';
  183.     header('Location: formController.php');
  184.     die();
  185. }
  186.  
  187. /**
  188.  * Logs actions to file
  189.  */
  190. function logToFile($messege)
  191. {
  192.     $f = fopen("../../log.txt", "a+");
  193.     fwrite($f, $messege . "\n");
  194.     fclose($f);
  195. }
  196. /**
  197.  * Formats email
  198.  */
  199. function formEmail($language, $employeeName, $employeeEmail, $friendsName, $friendsEmail, $friendsPhone, $employeeRecommendation)
  200. {
  201.     $filename = "../views/emailContent1.html";
  202.     $handle = fopen($filename, "r");
  203.     $contents = fread($handle, filesize($filename));
  204.     fclose($handle);
  205.  
  206.     if($language == 'lt'){
  207.         $recommendation = "Sveiki, " . $employeeName . " (" . $employeeEmail . ")" . " jums rekomenduoja naują darbuotoją:" .
  208.         $friendsName . " " . $friendsEmail . " " . $friendsPhone . "." . " " . $employeeName . " mano, kad" . $friendsName . "yra tinkamas, nes:" .
  209.         $employeeRecommendation;
  210.     }
  211.     if($language == "en"){
  212.         $recommendation = "Hello, " . $employeeName . " (" . $employeeEmail . ")" . " has recommend a new employee for you:" .
  213.         $friendsName . " " . $friendsEmail . " " . $friendsPhone . "." . " " . $employeeName . " thinks, that" . $friendsName . "is suitable for the possition, because:" .
  214.         $employeeRecommendation;
  215.     }    
  216.  
  217.     $filename = "../views/emailContent2.html";
  218.     $handle = fopen($filename, "r");
  219.     $contents1 = fread($handle, filesize($filename));
  220.     fclose($handle);
  221.  
  222.     $recommendationEmail = $contents . $recommendation . $contents1;
  223.  
  224.     return $recommendationEmail;
  225. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement