Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- //require 'PHPMailer-master/PHPMailerAutoload.php';
- header('Content-Type: application/json');
- $errors = array();
- $title = (isset($_POST['title']) && ($_POST['title'] != '0')) ? $_POST['title'] : null;
- $firstName = (isset($_POST['firstName']) && !(empty($_POST["firstName"]))) ? $_POST['firstName'] : null;
- $lastName = (isset($_POST['lastName']) && !(empty($_POST["lastName"]))) ? $_POST['lastName'] : null;
- $emailAddress = (isset($_POST['emailAddress']) && !(empty($_POST["emailAddress"]))) ? $_POST['emailAddress'] : null;
- $website = isset($_POST['website']) ? $_POST['website'] : '';
- $message = (isset($_POST['message']) && !(empty($_POST["message"]))) ? $_POST['message'] : null;
- if ($title === null) {
- $errors[] = 'You must select a title';
- }
- if ($firstName === null) {
- $errors[] = 'You must enter a first name';
- }
- if ($lastName === null) {
- $errors[] = 'You must enter a last name';
- }
- if ($emailAddress === null || filter_var($emailAddress, FILTER_VALIDATE_EMAIL) === false) {
- $errors[] = 'You must enter a valid email address';
- }
- if ($website !== '') {
- if(strpos($website, '://') === false) {
- $website = 'http://' . $website;
- }
- if (filter_var($website, FILTER_VALIDATE_URL, array('flags' => null)) === false) {
- $errors[] = 'You must enter a valid website address';
- }
- }
- if ($message === null) {
- $errors[] = 'You must enter a message';
- }
- if (empty($errors)) {
- require __DIR__ . '/PHPMailer-master/PHPMailerAutoload.php';
- //SMTP needs accurate times, and the PHP time zone MUST be set
- //This should be done in your php.ini, but this is how to do it if you don't have access to that
- date_default_timezone_set('Etc/UTC');
- $mail = new PHPMailer;
- $mail->isSMTP();
- $mail->Host = 'smtp.gmail.com';
- $mail->Port = 587;
- $mail->SMTPSecure = 'tls';
- $mail->SMTPAuth = true;
- $mail->Username = "[email protected]";
- $mail->Password = "YOUR APP SPESIFIC PASSWORD";
- $mail->setFrom($emailAddress, $firstName . ' ' . $lastName);
- $mail->addAddress('[email protected]', 'Bassa Basso');
- $mail->Subject = 'New request';
- $mail->Body = $message . "\r\n\r\nWebsite: " . $website;
- if ($mail->send()) {
- echo json_encode('Form submitted successfully!');
- exit();
- }
- // You may want to log this somewhere
- // $mail->ErrorInfo;
- $errors[] = 'Could not send email, please try again later';
- }
- http_response_code(400);
- echo json_encode($errors);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement