dongtuan1594 2015-07-19 20:11
浏览 42
已采纳

使用Gmail的PHPmailer不起作用

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>
  • 写回答

1条回答 默认 最新

  • duanchifo2866 2015-07-19 20:39
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 unity3d机械臂
  • ¥20 判断两个表是否完全相同
  • ¥20 工控机出现散热器转一下停一下这种情况,是什么原因造成的
  • ¥15 双显卡,启动docker容器报错。
  • ¥15 想编写一个期货跨期套利的程序
  • ¥15 一个线程在sleep的时候set一个信号会起作用吗
  • ¥50 如何利用matlab将现有的地基雷达回波数据调制为机载雷达回波数据
  • ¥100 需求高精度PT100设计电路和算法
  • ¥15 单片机配网,继电器开关,广播
  • ¥60 Qcustomplot绘制实时动态曲线