I have a question about some PHP form handling. What I want is to take the data in the form below, and sent the data to my Gmail account. I do not own a mail server. I already discovered that you can not do it with the PHP mail function. I'm trying the PHPmailer out at this moment but it does not seem to have the right results, because it seems that PHPmailer only can be used to fill in a form and sent it to a person with your Gmail address.I want something different i want to let a user mail me to my Gmail address using the form inputs. So what shall I do? setting up a mail server with hmailserver and SquirrelMail or is there a better method.
<form id="form" action="PHPmail2.php" method="POST">
<input type="text" name="firstname"class="first" placeholder="first name"><font color="red">*</font>
<input type="text" name="lastname" class="first" placeholder="lastname"><font color="red">*</font>
<input type="email" name="email" class="first" placeholder="E-mail"><font color="red">*</font>
<br>
<input type="text" name="subject" class=" subject first" placeholder="subject">
<textarea type="text" class="textarea mail first" name="textarea" placeholder="fill somting in"></textarea>
<button type ="submit" id="submitbutton"name="submit">Send</button>
</form>
The PHP code:
<?php
if (isset($_POST['submit'])) {
if (!empty($firstname)) {
header('Location:index.php');
}else{
header('Location: filleErrorHandeling/nietAlleVeldenIngevuld.php');
}
if (!empty($lastname)) {
header('Location:index.php');
}else{
header('Location: filleErrorHandeling/nietAlleVeldenIngevuld.php');
}
if (!empty($from)) {
header('Location:index.php');
}else{
header('Location: filleErrorHandeling/nietAlleVeldenIngevuld.php');
}
}
adding some PHPmailer code all i have to do is link the form action to the file name.
<?php
$to="*";
$from=$_POST['email'];
$firstname=$_POST['firstname'];
$lastname=$_POST['lastname'];
$subject=$_POST['subject'];
$msg=$_POST['textarea'];
$subject= "form submission";
$subject2="Copy of your form submission";
$message= $firstname. " ".$lastname. " Wrote the following: "."\n\n".$msg;
$message2= "Here is a copy of your message ". $firstname ."\n\n". $msg;
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require ("composer/vendor/autoload.php");
$mail= new PHPMailer(true);
try{
$mail->IsSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Port =25;
$mail->Username =$to;
$mail->Password = '*';
$mail->setFrom($email);
$mail->addAddress($email);
$mail->Subject='*';
$mail->Body=$msg;
$mail->send();
}
catch(Exception $e){
echo $e->errorMessage();
}
catch(\Exception $e)
{
echo $e->getMessage();
}
?>
What i basically want is when a user pressed on submit I want an email to my Gmail account with all the input data, or better even a part of it.
Thank you for reading my post if there are any questions regarding this topic let me know.