Should I use Php Mailer to create a contact form?

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.

PHPMailer is one option. Since the form is just going to you, does it need to email to you? Would just storing it in a database function better for you, and a list when you login to your site?

Every mail instance I create now, uses MailGun. 10k emails per month for free; guaranteed delivery; all done with an API call.

Thanks astonecipher for the reply, and sorry for my late comeback. Yeah, I’d like the email to go to me (an address I specify). I’ve used the basic (and often criticized) php mail() function for the moment, which seems to the job. I’ll take a look at MailGun tough.

Thanks.

More often than not sending mail with mail() with either fail to make it to your inbox at all or go straight to junk mail. What works most reliably for me is having an SMTP account on a real mail server (you can use a gmail address) and configuring PHPMailer to send mail through that.

I’ve actually been working on a beginner-friendly example contact form: https://github.com/thinsoldier/php-beginner-form

Try it out if you have the time and give feedback.

Sponsor our Newsletter | Privacy Policy | Terms of Service