Hello all,
I’m trying to create a basic ‘contact us form’ and, as I have also recently been using PHP Mailer to send emails, I wondered if it makes sense to use PHP Mailer when creating a contact form.
After spending a bit of time trying to configure it to be a contact form which sends to my address, I began to think that PHP Mailer maybe isn’t the best way to do this? I mean I don’t need to send emails out from my gmail account or mail server, rather I am just receiving them. Just a general question really - should I use PHP mailer for a one way (customer to me) contact form? The Mailer code I was messing around with is below - but it’s not working as it should. I’ve tried playing about with the form, but basically the idea is that when the form is submitted, the submitted message goes to [email protected] and the senders email ($email) appears as the senders email address when I view from [email protected].
[php]<?php
include_once “PHPMailer/PHPMailer.php”;
include_once “PHPMailer/Exception.php”;
include_once “PHPMailer/SMTP.php”;
$subject = “”;
$email = “”;
$message = “No Message Sent”;
if (isset($_POST[‘submit’])) {
$subject = $_POST[‘subject’];
$email = $_POST[‘email’];
$message = $_POST[‘message’];
}
use PHPMailer\PHPMailer\PHPMailer;
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPDebug = 3;
$mail->SMTPAuth = true;
$mail->SMTPSecure = ‘ssl’;
$mail->Host = “smtp.gmail.com”;
$mail->Port = 465;
$mail->IsHTML(false);
$mail->Username = ‘[email protected]’;
$mail->Password = ‘XXXX’;
$mail->setFrom(‘[email protected]’);
$mail->Subject = $subject;
$mail->Body = $message;
$mail->AddAddress($email);
if(!$mail->Send()) {
} else {
echo “Message has been sent”;
}
?>[/php]
…Used with the following form:
<form method="post" action="sendemail_basic_Contact_US_form.php"> <!--Sending to this form-->
<input class="form-control" name="subject" placeholder="Your Message Subject"><br>
<input class="form-control" name="email" type="email" placeholder="Your Email Address"><br>
<textarea placeholder="Enter your message" class="form-control" name="message"></textarea><br>
<input class="btn btn-primary" name="submit" type="submit" value="Send Email">
</form>
I understand there are other ways to create contact forms, but also read that php mailer is more reliable than the php mail() function, so wondered if I should be using php Mailer.
Any thoughts / opinions would be appreciated here. Many thanks for reading.