Skip to content

Instantly share code, notes, and snippets.

@mac10046
Created December 4, 2019 18:13
Show Gist options
  • Save mac10046/6d159d930a4f8b92ac9af2ea463daeb9 to your computer and use it in GitHub Desktop.
Save mac10046/6d159d930a4f8b92ac9af2ea463daeb9 to your computer and use it in GitHub Desktop.
Contact Us Form sender PHP code for sending email at particular email id using a SMTP settings.
<?php
//MAKE SURE THAT THE PHP MAILER FOLDER IS UNDER THE INCLUDE PATH - check phpinfo and ensure that folder is in include_path using php.ini file settings
//ini_set("include_path", './home/rendertechstudio/public_html/quote/phpmailer');
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'src/Exception.php';
require 'src/PHPMailer.php';
require 'src/SMTP.php';
//JSON replier function from https://gist.github.com/james2doyle/33794328675a6c88edd6
function json_response($message = null, $code = 200)
{
// clear the old headers
header_remove();
// set the actual code
http_response_code($code);
// set the header to make sure cache is forced
header("Cache-Control: no-transform,public,max-age=300,s-maxage=900");
// treat this as json
header('Content-Type: application/json');
$status = array(
200 => '200 OK',
400 => '400 Bad Request',
422 => 'Unprocessable Entity',
500 => '500 Internal Server Error'
);
// ok, validation error, or failure
header('Status: '.$status[$code]);
// return the encoded json
return json_encode(array(
'status' => $code < 300, // success or not?
'message' => $message
));
}
// for reading the incoming JSON POST body
$data = json_decode(file_get_contents('php://input'), true);
if($data["contact-name"]) {
$mailbody = $data["contact-name"] . ' from '. $data["company"]. ' has send a quotation request. Please connect to him on ' .$data["email"];
} else if($_POST["contact-name"]) {
$mailbody = $_POST["contact-name"] . ' from '. $_POST["company"]. ' has send a quotation request. Please connect to him on ' .$_POST["email"];
}else {
echo 'Incoming request is invalid';
return;
}
$emailFrom = 'donotreply@example.com';
$emailFromName = 'Example Text';
$emailTo = 'example@example.com';
$emailToName = 'example text';
$smtpUsername = 'donotreply@example.com';
$smtpPassword = 'skexample8';
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPDebug = 0; // 0 = off (for production use) - 1 = client messages - 2 = client and server messages
$mail->Host = "mail.example.com"; // use $mail->Host = gethostbyname('smtp.gmail.com'); // if your network does not support SMTP over IPv6
$mail->Port = 26; // TLS only
$mail->SMTPSecure = false;
$mail->SMTPAutoTLS = false;
//$mail->SMTPSecure = 'tls'; // ssl is depracated
$mail->SMTPAuth = true;
$mail->Username = $smtpUsername;
$mail->Password = $smtpPassword;
$mail->setFrom($emailFrom, $emailFromName);
$mail->addAddress($emailTo, $emailToName);
$mail->Subject = 'Mail from Quotation Landing Page';
$mail->msgHTML($mailbody);
$mail->AltBody = $mailbody;
if(!$mail->send()){
$message = "Mailer Error: " . $mail->ErrorInfo;
echo json_response($message, 400);
}else{
$message = 'Thank You for contacting us, We will contact you back soon.';
echo json_response($message, 200);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment