Skip to content

Instantly share code, notes, and snippets.

@JustAyush
Created May 24, 2020 12:31
Show Gist options
  • Save JustAyush/2d9dfc34c75fff26257e62db667606f3 to your computer and use it in GitHub Desktop.
Save JustAyush/2d9dfc34c75fff26257e62db667606f3 to your computer and use it in GitHub Desktop.
<?php
// Import PHPMailer classes into the global namespace
// These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
// Load Composer's autoloader
require 'vendor/autoload.php';
if (isset($_POST['name']) && isset($_POST['email']) && isset($_POST['message'])) {
$name = htmlentities($_POST['name']);
$email = htmlentities($_POST['email']);
$message = htmlentities($_POST['message']);
// Instantiation and passing `true` enables exceptions
$mail = new PHPMailer(true);
try {
$mail->IsSMTP();
$mail->CharSet = 'UTF-8';
$mail->Host = "smtp.gmail.com"; // SMTP server example
$mail->SMTPDebug = 0; // enables SMTP debug information (for testing)
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Port = 587; // set the SMTP port for the GMAIL server
$mail->Username = '[email protected]'; // SMTP username
$mail->Password = 'n@jik@iko1234'; // SMTP account password example
$mail->setFrom($email, $name);
$mail->addAddress('[email protected]'); // Add a recipient
// Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Message from ' . $email . ' via contact form';
$mail->Body = $message;
$mail->AltBody = $message;
$mail->send();
echo "Message sent";
} catch (Exception $e) {
echo "Message not sent";
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment