I want to make contact form work/How Do I make php work on a website ?

A

Anonymous

Guest
Hello!
Im a beginner just registered here. I know a little bit of css/html/javascript, have 0 understanding in using php on a website, nevertheless I understand what it is for and that it works with the server mails and databases.
Made a website not a long ago. created contact form which looks like this:
"<div class="container">

<div class="login">
<input type="text" placeholder="name" class="input">
<input type="text" placeholder="e-mail" class="input">
</div>

<div class="subject">
<input type="text" placeholder="Subject" class="input">
</div>

<div class="msg">
<textarea class="area" placeholder="Your Text"></textarea>
</div>

<div class="btni">Send</div> // this is a send button
</div>"
I urgently want to make this form send input text to Gmail mail, but don't know how. Nor I know how I make the php code work on a webpage, as it doesn't go in index.html and if I have something.php file I have no idea how I make a reference. I have no working php code for this contact form to send mails to Gmail. I looked up on youtube on other forums, asked my webdevelopment-knowing friends they dont know as non of them works with php.
I'm asking for help if you have very little time and give me directions on this. Please Please Please! I really need help here.
I uploaded the files on the free hosting of 0fees.us it all works, only php code for sending mails is missing.
 
Lookup PHPMailer on github, it's an easy wrapper with many examples.
 
Thanks a lot I tried it out. with only php mailer it worked, it sent message through Gmail SMTP with 0 problem. but when I link it with HTML it gives me error : (
here is the code, it gives me error ""Message could not be sent. Mailer Error: Invalid address: (to):"
Any ideas? : (
html--->
<form method="post" action="send.php">
<div class="login">
<input type="text" name="name" placeholder="სახელი" class="input">
<input name="email" type="email" placeholder="e-mail მისამართი" class="input">
</div>

<div class="msg">
<textarea name="message" class="area" placeholder="ტექსტი..."></textarea>
</div>
<div class="btni"><button class="btni1">გაგზავნა</button> </div>
</div>
</form


php ---->
<?php
$name = $_POST['name'];
$maili = $_POST['email'];
$message = $_POST['message'];



// Import PHPMailer classes into the global namespace
// These must be at the top of your scr1pt, not inside a functi0n
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

//Load Composer's autoloader
require 'vendor/autoload.php';

$mail = new PHPMailer(true); // Passing `true` enables exceptions
try {
//Server settings
$mail->SMTPDebug = 0; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = '******@gmail.com'; // SMTP username
$mail->Password = '******'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to

//Recipients
$mail->setFrom('******@gmail.com','gmail');
$mail->addAddress($maili,$name); // Add a recipient
// Name is optional

$body="Hello ".$name."meili:".$message.'reply '.$maili;

//Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Inquiry'.$name;
$mail->Body = $body;
$mail->AltBody = strip_tags($body);

$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}
 
have a look at var_dump($_POST) to see what you get from the form.
 
Create a new PHP file: Create a new file with a .php extension (e.g., contact.php) in the same directory as your HTML file.

Set up the PHP mail function: In the contact.php file, you'll need to write PHP code to handle the form submission and send the email. Below is a simple example to get you started:

php
Copy code
<?php
if(isset($_POST['submit'])){
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];

$to = 'abc@gmail.com'; //
$headers = "From: $email\r\n";
$headers .= "Reply-To: $email\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";

$body = "Name: $name <br>Email: $email <br>Subject: $subject <br>Message: $message";

if(mail($to, $subject, $body, $headers)){
echo "Email sent successfully!";
} else {
echo "Sorry, there was an error sending your message.";
}
}
?>
Update the HTML form: In your HTML file, add the action attribute to the <form> tag and set it to the name of your PHP file. For example:
html
Copy code
<form action="contact.php" method="POST">
Upload the PHP file: Upload the contact.php file to your hosting server using FTP or any file manager provided by your hosting service.

Test the contact form: Access your website, fill out the form, and click the "Send" button. If everything is set up correctly, the PHP code will handle the form submission and send an email to your Gmail address.

Remember to replace 'email' in the PHP code with your actual Gmail address where you want to receive the contact form submissions.
Please note that this is a basic example and may require further customization based on your specific requirements. Additionally, make sure to implement necessary security measures to prevent misuse or spam.
 
Create a PHP file with a .php extension, e.g., send_email.php.
Update your HTML form by adding the action attribute to the <form> tag and setting it to the filename of your PHP file. Also, set the method attribute to "POST".
In the PHP file, retrieve the form data using $_POST and assign it to variables.
Use the mail() function in PHP to send the email. Provide the recipient's email address, subject, body, and headers.
Upload both your HTML file and the send_email.php file to your web hosting provider.

Remember to replace "your-email@gmail.com" with your actual Gmail address in the PHP file.

Please note that free hosting services might have limitations on sending emails. You might need to consider alternative email libraries or services like PHPMailer or SendGrid.

I hope this simplified guide helps you!
 
Back
Top