Simple PHP email subscription form with email verification and confirmation – ideal for beginers and medium level developers

Simple PHP email subscription form in few minutes? Really?

The answer is “yes”!

For the sake of this tutorial you will need to include Jquery in in your <head> tag.

I will not include the database part in this PHP email subscription form tutorial, I will just show you how to verify the user’s email. The database part is a simple matter of INSERT and UPDATE. If you can’t handle it, just let me know in the comments.

Ok let’s get started and create a simple HTML form.

[code language=”html”]
<form>
Email:<br>
<input type="text" name="email" id="email">
</form>
<br>
<button id="subscribeBtn" type="button">Subscribe!</button>
[/code]

And the javascript part:

[code language=”javascript”] 
$(document).ready(function () {
$("#subscribeBtn").click(function () {
if($(‘#email’).val()){
$.ajax({
type: ‘POST’,
url: "sendMail.php", //The file w’re making a POST request to
dataType: ‘html’,
async: true,
data: {
email: $(‘#email’).val()
},
success: function (data) {
alert("PLease check andconfirm your email");
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert(errorThrown);
}
});
}else{
alert("Please, insert an email");
}
});
});
[/code]

Now as you can see we need to create the sendMail.php file, which will send the verification email to the subscriber.
sendMail.php

[code language=”php”]
<?php

$email = filter_input(INPUT_POST, ’email’); //Get the email submited from our ajax form

// HERE YOU HAVE TO INSERT THE EMAIL IN YOUR DATABASE. I suggest just 2 clumns ’email’ and ‘isVerified’ where email = $email and isVerified = false

/* Send the confirmation email */
$to = $email;
$subject = ‘Email confirmation’;
$message = "Hello!" . "rn" . "Please confirm your email by clicking on the link below: " . "rn" . "rn"
. "yourdomain.com/confirmEmail.php?email=" . $email
. "rn" . "rn" . "This is an automated message, please do not respond to it!";

$headers = ‘From: mail@yourdomain.com’ . "rn" .
‘Reply-To: mail@yourdomain.com’ . "rn" .
‘Content-Type: text/plain; charset=UTF-8’ . "rn" .
‘MIME-Version: 1.0’ . "rn" .
‘Content-Transfer-Encoding: quoted-printable’ . "rn";

mail($to, $subject, $message, $headers);
[/code]

The above code will send a verification email to the subscriber and in order to complete this simple PHP email subscription, we have to verify the user when he click on the link. If you take a look at the $message there is a link pointing to confirmEmail.php and it takes one parameter named email, where the value of this parameter is the user’s email adress, so here is how we can verify it when teh user click on our link

[code language=”php”]
<?php

$email = filter_input(INPUT_POST, ’email’);

// UPDATE YOUR DATABASE TABLE AND SET isActive = true WHERE email = $email
[/code]

This will complete the email verification and you will be able to know at least, if the user’s email exists.
Again, if you’re facing problems with the SQL and database part of this simple php email subscription form, feel free to leave a comment below.

10 thoughts on “Simple PHP email subscription form with email verification and confirmation – ideal for beginers and medium level developers”

    1. You need a simple table with 2 columns: “email” – varchar(100) and isVerified – boolean. You can call this table Subscriptions. Once the database is created you just need 2 queries. The first one(when you send the email verification) is “INSERT INTO Subscriptions(email, isVerified) VALUES($email, 0)”;

      Once the email is confirmed you just have to update your Subscriptions table by the following query:
      “UPDATE Subscriptions SET isActive=1 WHERE email = $email”

      There is no need to download something, because it is the easiest solution I was able to think of. As you see around 40 lines of code.

      Feel free to replay, if you’re facing any difficulties!

  1. Interesting! I didn’t know about the filter_input() function, but I’m definitely going to start making use of that. Thanks for posting this article.

    1. You can really add the code anywhere, as we don’t have any dependencies on it – BUT, following the best practice guides, you have to put your code at the end of the page – just before the [/body] tag. In some other case you can use the ‘defer’ tag – using this the browser will download your script once the HTML is loaded.

      example: [script src=”myScript.js” type=”text/javascript” defer=”defer”>[/script]

      Please replace the ‘[]’ with ‘<>‘.

    2. Yes, it is safe. We are following google’s guide. If you check we are making a request to google’s server on our server side. Google is tracking the user actions, so if something seems wrong/modified we will receive an error response.

  2. I kind of get all you explained regarding the php codes even though i ma not an expert,quick question
    1. Can i edit the contents of the email sent to the user if for exmaple i want it to be an html email meant for verification
    2. i dont know anything abt mysql at all can you help ?
    3.Is there a way i can just make the emails populate into a text file .
    Thank you very much

    1. Hi, yes, you can edit the content of the email and place your own HTML, if you wait a bit, I’m planning to update the tutorial with teh SQL code and of course you can populate the emails into a text file – I will try to include a sample in this tutorial, or I will create a separate one. Please, be patient. 🙂

      1. Ok thanks,i dont mean to sound too “pushy” but i have a project to complete by friday and it has such functionalities i dont knw how fast you will upload the solution,i will be grateful if you can do it soonest,maybe tommorow. thank you so much

  3. Ok thank you,i dont mean to sound too “pushy” but i have a project to complete by friday and it has such functionalities i dont knw how fast you will upload the solution,i will be grateful if you can do it soonest,maybe tommorow. thank you very much

Leave a Comment

Your email address will not be published. Required fields are marked *