Contact Form

Contact Form

Server Side Sundays – Bullet Proof Contact Form

Blog thread for backend issues!

Welcome to Server Side Sundays, a blog thread focused on solutions to issues that arise while developing backend applications and/or tips for server side projects.

For our first blog, let’s talk about developing your our own PHP contact forms. The issue that we are going to be covering is an authentication failure [SMTP: Invalid response code received from server (code: 534, response: 5.7.14)] while setting up a SMTP (Simple Mail Transfer Protocol) using PEAR mail package.

At first, we were using PHP’s function mail(). However, this function sends an email directly from the web server that your site is being hosted on. Depending on where the email is going, it might be be blocked if the server ip your site is hosted on has been blacklisted or you change the ‘From’ tag within the headers. For gmail, you won’t run into any problems but if a recipient uses outlook (hotmail, live, etc.) it will be blocked, which is what we saw.

First things first, you need a form within your html file. This should include a name, email address, phone number, subject and question or message.

Here is the codepen link for a simple and unstyled form contact form. It is important to include some sort of antispam field. We did this by hidding an input that a bot would alter. In the contact form, we check for this.

Next, the fun stuff. So in order for our form to do anything. We have to include a php file entitled contactForm.php in the same directory as the html. By assigning an action to the form (action=“contactForm.php”) we are affectively going to post to that file.

To set up the mailing function you have two steps:

  • Install Pear on your server Pear Mail.
  • Set up the post request. Below is the code for a standard form that cleans the strings, validates our antispam flag, and send an email with Pear Mail function.

Finally, you have to allow access to your gmail account. If not, you will get the authentication failure [SMTP: Invalid response code received from server (code: 534, response: 5.7.14)] .

  • Allow access to your google account. Just follow this link. Select okay and wait 10 minutes to activate.
  • Turn on less secure app access follow this link.
  • If the link in the above step does not allow you to update the setting. You have to sign into your gsuites admin panel with your domain and change the security settings. This function is disabled if you purchased the domain through a third party source. Just follow this link.
  • If the error persists, we had to change the password to the gmail address. DON’T FORGET to change this in the php file.

There you go, you should at this point have a fully functioning contact form! Let us know if there are any topics that you would like covered or question you have.

Cheers