I am using Xampp Server to do a send a message to an email.
I am using the PHP Mailer to do this
I ran into an error and i am not sure how to fix it , I would love some assistance if you have any
<html>
<head>
<title> Sending Email Using PHP </title>
</head>
<body>
<form method="post" action="">
Please enter the email address <input type="email" name="to" placeholder="********@gmail.com">
<br>
Kindly enter the subject <input type="text" name="subject" placeholder = "Requesting all information">
<br>
Kindly enter the message <textarea name="message"> </textarea>
<br><br>
<input type="submit" value="Send">
</form>
<?php
use PHPMailerPHPMailerException;
use PHPMailerPHPMailerPHPMailer;
use PHPMailerPHPMailerSMTP;
require 'PHPMailer/src/Exception.php';
require 'PHPMailer/src/PHPMailer.php';
require 'PHPMailer/src/SMTP.php';
require 'information.php'; //credentials for emails
if($_SERVER["REQUEST_METHOD"] == "POST"){
$to = $_POST["to"];
$subject = $_POST["subject"];
$message = $_POST["message"];
$mail = new PHPMailer (true);
$mail -> Subject = $subject;
$mail -> Body = $message;
$mail -> addAddress ($to);
$mail -> AltBody = $message;
$mail -> setFrom (SEND_FROM, SEND_FROM_NAME);
$mail -> addReplyTo (REPLY_TO, REPLY_TO_NAME);
$mail -> Host = MAILHOST;
$mail -> Username = USERNAME;
$mail -> Password = PASSWORD;
$mail -> Port = 587;
$mail -> isSMTP ();
$mail -> SMTPAuth = true;
$mail -> SMTPSecure = PHPMailer:: ENCRYPTION_STARTTLS;
$mail -> IsHTML (true);
$mail -> send ();
if ($mail == true){
echo "Message sent successfully";
}
else {
echo "Message could not be sent";
}
}
?>
</body>
</html>
This is the credentials for the emails called information.php
<?php
define ('MAILHOST', "smtp.gmail.com");
define ('USERNAME', "*******@gmail.com");
define ('SEND_FROM', "*******[email protected]");
define ('SEND_FROM_NAME', "**** *****");
define ('REPLY_TO', "student email");
define ('REPLY_TO_NAME', "*******");
define ('PASSWORD', "**%^&%*");
?>
The error was
Warning: Undefined array key “to” in (file name) on line 40
Fatal error: Uncaught PHPMailerPHPMailerException: Invalid address:
(to): in 1100 Stack trace: #0 :
PHPMailerPHPMailerPHPMailer->addOrEnqueueAnAddress(‘to’, NULL, ”)
#1(49): PHPMailerPHPMailerPHPMailer->addAddress(NULL) #2 {main} thrown in on line 1100