-1

I'd like to create an email form using gmail but can't get it working. I downloaded PHPmailer and uploaded them. The form works, it also checks the if the email adsress is correct but does not show a message after hitting the submit button and does not send an email.

I also turned on the access for less secured apps in my gmail settings.

    <?php

// functie spamcheck
function spamcheck($field)
  {
  //filter_var() sanitizes de email 
  $field=filter_var($field, FILTER_SANITIZE_EMAIL);

  //filter_var() valideert de email
  if(filter_var($field, FILTER_VALIDATE_EMAIL))
    {
    return true;
    }
  else
    {
    return false;
    }
  }

if($_SERVER['REQUEST_METHOD'] == 'POST') 
{  


// PHP mailer settings instellen voor GMAIL
require_once ('contactform/phpmailer/class.phpmailer.php'); // het pad vanaf dit fomulier naar "class.phpmailer.php"
$mail = new PHPMailer(true);
$mail->CharSet = 'utf-8'; //character set utf-8 
$mail->IsSMTP();  // smtp protocol gebruiken voor de email te verzenden 
$mail->Host = "smtp.gmail.com"; // smtp servernaam van gmail
$mail->Port = "587";  // smtp poort voor gmail 465 or 587
$mail->SMTPSecure = "tls"; //gmail authenticeert door ssl ( andere optie is tls )
$mail->SMTPAuth = true; 
// account gegevens voor authenticatie Gmailserver
$mail->Username = "ethannn@gmail.com"; 
$mail->Password = "mypassword";
$mail->From = $_POST['email']; 
$mail->FromName = $_POST['naam']; 
$mail->AddAddress("ethannn@email.com", "Jouw Naam"); // emailadres ontvanger en de naam die in email verschijnt
$mail->Subject = "Contactformulier";
// variabelen voor de body en body email opmaken
$naam = $_POST['naam'];
$achternaam = $_POST['achternaam'];
$email = $_POST['email'];
$bericht = $_POST['bericht'];
// body opmaken
$body = "";
$body .= "Naam: ";
$body .= $naam;
$body .= "<br />";
$body .= "Achternaam: ";
$body .= $achternaam;
$body .= "<br />";    
$body .= "Email: ";
$body .= $email;
$body .= "<br />";
$body .= "Bericht: ";
$body .= $bericht;
$body .= "<br />";

$mail->WordWrap = 80; 
$mail->MsgHTML($body, dirname(__FILE__), true); // genereren van bodybericht 

// check of submitter een robot is en of er geldige input is geleverd
$mailcheck = spamcheck($_POST['email']);

if($_POST['robot'] != "test_spambot") {
    die();                  
} 
//check of email geldig is
elseif ($mailcheck == false) {
    echo "Ongeldige input van emailadres";
}
else {

// email verzenden 
$formsent = $mail->Send(); 

// echo's als verzenden goed of fout is gegaan
if ($formsent){
  echo 'Uw bericht is successvol verstuurd!'; 
}
else{
  echo 'Sorry, maar er is iets misgegaan met het versturen van het formulier; probeer het later nog eens.'; 
    }
  }

} // eind request method
?>

<form action="" method="post" name="" id="">
  <input type="hidden" name="robot" value="test_spambot" /><br />
  Naam: <br />
  <input type="text" name="naam" value ="ethannn"/><br />
  Achternaam: <br />
  <input type="text" name="achternaam" value ="Kikker"/><br />
  Email: <br />
  <input type="text" name="email" value ="kikker@gmail.com"/><br />
  Bericht: <br />
  <textarea name="bericht" />blablabla</textarea>
  <br /><br />
  <input type="reset" value="Reset" />
  <input type="submit" value="Verzenden" />
</form>
Ethannn
  • 191
  • 1
  • 2
  • 16
  • possible duplicate of [send email using Gmail SMTP server through PHP Mailer](http://stackoverflow.com/questions/16048347/send-email-using-gmail-smtp-server-through-php-mailer) – Shahzad Barkati Jul 19 '15 at 20:13
  • thank you for the link but could not find the answer there – Ethannn Jul 19 '15 at 20:19

1 Answers1

1

You should download PHPMailer from here - https://github.com/PHPMailer/PHPMailer

There is a working use case for gmail in examples folder named gmail.phps.

You should change your script on the basis of example script.

  • edit $mail->Port = 578 // port number is not a string
  • add $mail->SMTPDebug = 2; // for debuging you will se why is your script failing
  • edit require_once() // you should require PHPMailerAutoload.php
  • edit $mail->Send() // its $mail->send()

after these changes you should be able successfully send message if your credentials are correct..

Here is the complete version

    <?php

// functie spamcheck
function spamcheck($field)
  {
  //filter_var() sanitizes de email 
  $field=filter_var($field, FILTER_SANITIZE_EMAIL);

  //filter_var() valideert de email
  if(filter_var($field, FILTER_VALIDATE_EMAIL))
    {
    return true;
    }
  else
    {
    return false;
    }
  }

if($_SERVER['REQUEST_METHOD'] == 'POST') 
{  


// PHP mailer settings instellen voor GMAIL
require_once ('PHPMailerAutoload.php'); // het pad vanaf dit fomulier naar "class.phpmailer.php"
$mail = new PHPMailer();
$mail->CharSet = 'utf-8'; //character set utf-8 
$mail->IsSMTP();  // smtp protocol gebruiken voor de email te verzenden 
$mail->Host = "smtp.gmail.com"; // smtp servernaam van gmail
$mail->Port = 587;  // smtp poort voor gmail 465 or 587
$mail->SMTPSecure = "tls"; //gmail authenticeert door ssl ( andere optie is tls 
$mail->SMTPDebug = 2;
$mail->SMTPAuth = true; 
// account gegevens voor authenticatie Gmailserver
$mail->Username = "ethannn@gmail.com"; 
$mail->Password = "mypassword";
$mail->From = $_POST['email']; 
$mail->FromName = $_POST['naam']; 
$mail->AddAddress("ethannn@email.com", "Jouw Naam"); // emailadres ontvanger en de naam die in email verschijnt
$mail->Subject = "Contactformulier";
// variabelen voor de body en body email opmaken
$naam = $_POST['naam'];
$achternaam = $_POST['achternaam'];
$email = $_POST['email'];
$bericht = $_POST['bericht'];
// body opmaken
$body = "";
$body .= "Naam: ";
$body .= $naam;
$body .= "<br />";
$body .= "Achternaam: ";
$body .= $achternaam;
$body .= "<br />";    
$body .= "Email: ";
$body .= $email;
$body .= "<br />";
$body .= "Bericht: ";
$body .= $bericht;
$body .= "<br />";

$mail->WordWrap = 80; 
$mail->MsgHTML($body, dirname(__FILE__), true); // genereren van bodybericht 

// check of submitter een robot is en of er geldige input is geleverd
$mailcheck = spamcheck($_POST['email']);

if($_POST['robot'] != "test_spambot") {
    die();                  
} 
//check of email geldig is
elseif ($mailcheck == false) {
    echo "Ongeldige input van emailadres";
}
else {

// email verzenden 
$formsent = $mail->send(); 

// echo's als verzenden goed of fout is gegaan
if ($formsent){
  echo 'Uw bericht is successvol verstuurd!'; 
}
else{
  echo 'Sorry, maar er is iets misgegaan met het versturen van het formulier; probeer het later nog eens.'; 
    }
  }

} // eind request method
?>

<form action="" method="post" name="" id="">
  <input type="hidden" name="robot" value="test_spambot" /><br />
  Naam: <br />
  <input type="text" name="naam" value ="ethannn"/><br />
  Achternaam: <br />
  <input type="text" name="achternaam" value ="Kikker"/><br />
  Email: <br />
  <input type="text" name="email" value ="kikker@gmail.com"/><br />
  Bericht: <br />
  <textarea name="bericht" />blablabla</textarea>
  <br /><br />
  <input type="reset" value="Reset" />
  <input type="submit" value="Verzenden" />
  </form>

don't forget add PHPMailerAutoload.php, class.smtp.php and class.phpmailer.php from Github repo i posted into directory where is your script placed

xrep
  • 185
  • 4
  • 14
  • thank you xrep. that worked. I do receive an email now with the contents provided by the user. There comes however a an error with it too. – Ethannn Jul 19 '15 at 21:21
  • Delivery to the following recipient failed permanently: ethan@email.com Technical details of permanent failure: Google tried to deliver your message, but it was rejected by the server for the recipient domain email.com by mx01.gmx.com. [74.208.5.27]. The error that the other server returned was: 550 Requested action not taken: mailbox unavailable ----- Original message ----- DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:date:to:subject:message-id:mime-version:content-type :content-transfer-enc.......................... – Ethannn Jul 19 '15 at 21:22
  • looks like ethannn@email.com email address does not exists..maybe you have typo? $mail->AddAddress("ethannn@email.com", "Jouw Naam"); Is email address ethannn@email.com correct?? Try to put there ethannn@gmail.com or some other valid mail address – xrep Jul 19 '15 at 21:28
  • Thank you xrep, you helped me a lot with this! – Ethannn Jul 19 '15 at 21:41
  • Still one question. It all works now but after submitting I get all this server information like: 2015-07-20 07:08:32 SERVER -> CLIENT: 220 smtp.gmail.com ESMTP fa8sm10316664wib.14 - gsmtp 2015-07-20 07:08:32 CLIENT -> SERVER: EH.............. how can I prevent this from being shown – Ethannn Jul 20 '15 at 07:11
  • from your code remove line **$mail->SMTPDebug = 2;** – xrep Jul 20 '15 at 10:09