Thranduil Thranduil - 3 years ago 272
HTML Question

Add phpmailer to HTML form

I add a phpmailer with SMTP to my site. I made a phpmailer file, and do all the necessary coding what was needed. Now i need to know how to correctly add my phpmiler.php to my index.html so i can use my email form.

my phpmailer.php

<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require 'assets/PHPMailer/src/Exception.php';
require 'assets/PHPMailer/src/PHPMailer.php';
require 'assets/PHPMailer/src/SMTP.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 = 'test'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'test'; // SMTP username
$mail->Password = 'test'; // SMTP password
$mail->SMTPSecure = 'ssl'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 465; // TCP port to connect to

//Recipients
$mail->setFrom('test');
$mail->addAddress('test'); // Add a recipient
$mail->addReplyTo('test', 'Information');
$mail->addCC('cc@example.com');
$mail->addBCC('bcc@example.com');



//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->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
}
?>


I tested php file , all works , i use TEST in personal information fields.

My HTML form

<div class="block">
<h2>Send message</h2>
<form>
<div class="form-group">
<input type="text" name="mail" class="form-control" placeholder="Email Address">
</div>
<div class="form-group">
<input type="text" name="subject" class="form-control" placeholder="Subject">
</div>
<div class="form-group">
<textarea class="form-control" name="text" rows="3" placeholder="Your Message"></textarea>
<button class="btn btn-default" type="submit" >Send Message</button>
</div>
</form>
</div>


So how to correctly add that it takes email, text and subject into my phpmailer.php and send the message throw HTML form.
Thanks and sorry for my bad language, not my native.

Answer Source

First you need change <form> to

<form action="/maybepath/phpmailer.php" method="POST" >

Second you need change phpmailer.php to get data from $_POST and put it in proper place. Try var_dump($_POST); to see what you get.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download