A few days ago, I started working on my first project that I was assigned at work. Today, I came across an error that I get on PHPMailer. Essentially, when it should send the email taken with the $_POST, it doesn't. Instead it sends the email to the one (email) that should receive it.
Here's my code.
index.php:
TOP PART:
<?php include 'db.php';?>
<?php include 'function.php';?>
<?php
if(isset($_POST['subcon'])){
$sender = new SendData();
$sender->SendEmails($_POST['email'],$_POST['message']);
}
?>
FORM PART:
<div class="container-fluid">
<main class="form-signin">
<form method='post' action='index.php'>
<h1 class="mb-3 contact-text">Contact Me</h1>
<div class="form-floating mb-3">
<input type="text" class="form-control" name='usrn' id="floatingPassword" placeholder="Username">
<label for="floatingPassword" name='usrn' id='username-text'>Username</label>
</div>
<div class="form-floating mb-3">
<input type="email" name='email' class="form-control" id="floatingInput" placeholder="name@example.com">
<label for="floatingInput" id='email-text'>Email address</label>
</div>
<div class="md-form mb-3">
<textarea id="form7" class="md-textarea form-control" name='message' rows="10" noresize </textarea>
</div>
<button class="w-100 btn btn-lg btn-text btn-outline-dark mb-2" name='subcon' type="submit">Sign in</button>
</form>
</main>
</div>
db.php:
$server = 'localhost';
$user = 'root';
$pass = '';
$database = 'ipapp';
$conn = mysqli_connect($server,$user,$pass,$database);
if(!$conn){
echo '<script>alert("Connection to the server has failed!");</script>';
}
function.php:
<?php include "db.php"; ?>
<?php
/*
========================|
==> Requires & Use <==|
========================|
*/
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
require '../third-party/vendor/autoload.php';
/*
========================|
=> Classes <==|
========================|
*/
class SendData{
public function SendEmails(){
/*
========================|
==> PUBLIC VARIABLES |
========================|
*/
$receiver ='gimmy.none@gmail.com';
$sender = $_POST['email'];
$username = $_POST['usrn'];
$mail = new PHPMailer(true);
try {
$mail->SMTPDebug = 1;
$mail->isSMTP();
$mail->Host = 'smtp.googlemail.com';
$mail->SMTPAuth = true;
$mail->Username = '';
$mail->Password = 'APP_PASSWORD';
$mail->SMTPSecure = "tls";
$mail->Port = 587;
$mail->setFrom($sender);
$mail->addAddress($receiver); //Who is going to receive it
//Content
$mail->isHTML(true); //Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
}
}
?>
I've been trying to solve it on my own, but i guess i need help.
You're trying to spoof/forge the email sender, and gmail is just going to ignore you. Just don't do that. Follow the examples provided with PHPMailer: use your own address for both from and to addresses, and use the submitter's address as a reply-to address:
$mail->addAddress($receiver);
$mail->setFrom($receiver, $_POST['username']);
$mail->addReplyTo($_POST['email']);
Don't forget to do some error checking around this too.