Pages

Sunday, December 29, 2019

PHP Mailer Mail send with GMAIL SMTP Settings.

Install Composer and require the PHP Mailer Library

composer require phpmailer/phpmailer

<?php
error_reporting(0);
/**
 * This example shows settings to use when sending via Google's Gmail servers.
 * This uses traditional id & password authentication - look at the gmail_xoauth.phps
 * example to see how to use XOAUTH2.
 * The IMAP section shows how to save this message to the 'Sent Mail' folder using IMAP commands.
 */
//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\Exception;
use PHPMailer\PHPMailer\SMTP;

//Load composer's autoloader
require './vendor/autoload.php';
//Create a new PHPMailer instance

$mail = new PHPMailer;
//Tell PHPMailer to use SMTP

$mail->isSMTP();

//Enable SMTP debugging
// SMTP::DEBUG_OFF = off (for production use)
// SMTP::DEBUG_CLIENT = client messages
// SMTP::DEBUG_SERVER = client and server messages

// $mail->SMTPDebug = SMTP::DEBUG_SERVER;



error_reporting(-1);
ini_set('display_errors', 'On');
set_error_handler("var_dump");


$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$select_price = $_POST['select_price'];
$select_service = $_POST['select_service'];
$subject = isset($_POST['subject']) ? $_POST['subject'] : 'new email sent';
$comments = $_POST['comments'];

// $verify = $_POST['verify'];

if(trim($first_name) == '') {
echo '<div class="error_message">Attention! You must enter your name.</div>';
exit();
}  else if(trim($email) == '') {
echo '<div class="error_message">Attention! Please enter a valid email address.</div>';
exit();
} else if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
echo '<div class="error_message">Attention! You have enter an invalid e-mail address, try again.</div>';
exit();
}

if(trim($comments) == '') {
echo '<div class="error_message">Attention! Please enter your message.</div>';
exit();
}

if(get_magic_quotes_gpc()) {
$comments = stripslashes($comments);
}


// Configuration option.
// i.e. The standard subject will appear as, "You've been contacted by John Doe."

// Example, $e_subject = '$name . ' has contacted you via Your Website.';

$e_subject = 'You\'ve been contacted by ' . $first_name . '.';


// Configuration option.
// You can change this if you feel that you need to.
// Developers, you may wish to add more fields to the form, in which case you must be sure to add them here.

$e_body = "You have been contacted by <strong> $first_name. $first_name </strong>  <br> selected service of <strong> $select_service </strong> , <br> their additional message is as follows:  Customer max budget is $select_price, for this project." . PHP_EOL . PHP_EOL;
$e_content = "\"$comments\"" . PHP_EOL . PHP_EOL;
$e_reply = "You can contact $first_name via email, $email or via phone $phone";

$msg = wordwrap( $e_body . $e_content . $e_reply, 70 );



try {
     //$mail->Host       = "mail.gmail.com"; // SMTP server

      // $mail->SMTPDebug  = 2;                     // enables SMTP debug information (for testing)
      $mail->SMTPAuth   = true;                  // enable SMTP authentication
      $mail->SMTPSecure = "ssl";                 // sets the prefix to the servier
      $mail->Host       = "smtp.gmail.com";      // sets GMAIL as the SMTP server
      $mail->Port       = 465;   // set the SMTP port for the GMAIL server
      $mail->SMTPKeepAlive = true;
      $mail->Mailer = "smtp";
      $mail->Username   = "youremail@gmail.com";  // GMAIL username
      $mail->Password   = "yourgmailpassword";            // GMAIL password
      $mail->AddAddress('receiveremail@gmail.com', 'Info Webnem');
      $mail->addBCC('addbccemail@gmail.com', 'Info Webnem');



  $mail->AddReplyTo($email, $e_reply);

      $mail->SetFrom('emailfrom@gmail.com', $email);
      $mail->Subject = $e_subject;
      $mail->AltBody = 'To view the message, please use an HTML compatible email viewer!'; // optional - MsgHTML will create an alternate automatically
      $mail->MsgHTML($msg);
      if($mail->Send()) {
      echo "<fieldset>";
echo "<div id='success_page'>";
echo "<h1>Email Sent Successfully.</h1>";
echo "<p>Thank you <strong>$first_name</strong>, your message has been submitted to us.</p>";
echo "</div>";
echo "</fieldset>";
  }
} catch (phpmailerException $e) {
      echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
      echo $e->getMessage(); //Boring error messages from anything else!
}