'How to receive emails from client using PHPMailer instead of sending them in PHP?

I've tried the mail() function in PHP, and it won't work, but I've installed the PHPMailer library using composer, and It worked perfectly while sending emails to myself.

<?php

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

require 'vendor/autoload.php';


$mail = new PHPMailer();


//Server settings
$mail->SMTPDebug = SMTP::DEBUG_SERVER;                      //Enable verbose debug output
$mail->isSMTP();                                            //Send using SMTP
$mail->Host       = 'smtp.gmail.com';                     //Set the SMTP server to send through
$mail->SMTPAuth   = true;                                   //Enable SMTP authentication
$mail->Username   = '[email protected]';                     //SMTP username
$mail->Password   = 'MyPassword';                               //SMTP password
$mail->SMTPSecure = "ssl";                            //Enable implicit TLS encryption
$mail->Port       = 465;                                    //TCP port to connect to; use 587 if you have set `SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS`



// Content
$mail->isHTML(true);
$mail->CharSet = "UTF-8";


//Recipients
$mail->setFrom('[email protected]', '');
$mail->addAddress('[email protected]', '');     //Add a recipient

// Message
$mail->Subject = 'some subject';
$mail->Body    = 'Some Content';

// Sending
$mail->send();
?>

But, actually, my goal was to make people able to send me messages via a contact form on my project's contact page.

Basically, we'll have to provide a password here from the sender? and that's not what I want, I just want to receive messages from users, not to send them.

So, How could I perform this logic?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source