PHPMailer error parsing script

OK so, just trying to get through a not for profit project and full disclosure I know pretty much nothing about PHP. That being said… I wondered if one of you would be kind enough to fix this mess for me :



<!DOCTYPE html>
<html lang="en-US">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
	
	 <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Contact form</title>

	<link href="ContactForm.css" rel="stylesheet" type="text/css"/>
	<link href="https://fonts.googleapis.com/css?family=lobster|Lobster+Two" rel="stylesheet" >
</head>

<body> 
<div class="banner"> 


<h1>Conact Form</h1>

</div>

<div class="navbar">
	
	

	
	
		
	
	 <a target="_self" href="..\mytest.html">Home </a>
	 
		
	<div class="dropdown">
		
	<button class="dropbtn">Region </button> 
	
		<div class="dropdown-content">
			<a  target="_self" href="..\REC_Page.html">Regional Executive Council</a>
			<a target="_self" href="..\RegionPages\Commissions\Commissions.html">Council and Commissions</a>
			<a href="#">Calendar</a>
			<a href="#">Guidelines</a>
			<a href="#">Forms</a>
			<a href="#">Projects</a>
			<a href="#">Meditations</a>
			<a href="#">Newsletter</a>
			
  
		</div>
	  </div>
		
		
		
		<div class="dropdown">
	<button class="dropbtn">Families</button> 
	
		<div class="dropdown-content">
		
  
    <a target="_self" href="..\Thomas.html">St Thomas More </a>
    <a target="_self" href="..\Margaret.html">St. Margaret of Cortona </a>
    <a target="_self" href="..\Elizabeth.html">St. Elizabeth of Hungary </a>
	<a target="_self" href="..\FratMap.html"> Map</a>
			</div>
			</div>


	<div class="dropdown">
		
		<button class="dropbtn">Order</button> 
	
		<div class="dropdown-content">
    <a href="#">Rule</a>
    <a href="#">Constitutions</a>
    <a href="#">Statues</a>
	</div>
  </div>
  
 <div class="dropdown">
		
	<button class="dropbtn">Vacations </button> 
	
		<div class="dropdown-content">
    <a href="..\VacationsPages\.html">Come and See </a>
    <a href="..\VocationsPages\.html">Formation</a>
  </div>
  </div>
  
	<a target="_self" href="..\EventsPage\EventListing.html">Events</a>

  <a target= "_self" href="..\contact\contactform.html">Contact Us </a>
  
  
  
  <a target= "_self" href="..\form.html">Prayer Requests</a>
  
	

</div>

<div class="main">

<div class="navigation">

		 <div class="groupHolder">
    
      <a href="#">Home</a>
	  
	  </div>
	  <div class="groupHolder">
     
			<a href="#">Region</a>
	  
	  
	  	<div class="dropdown-content">	
			
			<a  target="_self" href="#">Regional Executive Council</a>
			<a target="_self" href="Commissions.html">Council and Commissions</a>
			<a href="#">Calendar</a>
			<a href="#">Guidelines</a>
			<a href="#">Forms</a>
			<a href="#">Projects</a>
			<a href="#">Mediations</a>
			<a href="#">Newsletter</a>
			
  
		</div>
	  </div>
	  
	  	  <div class="groupHolder">
	  
	  
      <a href="#">Family</a>
	  
			<div class="dropdown-content">
				<a href="#"> Thomas </a>
				<a href="#">Margaret </a>
				<a href="#"> Elizabeth</a>
			
			</div>
			</div>
	   <div class="groupHolder">
	   <a href="#"> Order</a>
	   
	   </div>
	   
	   <div class="groupHolder">
	   <a href="#"> Events </a>
	   
	   </div>
	   
	   <div class="groupHolder">
	   
	   <a href="#"> Vacations</a>
	   
			<div class="dropdown-content">
				<a href="#">Come visit </a>
				<a href="#">Formation </a>
			</div>
		</div>
		
		<div class="groupHolder">		
	   <a href="#"> Contact US</a>
	   </div>
	   
	   <div class="groupHolder">
	  <a href="#">  Requests</a>
	   </div>
	
</div>







<form action="/action_page.php" method="post">

<p>First Name : </p>
<input type = "text" name="firstN" size="15">

<p>Last Name :  </p>
<input type = "text" name="lastN" size ="25"> <br>  <br> 

<p>Email : </p>
<input Type="email" name="email" size"50" > <br>  <br> 

<p>Phone number : </p>

  <input type="tel" name="phone" pattern="[0-9]{3}-[0-9]{2}-[0-9]{3}"><br>  <br> 

<p>Comments : </p><br>
<textarea  name = "comments" rows="10   " cols ="80" >Comments :</textarea> <br>  <br> 

<input type="submit" value="send"> 

/* begin test*/
<?php

  use PHPMailer\PHPMailer\PHPMailer;
  use PHPMailer\PHPMailer\Exception;
  require 'PHPMailer-master/src/Exception.php';
  require 'PHPMailer-master/src/PHPMailer.php';
  require 'PHPMailer-master/src/SMTP.php';

if(isset($_POST["submit"]))
{


  $mail = new PHPMailer();

  $mail->IsSMTP();

  $mail->SMTPDebug  = 2;  
  $mail->SMTPAuth   = TRUE;
  $mail->SMTPSecure = "tls";
  $mail->Port       = 587;
  $mail->Host       = "smtp.gmail.com";
  $mail->Username   = "myemail@gmail.com";
  $mail->Password   = "mypassword";

  $mail->IsHTML(true);
  $mail->AddAddress('ffmr.contac@gmail.com');
  $mail->setFrom($_POST['email'],$_POST['firstN'] $_POST['lastN']);
  $mail->Subject = "Contact from FFMR Regional Website";
  $content ="<p> <b>Contact from the FFRM Regional Website : </b> </p><p>Name:   .$POST_[firstN]  .$POST_[lastN]<p></br><p>Email address: .$POST_[email]</p></br> <p>Message: ".$POST_[comments] </p>";

  $mail->MsgHTML($content); 
  if(!$mail->Send()) {
    echo echo "Fail - " . $mail->ErrorInfo;
    var_dump($mail);
  } else {
    echo "Email sent successfully";
  }

}
?>



</form>

</div>









</body>

</html>

I never used PhpMailer but I found many mistake in the above php code and the biggest mistake is you are using PhpMailer code inside a
if condition i.e.

if(isset($_POST["submit"])){

and when you submit the form using submit button it checks for the condition and the condition fails every time ,because it never found $_POST['submit'] and your PhpMailer code never runs.
You can fix this issue by giving name="submit" to the submit input i.e.

<input type="submit" name="submit" value="send"> 
1 Like

I have updated the script… and that seemed to get the ball rolling, however, it also uncovered another issue ( as it seems to always be the case LOL) -
Here is the code I am currently using :

<form action="" method="post">
 
<p>First Name :
<input type = "text" name="firstN" size="15">
 
Last Name :  
<input type = "text" name="lastN" size ="25"> </p> <br>  <br>
 
<p>Email : </p>
<input Type="email" name="email" size"50" > <br>  <br>
 
<p>Phone number : (format 999-999-9999 include the dashes) </p>
 
  <input type="tel" name="phone" pattern="[0-9]{3}-[0-9]{3}-[0-9]{4}"><br>  <br>
 
<p>Comments : </p><br>
<textarea  name = "comments" rows="10   " cols ="80" >Comments :</textarea> <br>  <br>
 
<input type="submit" name="submit" value="send">
 
 
<?php
 
  use PHPMailer\PHPMailer\PHPMailer;
  use PHPMailer\PHPMailer\Exception;
  require 'Exception.php';
  require 'PHPMailer.php';
  require 'SMTP.php';
 
if(isset($_POST["submit"]))
{
 
 
  $mail = new PHPMailer(TRUE);
 
  $mail->IsSMTP();
 
  $mail->SMTPDebug  = 2;  
  $mail->SMTPAuth   = TRUE;
  $mail->SMTPSecure = "tls";
  $mail->Port       = 587;
  $mail->Host       = "smtp.gmail.com";
  $mail->Username   = "nope@gmail.com";
  $mail->Password   = "nonono";
 
  $mail->IsHTML(true);
  $mail->AddAddress('notemail@gmail.com');
  $mail->setFrom($_POST['email']);
  $mail->Subject = "Contact from  Website";
  $content ="<p> <b>Contact from the  Website : </b> </p><p>Name: " . $POST_['firstN'] ." ". $POST_['lastN'] ."</p></br><p>Email address: " . $POST_['email'] . "</p></br> <p>Message: " . $POST_['comments'] . "</p>";
 
  $mail->MsgHTML($content);
 
  $mail->Send();
  if(!$mail->Send()) {
    echo "Fail - " . $mail->ErrorInfo;
    var_dump($mail);
  } else {
    echo "Email sent successfully";
  }
 
}
?>
 
 
 
</form>
 
</div>

and here is the output :

Notice: Undefined variable: POST_ in C:\xampp\htdocs\FFMRTest\ContactForm.php on line 228

Notice: Trying to access array offset on value of type null in C:\xampp\htdocs\FFMRTest\ContactForm.php on line 228

Notice: Undefined variable: POST_ in C:\xampp\htdocs\FFMRTest\ContactForm.php on line 228

Notice: Trying to access array offset on value of type null in C:\xampp\htdocs\FFMRTest\ContactForm.php on line 228

Notice: Undefined variable: POST_ in C:\xampp\htdocs\FFMRTest\ContactForm.php on line 228

Notice: Trying to access array offset on value of type null in C:\xampp\htdocs\FFMRTest\ContactForm.php on line 228

Notice: Undefined variable: POST_ in C:\xampp\htdocs\FFMRTest\ContactForm.php on line 228

Notice: Trying to access array offset on value of type null in C:\xampp\htdocs\FFMRTest\ContactForm.php on line 228
2020-03-07 13:14:03 SERVER -> CLIENT: 220 smtp.gmail.com ESMTP z12sm2468359ywj.42 - gsmtp
2020-03-07 13:14:03 CLIENT -> SERVER: EHLO localhost
2020-03-07 13:14:03 SERVER -> CLIENT: 250-smtp.gmail.com at your service, [47.205.9.56]250-SIZE 35882577250-8BITMIME250-STARTTLS250-ENHANCEDSTATUSCODES250-PIPELINING250 SMTPUTF8
2020-03-07 13:14:03 CLIENT -> SERVER: STARTTLS
2020-03-07 13:14:03 SERVER -> CLIENT: 220 2.0.0 Ready to start TLS
SMTP Error: Could not connect to SMTP host.
2020-03-07 13:14:04 CLIENT -> SERVER: QUIT
2020-03-07 13:14:04
2020-03-07 13:14:04
SMTP Error: Could not connect to SMTP host.

Fatal error: Uncaught PHPMailer\PHPMailer\Exception: SMTP Error: Could not connect to SMTP host. in C:\xampp\htdocs\FFMRTest\PHPMailer.php:2013 Stack trace: #0 C:\xampp\htdocs\FFMRTest\PHPMailer.php(1844): PHPMailer\PHPMailer\PHPMailer->smtpConnect(Array) #1 C:\xampp\htdocs\FFMRTest\PHPMailer.php(1587): PHPMailer\PHPMailer\PHPMailer->smtpSend(‘Date: Sat, 7 Ma…’, ‘This is a multi…’) #2 C:\xampp\htdocs\FFMRTest\PHPMailer.php(1423): PHPMailer\PHPMailer\PHPMailer->postSend() #3 C:\xampp\htdocs\FFMRTest\ContactForm.php(232): PHPMailer\PHPMailer\PHPMailer->send() #4 {main} thrown in C:\xampp\htdocs\FFMRTest\PHPMailer.php on line 2013

The offending line of code is the line that starts " $content = "
it appears as though the variable names are not getting the values from the form ?

Thank you also for your assistance and reply !
Mark

You are typing $POST_ instead of $_POST i.e.

  $content ="<p> <b>Contact from the  Website : </b> </p><p>Name: " . $POST_['firstN'] ." ". $POST_['lastN'] ."</p></br><p>Email address: " . $POST_['email'] . "</p></br> <p>Message: " . $POST_['comments'] . "</p>";

so change all $POST_ with $_POST like below.

  $content ="<p> <b>Contact from the  Website : </b> </p><p>Name: " . $_POST['firstN'] ." ". $_POST['lastN'] ."</p><br><p>Email address: " . $_POST['email'] . "</p><br> <p>Message: " . $_POST['comments'] . "</p>";

Perfect ! However, the last part is that to SMTP connection still fails for some reason ? Here is the out put

2020-03-08 13:43:34 SERVER -> CLIENT: 220 smtp.gmail.com ESMTP u2sm6188620ywa.60 - gsmtp
2020-03-08 13:43:34 CLIENT -> SERVER: EHLO localhost
2020-03-08 13:43:34 SERVER -> CLIENT: 250-smtp.gmail.com at your service, [47.205.9.56]250-SIZE 35882577250-8BITMIME250-STARTTLS250-ENHANCEDSTATUSCODES250-PIPELINING250 SMTPUTF8
2020-03-08 13:43:34 CLIENT -> SERVER: STARTTLS
2020-03-08 13:43:34 SERVER -> CLIENT: 220 2.0.0 Ready to start TLS
SMTP Error: Could not connect to SMTP host.
2020-03-08 13:43:35 CLIENT -> SERVER: QUIT
2020-03-08 13:43:35 SERVER -> CLIENT:
2020-03-08 13:43:35 SMTP ERROR: QUIT command failed:
SMTP Error: Could not connect to SMTP host.

Fatal error : Uncaught PHPMailer\PHPMailer\Exception: SMTP Error: Could not connect to SMTP host. in C:\xampp\htdocs\FFMRTest\PHPMailer.php:2013 Stack trace: #0 C:\xampp\htdocs\FFMRTest\PHPMailer.php(1844): PHPMailer\PHPMailer\PHPMailer->smtpConnect(Array) #1 C:\xampp\htdocs\FFMRTest\PHPMailer.php(1587): PHPMailer\PHPMailer\PHPMailer->smtpSend(‘Date: Sun, 8 Ma…’, ‘This is a multi…’) #2 C:\xampp\htdocs\FFMRTest\PHPMailer.php(1423): PHPMailer\PHPMailer\PHPMailer->postSend() #3 C:\xampp\htdocs\FFMRTest\ContactForm.php(232): PHPMailer\PHPMailer\PHPMailer->send() #4 {main} thrown in C:\xampp\htdocs\FFMRTest\PHPMailer.php on line 2013

I will search around … and again, many thanks for the assistance… :slight_smile:

UPDATE** We have success ! ( Happy Dance !)
OK , I would like an opinion. I could redirect the output ( it is set to verbose) to a log file, ( that would require some form of maintenance maybe a monthly deleting of that file… or some type of archive so as to not allow it to grow too large.)

In this particular application I dont have the need for a corporate level of email screening/security etc… but I should probably do it as a matter of course .

One thing for sure, nothing like a project to give you some insight into things you don’t know.

If you ever make it to Tampa bay… I will invite you for a beer or coffee or whatever you prefer…

Thank you !

As I understand you want to create a log file in which you can store the output (data submitted by form) . so you can do this by using php file handling as follows =>

$fileName="log.txt";
if(file_exists($fileName)){
	$file=fopen($fileName,"r");
	$lines=fread($file,filesize($fileName));
	fclose($file);
	$obj=json_decode($lines,true);
	$date=date('Y-m-d');
	if(isset($obj[$date])){
		array_push($obj[$date],array("First Name"=>$_POST["firstN"],"Last Name"=>$_POST['lastN'],"Email"=>$_POST['email'],"Phone number"=>$_POST["phone"],"Comments"=>$_POST["comments"]));
		$write_file=fopen($fileName,"w");
		fwrite($write_file,json_encode($obj));
		fclose($write_file);
	}else{
		$obj[$date]=[array("First Name"=>$_POST["firstN"],"Last Name"=>$_POST['lastN'],"Email"=>$_POST['email'],"Phone number"=>$_POST["phone"],"Comments"=>$_POST["comments"])];
		$write_file=fopen($fileName,"w");
		fwrite($write_file,json_encode($obj));
		fclose($write_file);
	}
}else{
	$obj=array(date("Y-m-d")=>[array("First Name"=>$_POST["firstN"],"Last Name"=>$_POST['lastN'],"Email"=>$_POST['email'],"Phone number"=>$_POST["phone"],"Comments"=>$_POST["comments"])]);
	$file=fopen($fileName,"w");
	fwrite($file,json_encode($obj));
	fclose($file);
}

and for maintenance of that file you can use the below code :

$fileName="log.txt";
$file=fopen($fileName,"r");
$lines=fread($file,filesize($fileName));
fclose($file);
$obj=json_decode($lines,true);
$date=date('Y-m-d');
foreach($obj as $key=>$value){
	$today_date=new DateTime($date);
	$check_date=new DateTime($key);
	$diffrence=$today_date->diff($check_date);
	if($diffrence->d >=28 || $diffrence->m >0 || $diffrence->y >0){
		unset($obj[$key]);
	}
}

it will delete all the data that is older than 28 days.