Kindly asking for any advise on how I can get the php mailer working on this code.
ive been working on this for about 3 weeks now and just cant get it to run.
I don’t have anyone to ask. I didn’t go to any school just trying to learn how to code by my self using tutorials and web infos.
I tried changing ports ,
I uploaded the latest version of phpmailer, uploaded the whole folder to htdocs ,
I created a php file with the coide below, placed it inside htdocs
there is no error on the browser , but on the browser logs it shows
HTTP500: SERVER ERROR - The server encountered an unexpected condition that prevented it from fulfilling the request.
POST - http://www.justtestingaform.rf.gd/part1.php
0: Unable to get property ‘SavePersonalAndPaymentData’ of undefined or null reference
Im using the code below:
?php
$error = '';
$name = '';
$email = '';
$subject = '';
$message = '';
function clean_text($string)
{
$string = trim($string);
$string = stripslashes($string);
$string = htmlspecialchars($string);
return $string;
}
if(isset($_POST["submit"]))
{
if(empty($_POST["name"]))
{
$error .= '<p><label class="text-danger">Please Enter your Name</label></p>';
}
else
{
$name = clean_text($_POST["name"]);
if(!preg_match("/^[a-zA-Z ]*$/",$name))
{
$error .= '<p><label class="text-danger">Only letters and white space allowed</label></p>';
}
}
if(empty($_POST["email"]))
{
$error .= '<p><label class="text-danger">Please Enter your Email</label></p>';
}
else
{
$email = clean_text($_POST["email"]);
if(!filter_var($email, FILTER_VALIDATE_EMAIL))
{
$error .= '<p><label class="text-danger">Invalid email format</label></p>';
}
}
if(empty($_POST["subject"]))
{
$error .= '<p><label class="text-danger">Subject is required</label></p>';
}
else
{
$subject = clean_text($_POST["subject"]);
}
if(empty($_POST["message"]))
{
$error .= '<p><label class="text-danger">Message is required</label></p>';
}
else
{
$message = clean_text($_POST["message"]);
}
if($error == '')
{
require 'PHPmailer/src/PHPMailer.php';
require 'PHPmailer/src/SMTP.php';
require './PHPMailer/src/Exception.php';
$mail = new PHPMailer;
$mail->Host = 'smtp.gmail.com'; //Sets the SMTP hosts
$mail->SMTPSecure = 'ssl'; //Sets connection prefix. Options are "", "ssl" or "tls"
$mail->Port = '465'; //Sets the default SMTP server port
$mail->SMTPAuth = true; //Sets SMTP authentication. Utilizes the Username and Password variables
$mail->Username = '[email protected]'; //Sets SMTP username
$mail->Password = 'password'; //Sets SMTP password
$mail->From = $_POST["email"]; //Sets the From email address for the message
$mail->FromName = $_POST["name"]; //Sets the From name of the message
$mail->AddAddress('[email protected]', 'Name');//Adds a "To" address
$mail->AddCC($_POST["email"], $_POST["name"]); //Adds a "Cc" address
$mail->WordWrap = 50; //Sets word wrapping on the body of the message to a given number of characters
$mail->IsHTML(true); //Sets message type to HTML
$mail->Subject = $_POST["subject"]; //Sets the Subject of the message
$mail->Body = $_POST["message"]; //An HTML or plain text message body
if($mail->Send()) //Send an Email. Return true on success or false on error
{
$error = '<label class="text-success">Thank you for contacting us</label>';
}
else
{
$error = '<label class="text-danger">There is an Error</label>';
}
$name = '';
$email = '';
$subject = '';
$message = '';
}
}
?>
On the contact form HTML file put required
attributes on all the fields, and on the email field put as type email
. On the autoloader.php file put this code:
<?php require "/path/to/Exception.php"; require "/path/to/PHPMailer.php"; require "/path/to/SMTP.php"; ?>
overwriting /path/to/
with the location of the PHPMailer class required files. Then use as preprocessing code this:
<?php
date_default_timezone_set("Etc/UTC");
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require "/path/to/autoloader.php";
$mail = new PHPMailer(true);
//$mail->setLanguage("lang", "/path/to/PHPMailer/language/");
$name = $_POST["name"];
$email = $_POST["email"];
$subject = $_POST["subject"];
$message = $_POST["message"];
try {
$mail->isSMTP();
$mail->Host = "ssl://smtp.gmail.com:465"; // Hostname - it could include even protocols and ports if it wouldn't work the first time
$mail->SMTPAuth = true;
$mail->Username = "yourgmail"; // overwrite "yourgmail" with the Gmail you're going to use
$mail->Password = "********"; // overwrite the asterisks with your Gmail password
$mail->setFrom($email, $name);
$mail->addAddress("yourGmail"); // overwrite "yourGmail" with your own Gmail address
$mail->addCC($email, $name);
$mail->isHTML(true);
$mail->Subject = $subject;
$mail->Body = $message;
$mail->AltBody = $message;
$mail->send();
echo("Thank you for contacting us!");
die();
} catch (Exception $e) {
echo("There is an error on the mailer. Error: ".$e->getMessage());
die();
}
?>
replacing /path/to/ with the location of the autoloader.php file and following the instructions on the comments.
Im so happy your reply was very fast.
which one is the autoloader. php?
is this the file Oauth. php inside the Phpmailer folder I downloaded from the site.
Thank you this will help a lot
You need to create the autoloader.php file if it doesn’t exist on the same folder where you put the mailer processor file with the contents I specified on the quote below:
overwriting /path/to/
with the location of the PHPMailer class required files. The OAuth.php is only needed if you want to connect to SMTP using the Gmail’s OAuth Keys.
This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.