Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- ini_set('display_errors', 1);
- ini_set('display_startup_errors', 1);
- error_reporting(E_ALL);
- /**
- * This example shows settings to use when sending via Google's Gmail servers.
- * This uses traditional id & password authentication - look at the gmail_xoauth.phps
- * example to see how to use XOAUTH2.
- * The IMAP section shows how to save this message to the 'Sent Mail' folder using IMAP commands.
- */
- //Import PHPMailer classes into the global namespace
- use PHPMailer\PHPMailer\PHPMailer;
- require '../../vendor/autoload.php';
- // echo $_FILES["fileToUpload"]['name'][1];
- // die();
- $employeeName = "";
- $employeeEmail = "";
- $employeeRecommendation = "";
- $friendsName = "";
- $friendsEmail = "";
- $friendsPhone = "";
- //get properties from form
- if (isset($_POST['name'])) {
- $employeeName = $_POST['name'];
- }
- if (isset($_POST['email'])) {
- $employeeEmail = $_POST['email'];
- }
- if (isset($_POST['recommendation'])) {
- $employeeRecommendation = $_POST['recommendation'];
- }
- if (isset($_POST['friends_name'])) {
- $friendsName = $_POST['friends_name'];
- }
- if (isset($_POST['friends_email'])) {
- $friendsEmail = $_POST['friends_email'];
- }
- if (isset($_POST['friends_phone'])) {
- $friendsPhone = $_POST['friends_phone'];
- }
- if (isset($_POST['language'])) {
- $language = $_POST['language'];
- }
- if ($employeeRecommendation == "") {
- echo "recommendation badly formed";
- die();
- if (preg_match('/[^A-Za-ząčęėįšųūžĄČĘĖĮŠŲŪŽ]/', $employeeName)) {
- echo "recommendation badly formed";
- header('Location: formController.php');
- die();
- }
- }
- if (preg_match("/^[a-zA-ZàáâäãåąčćęèéêëėįìíîïłńòóôöõøùúûüųūÿýżźñçčšžÀÁÂÄÃÅĄĆČĖĘÈÉÊËÌÍÎÏĮŁŃÒÓÔÖÕØÙÚÛÜŲŪŸÝŻŹÑßÇŒÆČŠŽ∂ð ,.'-]+$/u", $employeeName)) { //check if name is text only
- if (filter_var($employeeEmail, FILTER_VALIDATE_EMAIL)) { //check if email is valid
- if (preg_match("/^[a-zA-ZàáâäãåąčćęèéêëėįìíîïłńòóôöõøùúûüųūÿýżźñçčšžÀÁÂÄÃÅĄĆČĖĘÈÉÊËÌÍÎÏĮŁŃÒÓÔÖÕØÙÚÛÜŲŪŸÝŻŹÑßÇŒÆČŠŽ∂ð ,.'-]+$/u", $employeeName)) { //check if name is text only
- if (filter_var($employeeEmail, FILTER_VALIDATE_EMAIL)) { // if email is valid
- if (strpos($friendsPhone, '+') !== false) { // check if valid phone
- $mail = new PHPMailer;
- if ($_FILES["fileToUpload"]["name"][0] != "") { //check if files set and validate it
- //check if types valid
- ///////////////////////////////////////
- $valid_types = ["application/msword", "application/pdf"];
- foreach ($_FILES['fileToUpload']['type'] as $file) {
- if (!in_array($file, $valid_types)) {
- echo "Sorry only doc pdf and odt file types accepted.";
- header('Location: formController.php');
- die();
- }
- }
- //check if not too big
- /////////////////////////////////////
- $filesSize = 0;
- foreach ($_FILES["fileToUpload"]["size"] as $file) {
- $filesSize = $filesSize + $file;
- }
- if ($filesSize > 500000) {
- echo "Sorry, your file is too large.";
- header('Location: formController.php');
- die();
- }
- //////////////////////////////////////
- //Attach cv file(s)
- for ($i = 0; $i < count($_FILES["fileToUpload"]["name"]); $i++) {
- $mail->AddAttachment($_FILES['fileToUpload']['tmp_name'][$i],
- $_FILES['fileToUpload']['name'][$i]);
- }
- }
- $emailText = array( //more comfortable to pass to file if decided in the future
- 'employee_name' => $employeeName,
- 'employee_email' => $employeeEmail,
- 'recommendation' => $employeeRecommendation,
- 'friends_name' => $friendsName,
- 'friends_email' => $friendsEmail,
- 'friends_phone' => $friendsPhone,
- );
- //Tell PHPMailer to use SMTP
- $mail->isSMTP();
- $mail->isHTML(); //set for sending from html
- //Enable SMTP debugging
- // 0 = off (for production use)
- // 1 = client messages
- // 2 = client and server messages
- $mail->SMTPDebug = 0;
- //Set the hostname of the mail server
- $mail->Host = 'smtp.gmail.com';
- // use
- // $mail->Host = gethostbyname('smtp.gmail.com');
- // if your network does not support SMTP over IPv6
- //Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
- $mail->Port = 587;
- //Set the encryption system to use - ssl (deprecated) or tls
- $mail->SMTPSecure = 'tls';
- //Whether to use SMTP authentication
- $mail->SMTPAuth = true;
- //Username to use for SMTP authentication - use full email address for gmail
- $mail->Username = "formsteltonika@gmail.com";
- //Password to use for SMTP authentication
- $mail->Password = "2018KaunasTeltonika?";
- //Set who the message is to be sent from
- $mail->setFrom('formsteltonika@gmail.com', 'Darbuotojo rekomendacija');
- //Set an alternative reply-to address
- $mail->addReplyTo('formsteltonika@gmail.com', 'Darbuotojo rekomendacija');
- //Set who the message is to be sent to
- $mail->addAddress('kestas3210@gmail.com', 'Čia HR vardas'); //set to hr email
- $mail->Subject = $employeeName . ' ' . 'jums rekomenduoja darbuotoją'; // email subject
- //html formated email content
- $mail->Body = formEmail($language, $employeeName, $employeeEmail, $friendsName, $friendsEmail, $friendsPhone, $employeeRecommendation);
- $mail->CharSet = 'UTF-8';
- if ($mail->send()) {
- $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"];
- logToFile($logMessege);
- header('Location: /recommend-form/app/views/thankYou.html');
- die();
- } else {
- // mailer errors
- echo "Mailer Error: " . $mail->ErrorInfo; //log emailer errors if something wrong
- }
- } else {
- echo 'Blogai įvestas draugo numeris';
- header('Location: formController.php');
- die();
- }
- } else {
- echo 'Blogai įvestas draugo email';
- header('Location: formController.php');
- die();
- }
- } else {
- 'Blogai įvestas draugo vardas';
- header('Location: formController.php');
- die();
- }
- } else {
- echo 'Blogai įvestas tavo email';
- header('Location: formController.php');
- die();
- }
- } else {
- echo 'Blogai įvesti duomenys';
- header('Location: formController.php');
- die();
- }
- /**
- * Logs actions to file
- */
- function logToFile($messege)
- {
- $f = fopen("../../log.txt", "a+");
- fwrite($f, $messege . "\n");
- fclose($f);
- }
- /**
- * Formats email
- */
- function formEmail($language, $employeeName, $employeeEmail, $friendsName, $friendsEmail, $friendsPhone, $employeeRecommendation)
- {
- $filename = "../views/emailContent1.html";
- $handle = fopen($filename, "r");
- $contents = fread($handle, filesize($filename));
- fclose($handle);
- if($language == 'lt'){
- $recommendation = "Sveiki, " . $employeeName . " (" . $employeeEmail . ")" . " jums rekomenduoja naują darbuotoją:" .
- $friendsName . " " . $friendsEmail . " " . $friendsPhone . "." . " " . $employeeName . " mano, kad" . $friendsName . "yra tinkamas, nes:" .
- $employeeRecommendation;
- }
- if($language == "en"){
- $recommendation = "Hello, " . $employeeName . " (" . $employeeEmail . ")" . " has recommend a new employee for you:" .
- $friendsName . " " . $friendsEmail . " " . $friendsPhone . "." . " " . $employeeName . " thinks, that" . $friendsName . "is suitable for the possition, because:" .
- $employeeRecommendation;
- }
- $filename = "../views/emailContent2.html";
- $handle = fopen($filename, "r");
- $contents1 = fread($handle, filesize($filename));
- fclose($handle);
- $recommendationEmail = $contents . $recommendation . $contents1;
- return $recommendationEmail;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement