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 = "";
- const SMTP_PASSWORD = "";
- const SMTP_FROM_EMAIL = "";
- const SMTP_FROM_NAME = "";
- const SMTP_TARGET_EMAIL = "";
- const SMTP_CC_EMAIL = "";
- const SMTP_HOST = "";
- const SMTP_SUBJECT = "";
- // enable preflight request
- if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
- http_response_code(200);
- exit();
- }
- // require PHPMailer
- require './phpmailer/PHPMailerAutoload.php';
- //test request is valid, if not return 400 - bad request
- if (!isRequestValid()) {
- http_response_code(400);
- echo "Invalid request";
- exit();
- }
- // collect POST data from request
- $name = $_POST['name'];
- $email = $_POST['email'];
- $phone = $_POST['phone'];
- $message = $_POST['message'];
- if (!isDataValid($name, $email, $phone, $message)) {
- http_response_code(400);
- echo "Invalid data";
- exit();
- }
- // Send email function call
- sendMail($name, $email, $phone, $message);
- // Check if request method is POST
- function isRequestValid() {
- if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
- return false;
- }
- return true;
- }
- function isDataValid($name, $email, $phone, $message) {
- if (empty($name)) {
- return false;
- }
- if (empty($email) || !filter_var($email, FILTER_VALIDATE_EMAIL)) {
- return false;
- }
- if (empty($phone)) {
- return false;
- }
- if (empty($message)) {
- return false;
- }
- return true;
- }
- function sendMail($name, $email, $phone, $message) {
- $mail = new PHPMailer;
- $mail->SMTPDebug = 0;
- $mail->isSMTP();
- $mail->CharSet="UTF-8";
- $mail->Host = SMTP_HOST;
- $mail->SMTPAuth = true;
- $mail->Username = SMTP_USERNAME;
- $mail->Password = SMTP_PASSWORD;
- // TLS || SSL
- $mail->SMTPSecure = 'tls';
- // TCP PORT
- $mail->Port = 587;
- $mail->setFrom(SMTP_FROM_EMAIL, SMTP_FROM_NAME);
- $mail->addAddress(SMTP_TARGET_EMAIL);
- $mail->addCC(SMTP_CC_EMAIL);
- $mail->addBcc(SMTP_BCC_EMAIL);
- $mail->addReplyTo($email, $name);
- // Set email body content to HTML so you can embed HTML tags like <h1></h1>, <br>, etc.
- $mail->isHTML(true);
- $mail->Subject = SMTP_SUBJECT;
- $mail->Body = "MENO: $name, EMAIL: $email, PHONE: $phone, MESSAGE: $message";
- 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