PHP Build a Basic PHP Website (2018) Adding a Basic Form Utilizing Object Properties and Methods

Joe Sleiman
Joe Sleiman
5,921 Points

sending email to my gmail account by php wamp server

please if someone knows how can i send a message (by a submited form ) to my gmail account .. i'm using wamp server .... example : Name : joe sleiman email: example@gmail.com message: please i need your help!!! submit button

thanks your message has been send to us .. we will reply as soon as possible

and i want this message to receive to my personal email

9 Answers

Mayur Pande
PLUS
Mayur Pande
Courses Plus Student 11,711 Points

Not sure if you resolved this, but I had the same problem using LAMP. I figured out that gmail doesn't allow you to login into unsecure apps that aren't their's you have to go to sign-in & security on your gmail account, and at the bottom there is an option to turn Allow Less Secure Apps: OFF on. You will have to change this option to on. However it is not recommended by gmail. But what I do is turn it on for testing purposes and then turn it back off when I have done testing. Hope this helps

Matthew Bilz
Matthew Bilz
15,829 Points

First, head over to GitHub and download the PHPMailer. Then, you would do something where an HTML form is submitted with email address and a message to you - then you would make a PHP page which is activated by the submission however you would like (if(isset($_POST['submit'])) or simply a new page directed from the form and include something along these lines:

<?php
//Send the email
      require 'PHPMailer/PHPMailerAutoload.php';

      $mail = new PHPMailer;

      //$mail->SMTPDebug = 3;                               // Enable verbose debug output

      $mail->isSMTP();                                      // Set mailer to use SMTP

      $mail->SMTPOptions = array(
      'ssl' => array(
          'verify_peer' => false,
          'verify_peer_name' => false,
          'allow_self_signed' => true
      )
  );

      $mail->Host = "YOUR_HOST";  // Specify main and backup SMTP servers
      $mail->SMTPAuth = true;                               // Enable SMTP authentication
      $mail->Username = "YOUR_EMAIL_ADDRESS";                 // SMTP username
      $mail->Password = "YOUR_PASSWORD";                           // SMTP password
      $mail->SMTPSecure = "tls";                            // Enable TLS encryption, `ssl` also accepted
      $mail->Port = 587;                                    // TCP port to connect to

      $mail->From = "YOUR_EMAIL_ADDRESS";
      $mail->FromName = "YOUR_NAME";
      $mail->addAddress("THEIR_EMAIL_ADDRESS", "THEIR_NAME");     // Add a recipient
      $mail->addAddress("YOUR_EMAIL_ADDRESS", "YOUR_NAME");     // Add a recipient

      $mail->isHTML(true);                                  // Set email format to HTML

      $mail->Subject = 'Thank you for your submission!';
      $mail->Body    = "Your email body.";
      $mail->AltBody = 'Anything you\'d like the mail body to have in it.';

      if(!$mail->send()) {
          echo 'There was an error sending the contact form email. <br>';
          echo 'Mailer Error: ' . $mail->ErrorInfo;
      } 

?>
Joe Sleiman
Joe Sleiman
5,921 Points

i know this solution but i didn't know how let it success... how can i get a SMTP username ... it is free?

Matthew Bilz
Matthew Bilz
15,829 Points

The SMTP username would be your gmail account username and the authentication would be your gmail account password. It goes hand in hand with your gmail account, which provides IMAP/POP as well as SMTP on the same account for free.

Joe Sleiman
Joe Sleiman
5,921 Points

believe me i did all of that and nothing match.... maybe the problem i didn't know actually how i use the username and pass on smtp website ...please can you reach me or lead me

Matthew Bilz
Matthew Bilz
15,829 Points

All you'd do is put 'smpt.gmail.com' as your SMTP Host as well as your Gmail username and password for the Username and Password fields and that should get your emails sent from your Gmail account.

Joe Sleiman
Joe Sleiman
5,921 Points

assume my gmail account is joe@gmail.com and my pass is 123456 can you resend for me the code and where should i put the required fields.... i concluded that i don't want to access the site of smpt and sign up anything ..right??? best regards

Joe Sleiman
Joe Sleiman
5,921 Points

Message could not be sent.Mailer Error: Could not instantiate mail function.

Joe Sleiman
Joe Sleiman
5,921 Points

$mail->addAddress("THEIR_EMAIL_ADDRESS", "THEIR_NAME"); // Add a recipient THEIR_EMAIL_ADDRESS : i put $email : the email that a user fill it on the form THEIR_NAME : i put $name the name that the user fill it on the form

Matthew Bilz
Matthew Bilz
15,829 Points

Perhaps change the addAddress syntax to simply ($email) instead of ($email, $name) --- sometimes that works for whatever reason.

Other than that, I'd check to make sure your include file is in the right place for your mailer autoload?

Joe Sleiman
Joe Sleiman
5,921 Points

yes i include the phpmailer in the right place in www directory and i included it in the file by require

Dennis Amiel Domingo
Dennis Amiel Domingo
13,403 Points

The solution:

  1. Go to https://myaccount.google.com/ and log in to your account.
  2. Click "Sign-in & security.
  3. Scroll down to "Allow less secure apps: OFF" and turn it to "ON".

This is my set up:

//Import the PHPMailer class into the global namespace
use PHPMailer\PHPMailer\PHPMailer;
require 'vendor/PHPmailer/src/PHPMailer.php';
require 'vendor/PHPmailer/src/SMTP.php';
require 'vendor/PHPmailer/src/Exception.php';

if(!PHPMailer::validateAddress($email)){
    echo "Invalid Email Address";
    exit;
}

$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPAuth = true;
$mail->Host = "smtp.gmail.com";
$mail->Port = 587;
$mail->Username = "your email address";
$mail->Password = "your password";
$mail->SMTPDebug = 2;

$mail->setFrom('your email address', $name);
$mail->addReplyTo($email, $name);
$mail->addAddress('your email address', 'your name');
$mail->Subject = 'Library suggestion from ' . $name;
$mail->Body = $email_body;
if (!$mail->send()) {
    echo "Mailer Error: " . $mail->ErrorInfo;
    exit;
} 
header("location:suggest.php?status=thanks");
Matthew Bilz
Matthew Bilz
15,829 Points
//Send the email
      require 'PHPMailer/PHPMailerAutoload.php';

      $mail = new PHPMailer;

      //$mail->SMTPDebug = 3;                               // Enable verbose debug output

      $mail->isSMTP();                                      // Set mailer to use SMTP

      $mail->SMTPOptions = array(
      'ssl' => array(
          'verify_peer' => false,
          'verify_peer_name' => false,
          'allow_self_signed' => true
      )
  );

      $mail->Host = "smtp.gmail.com";  // Specify main and backup SMTP servers
      $mail->SMTPAuth = true;                               // Enable SMTP authentication
      $mail->Username = "joe@gmail.com";                 // SMTP username
      $mail->Password = "123456";                           // SMTP password
      $mail->SMTPSecure = "tls";                            // Enable TLS encryption, `ssl` also accepted
      $mail->Port = 587;                                    // TCP port to connect to

      $mail->From = "joe@gmail.com";
      $mail->FromName = "Joe";
      $mail->addAddress("THEIR_EMAIL_ADDRESS", "THEIR_NAME");     // Add a recipient
      $mail->addAddress("joe@gmail.com", "Joe");     // Add a recipient

      $mail->isHTML(true);                                  // Set email format to HTML

      $mail->Subject = 'Thank you for your submission!';
      $mail->Body    = "Your email body.";
      $mail->AltBody = 'Anything you\'d like the mail body to have in it.';

      if(!$mail->send()) {
          echo 'There was an error sending the contact form email. <br>';
          echo 'Mailer Error: ' . $mail->ErrorInfo;
      } 
Joe Sleiman
Joe Sleiman
5,921 Points

if i copied it entirely as it is .. it will run ??? $mail->Host = "smtp.gmail.com"; and this i copied like it is ??? i tried it perfectly like you say nothing run ....error