Advertisement
Mary_Pieroszkiewicz

skrypt php

May 21st, 2020
2,063
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.86 KB | None | 0 0
  1. <?php
  2.  
  3. $mailToSend = "mary.pieroszkiewicz@gmail.com";
  4.  
  5. if ($_SERVER["REQUEST_METHOD"] === "POST") {
  6.     $name = $_POST["name"];
  7.     $surname = $_POST["surname"];
  8.     $email = $_POST["email"];
  9.     $message = $_POST["email"];
  10.  
  11.     $errors = [];
  12.     $return = [];
  13.  
  14.     if (empty($name)) { //jeżeli pusta wartość
  15.         array_push($errors, "name");
  16.     }
  17.     if (empty($surname)) { //jeżeli pusta wartość
  18.         array_push($errors, "surname");
  19.     }
  20.     if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { //sprawdzamy czy email ma zły wzór
  21.         array_push($errors, "email");
  22.     }
  23.     if (empty($message)) {
  24.         array_push($errors, "message");
  25.     }
  26.     if (count($errors) > 0) {
  27.         $return["errors"] = $errors;
  28.     } else {
  29.         //każde wysłanie wiadomości musi być poprzedzone ustawieniem nagłówków
  30.         $headers  = "MIME-Version: 1.0" . "\r\n";
  31.         $headers .= "Content-type: text/html; charset=UTF-8". "\r\n";
  32.         $headers .= "From: ".$email."\r\n";
  33.         $headers .= "Reply-to: ".$email;
  34.         $message  = '
  35.            <html>
  36.                <head>
  37.                    <meta charset="utf-8">
  38.                </head>
  39.                <body>
  40.                    <div> Imię: $name </div>
  41.                    <div> Nazwisko: $surname </div>
  42.                    <div> Email: <a href="mailto:'.$email.'">$email</a> </div>
  43.                    <div> Telefon kontaktowy: $phone</div>
  44.                    <div> Wiadomość: </div>
  45.                    <div> $message </div>
  46.                </body>
  47.            </html>';
  48.  
  49.         if (mail($mailToSend, "Wiadomość ze strony - " . date("d-m-Y"), $message, $headers)) {
  50.             $return["status"] = "ok";
  51.         } else {
  52.             $return["status"] = "error";
  53.         }
  54.     }
  55.  
  56.     header("Content-Type: application/json");
  57.     echo json_encode($return);
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement