Advertisement
Guest User

Untitled

a guest
Jun 28th, 2018
457
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 10.03 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. /**
  7.  * This example shows settings to use when sending via Google's Gmail servers.
  8.  * This uses traditional id & password authentication - look at the gmail_xoauth.phps
  9.  * example to see how to use XOAUTH2.
  10.  * The IMAP section shows how to save this message to the 'Sent Mail' folder using IMAP commands.
  11.  */
  12.  
  13. //Import PHPMailer classes into the global namespace
  14. use PHPMailer\PHPMailer\PHPMailer;
  15.  
  16. require '../../vendor/autoload.php';
  17.  
  18. $employeeName = "";
  19. $employeeEmail = "";
  20. $employeeRecommendation = "";
  21. $friendsName = "";
  22. $friendsEmail = "";
  23. $friendsPhone = "";
  24. //get properties from form
  25. if (isset($_POST['name'])) {
  26.     $employeeName = $_POST['name'];
  27. }
  28. if (isset($_POST['email'])) {
  29.     $employeeEmail = $_POST['email'];
  30. }
  31. if (isset($_POST['recommendation'])) {
  32.     $employeeRecommendation = $_POST['recommendation'];
  33. }
  34. if (isset($_POST['friends_name'])) {
  35.     $friendsName = $_POST['friends_name'];
  36. }
  37. if (isset($_POST['friends_email'])) {
  38.     $friendsEmail = $_POST['friends_email'];
  39. }
  40. if (isset($_POST['friends_phone'])) {
  41.     $friendsPhone = $_POST['friends_phone'];
  42. }
  43.  
  44. if ($employeeRecommendation == "") {
  45.     echo "recommendation badly formed";
  46.     die();
  47.     if (preg_match('/[^A-Za-ząčęėįšųūžĄČĘĖĮŠŲŪŽ]/', $employeeName)) {
  48.         echo "recommendation badly formed";
  49.         header('Location: formController.php');
  50.         die();
  51.     }
  52. }
  53.  
  54. if (preg_match("/^[a-zA-ZàáâäãåąčćęèéêëėįìíîïłńòóôöõøùúûüųūÿýżźñçčšžÀÁÂÄÃÅĄĆČĖĘÈÉÊËÌÍÎÏĮŁŃÒÓÔÖÕØÙÚÛÜŲŪŸÝŻŹÑßÇŒÆČŠŽ∂ð ,.'-]+$/u", $employeeName)) { //check if name is text only
  55.     if (filter_var($employeeEmail, FILTER_VALIDATE_EMAIL)) { //check if email is valid
  56.         if (preg_match("/^[a-zA-ZàáâäãåąčćęèéêëėįìíîïłńòóôöõøùúûüųūÿýżźñçčšžÀÁÂÄÃÅĄĆČĖĘÈÉÊËÌÍÎÏĮŁŃÒÓÔÖÕØÙÚÛÜŲŪŸÝŻŹÑßÇŒÆČŠŽ∂ð ,.'-]+$/u", $employeeName)) { //check if name is text only
  57.             if (filter_var($employeeEmail, FILTER_VALIDATE_EMAIL)) { // if email is valid
  58.                 if (strpos($friendsPhone, '+') !== false) { // check if valid phone
  59.                     if ($_FILES["fileToUpload"]["name"] != "") { //check if file is set
  60.                         $target_dir = "../../uploads/";
  61.                         $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
  62.                         $fileType = strtolower(pathinfo($target_file, PATHINFO_EXTENSION));
  63.                         if ($fileType != "doc" && $fileType != "pdf" && $fileType != "odt" && $fileType != "docx") {
  64.                             echo "Sorry only doc pdf and odt file types accepted.";
  65.                             header('Location: formController.php');
  66.                             die();
  67.                         }
  68.                         if ($_FILES["fileToUpload"]["size"] > 500000) {
  69.                             echo "Sorry, your file is too large.";
  70.                             header('Location: formController.php');
  71.                             die();
  72.                         }
  73.                     }
  74.                     //dar reik patikrint ar zjbs failas
  75.                     $emailText = array( //čia kad tekstą patogiau formuot jei ką persiduot kitam failui
  76.                         'employee_name' => $employeeName,
  77.                         'employee_email' => $employeeEmail,
  78.                         'recommendation' => $employeeRecommendation,
  79.                         'friends_name' => $friendsName,
  80.                         'friends_email' => $friendsEmail,
  81.                         'friends_phone' => $friendsPhone,
  82.                     );
  83.                     // if('viskas zjbs') siųsti email
  84.                     $mail = new PHPMailer;
  85.  
  86.                     //Tell PHPMailer to use SMTP
  87.                     $mail->isSMTP();
  88.                     $mail->isHTML();
  89.  
  90.                     //Enable SMTP debugging
  91.                     // 0 = off (for production use)
  92.                     // 1 = client messages
  93.                     // 2 = client and server messages
  94.                     $mail->SMTPDebug = 0;
  95.  
  96.                     //Set the hostname of the mail server
  97.                     $mail->Host = 'smtp.gmail.com';
  98.                     // use
  99.                     // $mail->Host = gethostbyname('smtp.gmail.com');
  100.                     // if your network does not support SMTP over IPv6
  101.  
  102.                     //Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
  103.                     $mail->Port = 587;
  104.  
  105.                     //Set the encryption system to use - ssl (deprecated) or tls
  106.                     $mail->SMTPSecure = 'tls';
  107.  
  108.                     //Whether to use SMTP authentication
  109.                     $mail->SMTPAuth = true;
  110.  
  111.                     //Username to use for SMTP authentication - use full email address for gmail
  112.                     $mail->Username = "formsteltonika@gmail.com";
  113.  
  114.                     //Password to use for SMTP authentication
  115.                     $mail->Password = "2018KaunasTeltonika?";
  116.  
  117.                     //Set who the message is to be sent from
  118.                     $mail->setFrom('formsteltonika@gmail.com', 'Darbuotojo rekomendacija');
  119.  
  120.                     //Set an alternative reply-to address
  121.                     $mail->addReplyTo('formsteltonika@gmail.com', 'Darbuotojo rekomendacija');
  122.  
  123.                     //Set who the message is to be sent to
  124.                     $mail->addAddress('kestas3210@gmail.com', 'Čia hr vardas');
  125.  
  126.                     $mail->Subject = $employeeName . ' ' . 'jums rekomenduoja darbuotoją';
  127.  
  128.                     //Replace the plain text body with one created manually
  129.                     //$varasdasd = include("/var/www/html/recommend-form/app/views/emailContent.html");
  130.                     $mail->Body = formEmail($employeeName, $employeeEmail, $friendsName, $friendsEmail, $friendsPhone, $employeeRecommendation);
  131.  
  132.                     //Attach cv image file
  133.                     if ($_FILES["fileToUpload"]["name"] != "") {
  134.                         $mail->AddAttachment($_FILES['fileToUpload']['tmp_name'],
  135.                             $_FILES['fileToUpload']['name']);
  136.                     }
  137.  
  138.                     $mail->CharSet = 'UTF-8';
  139.                     if ($mail->send()) {
  140.                         $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"];
  141.                         logToFile($logMessege);
  142.                         ?>
  143.                         <script>window.location = "formController.php"</script>
  144.                         // header('Location: formController.php');<?php
  145.                         die();
  146.                         //Section 2: IMAP
  147.                         //Uncomment these to save your message in the 'Sent Mail' folder.
  148.                         #if (save_mail($mail)) {
  149.                        #    echo "Message saved!";
  150.                        #}
  151.                    } else {
  152.                         // mailer errors
  153.                         echo "Mailer Error: " . $mail->ErrorInfo;
  154.                        
  155.                        
  156.                     }
  157.                 } else {
  158.                     echo 'Blogai įvestas draugo numeris';
  159.                     header('Location: formController.php');
  160.                     die();
  161.                 }
  162.             } else {
  163.                 echo 'Blogai įvestas draugo email';
  164.                 header('Location: formController.php');
  165.                 die();
  166.             }
  167.         } else {
  168.             'Blogai įvestas draugo vardas';
  169.             header('Location: formController.php');
  170.             die();
  171.         }
  172.     } else {
  173.         echo 'Blogai įvestas tavo email';
  174.         header('Location: formController.php');
  175.         die();
  176.     }
  177. } else {
  178.     echo 'Blogai įvesti duomenys';
  179.     header('Location: formController.php');
  180.     die();
  181. }
  182.  
  183. //Create a new PHPMailer instance
  184.  
  185. //Section 2: IMAP
  186. //IMAP commands requires the PHP IMAP Extension, found at: https://php.net/manual/en/imap.setup.php
  187. //Function to call which uses the PHP imap_*() functions to save messages: https://php.net/manual/en/book.imap.php
  188. //You can use imap_getmailboxes($imapStream, '/imap/ssl') to get a list of available folders or labels, this can
  189. //be useful if you are trying to get this working on a non-Gmail IMAP server.
  190. function save_mail($mail)
  191. {
  192.     //You can change 'Sent Mail' to any other folder or tag
  193.     $path = "{imap.gmail.com:993/imap/ssl}[Gmail]/Sent Mail";
  194.  
  195.     //Tell your server to open an IMAP connection using the same username and password as you used for SMTP
  196.     $imapStream = imap_open($path, $mail->Username, $mail->Password);
  197.  
  198.     $result = imap_append($imapStream, $path, $mail->getSentMIMEMessage());
  199.     imap_close($imapStream);
  200.  
  201.     return $result;
  202. }
  203.  
  204. function logToFile($messege)
  205. {
  206.     $f = fopen("../../log.txt", "a+");
  207.     fwrite($f, $messege . "\n");
  208.     fclose($f);
  209. }
  210.  
  211. function formEmail($employeeName, $employeeEmail, $friendsName, $friendsEmail, $friendsPhone, $employeeRecommendation)
  212. {
  213.     $filename = "../views/emailContent1.html";
  214.     $handle = fopen($filename, "r");
  215.     $contents = fread($handle, filesize($filename));
  216.     fclose($handle);
  217.  
  218.     $recommendation = "Sveiki, " . $employeeName . " (" . $employeeEmail . ")" . " jums rekomenduoja naują darbuotoją:" .
  219.         $friendsName . " " . $friendsEmail . " " . $friendsPhone . "." . " " . $employeeName . " mano, kad" . $friendsName . "yra tinkamas, nes:" .
  220.         $employeeRecommendation;
  221.  
  222.     $filename = "../views/emailContent2.html";
  223.     $handle = fopen($filename, "r");
  224.     $contents1 = fread($handle, filesize($filename));
  225.     fclose($handle);
  226.  
  227.     $recommendationEmail = $contents . $recommendation . $contents1;
  228.  
  229.     return $recommendationEmail;
  230. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement