Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- header('Content-Type: multipart/form-data');
- header('Access-Control-Allow-Origin: *');
- header('Access-Control-Allow-Methods: DELETE, HEAD, GET, OPTIONS, POST, PUT');
- header('Access-Control-Allow-Headers: Content-Type, Content-Range, Content-Disposition, Content-Description');
- const SMTP_USERNAME = "phpemailresponser@gmail.com";
- const SMTP_PASSWORD = "mQWewpe8";
- const SMTP_FROM_EMAIL = "phpemailresponser@gmail.com";
- const SMTP_FROM_NAME = "hanacakes.sk";
- const SMTP_TARGET_EMAIL = "info@hanacakes.sk";
- const SMTP_CC_EMAIL = "infohanacakes@gmail.com";
- const SMTP_HOST = "smtp.gmail.com";
- const SMTP_SUBJECT = "Objednávka [Torta] - Hana Cakes";
- //enable preflight request
- if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
- http_response_code(200);
- exit();
- }
- require './phpmailer/PHPMailerAutoload.php';
- //test request is valid, if not return 400 - bad request
- if (!isRequestValid()) {
- http_response_code(400);
- echo "Invalid request";
- exit();
- }
- $name = $_POST['name'];
- $email = $_POST['email'];
- $phone = $_POST['phone'];
- $date = $_POST['date'];
- $floors = $_POST['floors'];
- $average = $_POST['average'];
- $taste = $_POST['taste'];
- $korpus = $_POST['korpus'];
- $cream = $_POST['cream'];
- $gel = $_POST['gel'];
- $message = $_POST['message'];
- if (!isDataValid($name, $email, $phone, $date, $floors, $average, $taste, $korpus, $cream, $gel, $message)) {
- http_response_code(400);
- echo "Invalid data";
- exit();
- }
- sendMail($name, $email, $phone, date('d.m.Y', strtotime($date)), $floors, $average, $taste, $korpus, $cream, $gel, $message);
- /*************************************************************************/
- /**
- * Test request method is POST and content type is application/json
- */
- function isRequestValid() {
- if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
- return false;
- }
- return true;
- }
- function isDataValid($name, $email, $phone, $date, $floors, $average, $taste, $korpus, $cream, $gel, $message) {
- if (empty($name)) {
- return false;
- }
- if (empty($email) || !filter_var($email, FILTER_VALIDATE_EMAIL)) {
- return false;
- }
- if (empty($phone)) {
- return false;
- }
- if (empty($date)) {
- return false;
- }
- if (empty($floors)) {
- return false;
- }
- if (empty($average)) {
- return false;
- }
- if (empty($taste)) {
- return false;
- }
- if (empty($korpus)) {
- return false;
- }
- if (empty($cream)) {
- return false;
- }
- if (empty($gel)) {
- return false;
- }
- if (empty($message)) {
- return false;
- }
- return true;
- }
- function sendMail($name, $email, $phone, $date, $floors, $average, $taste, $korpus, $cream, $gel, $message) {
- $mail = new PHPMailer;
- $mail->SMTPDebug = 0; // Enable verbose debug output
- $mail->isSMTP(); // Set mailer to use SMTP
- $mail->CharSet="UTF-8";
- $mail->Host = SMTP_HOST; // Specify main and backup SMTP servers
- $mail->SMTPAuth = true; // Enable SMTP authentication
- $mail->Username = SMTP_USERNAME; // SMTP username
- $mail->Password = SMTP_PASSWORD; // SMTP password
- $mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
- $mail->Port = 587; // TCP port to connect to
- $mail->setFrom(SMTP_FROM_EMAIL, SMTP_FROM_NAME);
- $mail->addAddress(SMTP_TARGET_EMAIL); // Add a recipient
- $mail->addCC(SMTP_CC_EMAIL); // Add a copy recipient
- $mail->addBcc(SMTP_BCC_EMAIL); // Add secret recipient
- $mail->addReplyTo($email, $name);
- $mail->isHTML(true); // Set email format to HTML
- $mail->Subject = SMTP_SUBJECT;
- $mail->Body = "<b>Správa od:</b><br>$name<br>$phone<br>$email<br><b>Dátum vybavenie objednávky:</b><br>$date<br><br><b>Počet poschodí torty:</b><br>$floors<br><b>Priemer torty:</b><br>$average<br><b>Torta:</b><br>$taste<br><b>Korpus:</b><br>$korpus<br><b>Krém:</b><br>$cream<br><b>Gél:</b><br>$gel<br><br><b>Obsah správy:</b><br>$message";
- if (isset($_FILES['file'])) {
- $mail->AddAttachment($_FILES['file']['tmp_name'], $_FILES['file']['name']);
- }
- if(!$mail->send()) {
- http_response_code(500);
- echo "Encountered error while sending email.";
- exit();
- } else {
- http_response_code(200);
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement