{"id":4289,"date":"2020-02-04T19:27:03","date_gmt":"2020-02-04T19:27:03","guid":{"rendered":"https:\/\/www.codexworld.com\/?p=4289"},"modified":"2025-11-06T14:14:22","modified_gmt":"2025-11-06T14:14:22","slug":"newsletter-email-subscription-with-php-mysql","status":"publish","type":"post","link":"https:\/\/www.codexworld.com\/newsletter-email-subscription-with-php-mysql\/","title":{"rendered":"Email Newsletter Subscription System with PHP &#038; MySQL"},"content":{"rendered":"<p>A <b>newsletter subscription<\/b> is the most used feature on the website or blog. The email subscription functionality provides an option for the visitor to receive updates via email. The email subscription feature is the most important part of email marketing. Most of the website provides an email subscription option that allows the site visitors to get updates daily or weekly newsletter from the website.<\/p>\n<p>An email newsletter subscription system allows website visitors to subscribe using their email addresses. Once subscribed, they receive an email confirmation link to verify their address. This helps you build a genuine, spam-free mailing list for updates, offers, or new articles.<\/p>\n<p>In this tutorial, you\u2019ll learn how to create a complete <b>email newsletter subscription<\/b> system using PHP and MySQL, including database design, form validation, and email verification.<\/p>\n<h2>\ud83d\ude80 Tutorial Overview<\/h2>\n<p>&#8211; Create a MySQL database and table to store subscriber information.<br \/>\n&#8211; Build an HTML subscription form with client-side validation using JavaScript.<br \/>\n&#8211; Implement server-side validation and handle form submissions with PHP.<br \/>\n&#8211; Send verification emails to subscribers using PHP&#8217;s mail function.<br \/>\n&#8211; Verify email addresses through a unique link sent to the subscriber&#8217;s email.<\/p>\n<p><b>Features:<\/b><\/p>\n<ul>\n<li>Responsive subscription form with HTML and CSS.<\/li>\n<li>Client-side validation for name and email using JavaScript.<\/li>\n<li>Server-side validation and sanitization with PHP.<\/li>\n<li>MySQL database integration to store subscriber data.<\/li>\n<li>Email verification system to confirm subscriptions.<\/li>\n<li>AJAX form submission for a seamless user experience.<\/li>\n<li>Secure form handling with validation to prevent SQL injection and ensure data integrity.<\/li>\n<li>User-friendly feedback messages for successful subscriptions and errors.<\/li>\n<\/ul>\n<p><b>Technologies Used:<\/b><\/p>\n<ul>\n<li>HTML5 and CSS3 for the subscription form design.<\/li>\n<li>JavaScript for client-side validation and AJAX requests.<\/li>\n<li>PHP for server-side processing and email handling.<\/li>\n<li>MySQL for database management and storing subscriber information.<\/li>\n<\/ul>\n<h2>\ud83d\udcc1 Folder Structure<\/h2>\n<pre class=\"file-struc\">email_newsletter_subscription<span style=\"color:#794938\">\/<\/span>\r\n\u251c\u2500\u2500 config.php\r\n\u251c\u2500\u2500 db.php\r\n\u251c\u2500\u2500 index.html\r\n\u251c\u2500\u2500 subscribe.php\r\n\u251c\u2500\u2500 verify.php\r\n\u2514\u2500\u2500 css<span style=\"color:#794938\">\/<\/span>\r\n    \u2514\u2500\u2500 style.css\r\n<\/pre>\n<h2>\u2699\ufe0f Step 1: Create the Database and Table<\/h2>\n<p>First, create a MySQL database (e.g., <code>newsletter_db<\/code>) and a table (e.g., <code>subscriptions<\/code>) to store the subscriber information. You can use phpMyAdmin or any MySQL client to run the following SQL commands:<\/p>\n<pre style=\"color: rgb(68, 68, 68);\"><span class=\"hljs-keyword\" style=\"font-weight: 700;\">CREATE<\/span> <span class=\"hljs-keyword\" style=\"font-weight: 700;\">TABLE<\/span> <span class=\"hljs-string\" style=\"color: rgb(136, 0, 0);\">`subscriptions`<\/span> (\r\n  <span class=\"hljs-string\" style=\"color: rgb(136, 0, 0);\">`id`<\/span> <span class=\"hljs-built_in\" style=\"color: rgb(57, 115, 0);\">INT<\/span> <span class=\"hljs-keyword\" style=\"font-weight: 700;\">UNSIGNED<\/span> <span class=\"hljs-keyword\" style=\"font-weight: 700;\">NOT<\/span> <span class=\"hljs-literal\" style=\"color: rgb(120, 169, 96);\">NULL<\/span> AUTO_INCREMENT,\r\n  <span class=\"hljs-string\" style=\"color: rgb(136, 0, 0);\">`name`<\/span> <span class=\"hljs-built_in\" style=\"color: rgb(57, 115, 0);\">VARCHAR<\/span>(<span class=\"hljs-number\" style=\"color: rgb(136, 0, 0);\">255<\/span>) <span class=\"hljs-keyword\" style=\"font-weight: 700;\">NOT<\/span> <span class=\"hljs-literal\" style=\"color: rgb(120, 169, 96);\">NULL<\/span>,\r\n  <span class=\"hljs-string\" style=\"color: rgb(136, 0, 0);\">`email`<\/span> <span class=\"hljs-built_in\" style=\"color: rgb(57, 115, 0);\">VARCHAR<\/span>(<span class=\"hljs-number\" style=\"color: rgb(136, 0, 0);\">255<\/span>) <span class=\"hljs-keyword\" style=\"font-weight: 700;\">NOT<\/span> <span class=\"hljs-literal\" style=\"color: rgb(120, 169, 96);\">NULL<\/span>,\r\n  <span class=\"hljs-string\" style=\"color: rgb(136, 0, 0);\">`token`<\/span> <span class=\"hljs-built_in\" style=\"color: rgb(57, 115, 0);\">VARCHAR<\/span>(<span class=\"hljs-number\" style=\"color: rgb(136, 0, 0);\">64<\/span>) <span class=\"hljs-keyword\" style=\"font-weight: 700;\">DEFAULT<\/span> <span class=\"hljs-literal\" style=\"color: rgb(120, 169, 96);\">NULL<\/span>,\r\n  <span class=\"hljs-string\" style=\"color: rgb(136, 0, 0);\">`is_verified`<\/span> TINYINT(<span class=\"hljs-number\" style=\"color: rgb(136, 0, 0);\">1<\/span>) <span class=\"hljs-keyword\" style=\"font-weight: 700;\">NOT<\/span> <span class=\"hljs-literal\" style=\"color: rgb(120, 169, 96);\">NULL<\/span> <span class=\"hljs-keyword\" style=\"font-weight: 700;\">DEFAULT<\/span> <span class=\"hljs-number\" style=\"color: rgb(136, 0, 0);\">0<\/span>,\r\n  <span class=\"hljs-string\" style=\"color: rgb(136, 0, 0);\">`created_at`<\/span> DATETIME <span class=\"hljs-keyword\" style=\"font-weight: 700;\">DEFAULT<\/span> <span class=\"hljs-keyword\" style=\"font-weight: 700;\">CURRENT_TIMESTAMP<\/span>,\r\n  <span class=\"hljs-string\" style=\"color: rgb(136, 0, 0);\">`verified_at`<\/span> DATETIME <span class=\"hljs-keyword\" style=\"font-weight: 700;\">DEFAULT<\/span> <span class=\"hljs-literal\" style=\"color: rgb(120, 169, 96);\">NULL<\/span>,\r\n  PRIMARY <span class=\"hljs-keyword\" style=\"font-weight: 700;\">KEY<\/span> (<span class=\"hljs-string\" style=\"color: rgb(136, 0, 0);\">`id`<\/span>),\r\n  <span class=\"hljs-keyword\" style=\"font-weight: 700;\">UNIQUE<\/span> <span class=\"hljs-keyword\" style=\"font-weight: 700;\">KEY<\/span> <span class=\"hljs-string\" style=\"color: rgb(136, 0, 0);\">`email_unique`<\/span> (<span class=\"hljs-string\" style=\"color: rgb(136, 0, 0);\">`email`<\/span>)\r\n) <span class=\"hljs-keyword\" style=\"font-weight: 700;\">ENGINE<\/span>=<span class=\"hljs-keyword\" style=\"font-weight: 700;\">InnoDB<\/span> <span class=\"hljs-keyword\" style=\"font-weight: 700;\">DEFAULT<\/span> <span class=\"hljs-keyword\" style=\"font-weight: 700;\">CHARSET<\/span>=utf8mb4;<\/pre>\n<h2>\u2699\ufe0f Step 2: Database &#038; Mail Configuration<\/h2>\n<p>Create a <code>config.php<\/code> file to store your database and mail configuration settings. Update the placeholders with your actual database credentials (db_host, db_user, db_pass, and db_name) and mail settings (mail_from and mail_from_name).<\/p>\n<pre><span style=\"color: #0000BB\">&lt;?php <br \/><\/span><span style=\"color: #FF8000\">\/\/&nbsp;Configuration&nbsp;for&nbsp;database&nbsp;and&nbsp;mail <br \/>\/\/&nbsp;IMPORTANT:&nbsp;Update&nbsp;these&nbsp;values&nbsp;for&nbsp;your&nbsp;environment <br \/><\/span><span style=\"color: #007700\">return&nbsp;[ <br \/>&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #FF8000\">\/\/&nbsp;Database&nbsp;(MySQL)&nbsp;settings <br \/>&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #DD0000\">'db_host'&nbsp;<\/span><span style=\"color: #007700\">=&gt;&nbsp;<\/span><span style=\"color: #DD0000\">'localhost'<\/span><span style=\"color: #007700\">, <br \/>&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #DD0000\">'db_user'&nbsp;<\/span><span style=\"color: #007700\">=&gt;&nbsp;<\/span><span style=\"color: #DD0000\">'root'<\/span><span style=\"color: #007700\">, <br \/>&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #DD0000\">'db_pass'&nbsp;<\/span><span style=\"color: #007700\">=&gt;&nbsp;<\/span><span style=\"color: #DD0000\">'root_pass'<\/span><span style=\"color: #007700\">, <br \/>&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #DD0000\">'db_name'&nbsp;<\/span><span style=\"color: #007700\">=&gt;&nbsp;<\/span><span style=\"color: #DD0000\">'newsletter_db'<\/span><span style=\"color: #007700\">, <br \/> <br \/>&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #FF8000\">\/\/&nbsp;From&nbsp;email&nbsp;used&nbsp;when&nbsp;sending&nbsp;verification&nbsp;email <br \/>&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #DD0000\">'mail_from'&nbsp;<\/span><span style=\"color: #007700\">=&gt;&nbsp;<\/span><span style=\"color: #DD0000\">'no-reply@example.com'<\/span><span style=\"color: #007700\">, <br \/>&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #DD0000\">'mail_from_name'&nbsp;<\/span><span style=\"color: #007700\">=&gt;&nbsp;<\/span><span style=\"color: #DD0000\">'Your&nbsp;Company'<\/span><span style=\"color: #007700\">, <br \/>]; <br \/><\/span><span style=\"color: #0000BB\">?&gt;<\/span><\/pre>\n<h2>\u2699\ufe0f Step 3: Create Database Connection<\/h2>\n<p>Create a <code>db.php<\/code> file to establish a connection to the MySQL database using MySQLi.<\/p>\n<pre><span style=\"color: #0000BB\">&lt;?php <br \/><\/span><span style=\"color: #FF8000\">\/\/&nbsp;Load&nbsp;configuration&nbsp;file&nbsp;and&nbsp;get&nbsp;DB&nbsp;settings <br \/><\/span><span style=\"color: #0000BB\">$config&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;require&nbsp;<\/span><span style=\"color: #0000BB\">__DIR__&nbsp;<\/span><span style=\"color: #007700\">.&nbsp;<\/span><span style=\"color: #DD0000\">'\/config.php'<\/span><span style=\"color: #007700\">; <br \/> <br \/><\/span><span style=\"color: #FF8000\">\/\/&nbsp;Create&nbsp;MySQLi&nbsp;connection <br \/><\/span><span style=\"color: #0000BB\">$mysqli&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;new&nbsp;<\/span><span style=\"color: #0000BB\">mysqli<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$config<\/span><span style=\"color: #007700\">[<\/span><span style=\"color: #DD0000\">'db_host'<\/span><span style=\"color: #007700\">],&nbsp;<\/span><span style=\"color: #0000BB\">$config<\/span><span style=\"color: #007700\">[<\/span><span style=\"color: #DD0000\">'db_user'<\/span><span style=\"color: #007700\">],&nbsp;<\/span><span style=\"color: #0000BB\">$config<\/span><span style=\"color: #007700\">[<\/span><span style=\"color: #DD0000\">'db_pass'<\/span><span style=\"color: #007700\">],&nbsp;<\/span><span style=\"color: #0000BB\">$config<\/span><span style=\"color: #007700\">[<\/span><span style=\"color: #DD0000\">'db_name'<\/span><span style=\"color: #007700\">]); <br \/> <br \/><\/span><span style=\"color: #FF8000\">\/\/&nbsp;Check&nbsp;connection <br \/><\/span><span style=\"color: #007700\">if&nbsp;(<\/span><span style=\"color: #0000BB\">$mysqli<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">connect_errno<\/span><span style=\"color: #007700\">)&nbsp;{ <br \/>&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #FF8000\">\/\/&nbsp;avoid&nbsp;exposing&nbsp;internals&nbsp;to&nbsp;the&nbsp;client;&nbsp;log&nbsp;and&nbsp;return&nbsp;a&nbsp;generic&nbsp;message <br \/>&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">error_log<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #DD0000\">'MySQL&nbsp;connect&nbsp;error:&nbsp;'&nbsp;<\/span><span style=\"color: #007700\">.&nbsp;<\/span><span style=\"color: #0000BB\">$mysqli<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">connect_error<\/span><span style=\"color: #007700\">); <br \/>&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">http_response_code<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">500<\/span><span style=\"color: #007700\">); <br \/>&nbsp;&nbsp;&nbsp;&nbsp;die(<\/span><span style=\"color: #DD0000\">'Database&nbsp;connection&nbsp;failed.'<\/span><span style=\"color: #007700\">); <br \/>} <br \/> <br \/><\/span><span style=\"color: #0000BB\">$mysqli<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">set_charset<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #DD0000\">'utf8mb4'<\/span><span style=\"color: #007700\">); <br \/><\/span><span style=\"color: #0000BB\">?&gt;<\/span><\/pre>\n<h2>\u2699\ufe0f Step 4: Create the Subscription Form<\/h2>\n<p>Create an <code>index.html<\/code> file with the subscription form and client-side validation using JavaScript and AJAX.<\/p>\n<ul>\n<li>Define the HTML structure for the subscription form.<\/li>\n<li>Add input fields for name and email with appropriate labels and placeholders.<\/li>\n<li>Include a section to display feedback messages to the user.<\/li>\n<li>Implement JavaScript to handle form submission, perform client-side validation, and send data to the server-side handler script (<code>subscribe.php<\/code>) using AJAX.<\/li>\n<\/ul>\n<pre style=\"color: rgb(95, 94, 78);\"><span class=\"hljs-comment\" style=\"color: rgb(108, 107, 90);\">&lt;!-- subscription form --&gt;<\/span>\r\n<span class=\"hljs-tag\" style=\"color: rgb(186, 98, 54);\">&lt;<span class=\"hljs-name\" style=\"color: rgb(186, 98, 54);\">form<\/span> <span class=\"hljs-attr\">id<\/span>=<span class=\"hljs-string\" style=\"color: rgb(125, 151, 38);\">\"subscribeForm\"<\/span> <span class=\"hljs-attr\">novalidate<\/span>&gt;<\/span>\r\n    <span class=\"hljs-tag\" style=\"color: rgb(186, 98, 54);\">&lt;<span class=\"hljs-name\" style=\"color: rgb(186, 98, 54);\">div<\/span> <span class=\"hljs-attr\">id<\/span>=<span class=\"hljs-string\" style=\"color: rgb(125, 151, 38);\">\"form-messages\"<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\" style=\"color: rgb(125, 151, 38);\">\"messages\"<\/span> <span class=\"hljs-attr\">aria-live<\/span>=<span class=\"hljs-string\" style=\"color: rgb(125, 151, 38);\">\"polite\"<\/span>&gt;<\/span><span class=\"hljs-tag\" style=\"color: rgb(186, 98, 54);\">&lt;\/<span class=\"hljs-name\" style=\"color: rgb(186, 98, 54);\">div<\/span>&gt;<\/span>\r\n\r\n    <span class=\"hljs-tag\" style=\"color: rgb(186, 98, 54);\">&lt;<span class=\"hljs-name\" style=\"color: rgb(186, 98, 54);\">label<\/span> <span class=\"hljs-attr\">for<\/span>=<span class=\"hljs-string\" style=\"color: rgb(125, 151, 38);\">\"name\"<\/span>&gt;<\/span>Name<span class=\"hljs-tag\" style=\"color: rgb(186, 98, 54);\">&lt;\/<span class=\"hljs-name\" style=\"color: rgb(186, 98, 54);\">label<\/span>&gt;<\/span>\r\n    <span class=\"hljs-tag\" style=\"color: rgb(186, 98, 54);\">&lt;<span class=\"hljs-name\" style=\"color: rgb(186, 98, 54);\">input<\/span> <span class=\"hljs-attr\">type<\/span>=<span class=\"hljs-string\" style=\"color: rgb(125, 151, 38);\">\"text\"<\/span> <span class=\"hljs-attr\">id<\/span>=<span class=\"hljs-string\" style=\"color: rgb(125, 151, 38);\">\"name\"<\/span> <span class=\"hljs-attr\">name<\/span>=<span class=\"hljs-string\" style=\"color: rgb(125, 151, 38);\">\"name\"<\/span> <span class=\"hljs-attr\">placeholder<\/span>=<span class=\"hljs-string\" style=\"color: rgb(125, 151, 38);\">\"Your full name\"<\/span> <span class=\"hljs-attr\">required<\/span>&gt;<\/span>\r\n\r\n    <span class=\"hljs-tag\" style=\"color: rgb(186, 98, 54);\">&lt;<span class=\"hljs-name\" style=\"color: rgb(186, 98, 54);\">label<\/span> <span class=\"hljs-attr\">for<\/span>=<span class=\"hljs-string\" style=\"color: rgb(125, 151, 38);\">\"email\"<\/span>&gt;<\/span>Email<span class=\"hljs-tag\" style=\"color: rgb(186, 98, 54);\">&lt;\/<span class=\"hljs-name\" style=\"color: rgb(186, 98, 54);\">label<\/span>&gt;<\/span>\r\n    <span class=\"hljs-tag\" style=\"color: rgb(186, 98, 54);\">&lt;<span class=\"hljs-name\" style=\"color: rgb(186, 98, 54);\">input<\/span> <span class=\"hljs-attr\">type<\/span>=<span class=\"hljs-string\" style=\"color: rgb(125, 151, 38);\">\"email\"<\/span> <span class=\"hljs-attr\">id<\/span>=<span class=\"hljs-string\" style=\"color: rgb(125, 151, 38);\">\"email\"<\/span> <span class=\"hljs-attr\">name<\/span>=<span class=\"hljs-string\" style=\"color: rgb(125, 151, 38);\">\"email\"<\/span> <span class=\"hljs-attr\">placeholder<\/span>=<span class=\"hljs-string\" style=\"color: rgb(125, 151, 38);\">\"you@example.com\"<\/span> <span class=\"hljs-attr\">required<\/span>&gt;<\/span>\r\n\r\n    <span class=\"hljs-tag\" style=\"color: rgb(186, 98, 54);\">&lt;<span class=\"hljs-name\" style=\"color: rgb(186, 98, 54);\">button<\/span> <span class=\"hljs-attr\">type<\/span>=<span class=\"hljs-string\" style=\"color: rgb(125, 151, 38);\">\"submit\"<\/span> <span class=\"hljs-attr\">id<\/span>=<span class=\"hljs-string\" style=\"color: rgb(125, 151, 38);\">\"submitBtn\"<\/span>&gt;<\/span>Subscribe<span class=\"hljs-tag\" style=\"color: rgb(186, 98, 54);\">&lt;\/<span class=\"hljs-name\" style=\"color: rgb(186, 98, 54);\">button<\/span>&gt;<\/span>\r\n<span class=\"hljs-tag\" style=\"color: rgb(186, 98, 54);\">&lt;\/<span class=\"hljs-name\" style=\"color: rgb(186, 98, 54);\">form<\/span>&gt;<\/span>\r\n\r\n<span style=\"color: rgb(95, 94, 78);\"><span class=\"hljs-comment\" style=\"color: rgb(108, 107, 90);\">&lt;!-- JavaScript and AJAX --&gt;<\/span>\r\n<span class=\"hljs-tag\" style=\"color: rgb(186, 98, 54);\">&lt;<span class=\"hljs-name\" style=\"color: rgb(186, 98, 54);\">script<\/span>&gt;<\/span>\r\n<span style=\"color: rgb(68, 68, 68);\">(<span class=\"hljs-function\"><span class=\"hljs-keyword\" style=\"font-weight: 700;\">function<\/span>(<span class=\"hljs-params\"><\/span>)<\/span>{\r\n    <span class=\"hljs-keyword\" style=\"font-weight: 700;\">const<\/span> form = <span class=\"hljs-built_in\" style=\"color: rgb(57, 115, 0);\">document<\/span>.getElementById(<span class=\"hljs-string\" style=\"color: rgb(136, 0, 0);\">'subscribeForm'<\/span>);\r\n    <span class=\"hljs-keyword\" style=\"font-weight: 700;\">const<\/span> messages = <span class=\"hljs-built_in\" style=\"color: rgb(57, 115, 0);\">document<\/span>.getElementById(<span class=\"hljs-string\" style=\"color: rgb(136, 0, 0);\">'form-messages'<\/span>);\r\n    <span class=\"hljs-keyword\" style=\"font-weight: 700;\">const<\/span> submitBtn = <span class=\"hljs-built_in\" style=\"color: rgb(57, 115, 0);\">document<\/span>.getElementById(<span class=\"hljs-string\" style=\"color: rgb(136, 0, 0);\">'submitBtn'<\/span>);\r\n\r\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\" style=\"font-weight: 700;\">function<\/span> <span class=\"hljs-title\" style=\"color: rgb(136, 0, 0); font-weight: 700;\">showMessage<\/span>(<span class=\"hljs-params\">html, type='info'<\/span>)<\/span>{\r\n        messages.innerHTML = <span class=\"hljs-string\" style=\"color: rgb(136, 0, 0);\">`&lt;div class=\"msg <span class=\"hljs-subst\" style=\"color: rgb(68, 68, 68);\">${type}<\/span>\"&gt;<span class=\"hljs-subst\" style=\"color: rgb(68, 68, 68);\">${html}<\/span>&lt;\/div&gt;`<\/span>;\r\n    }\r\n\r\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\" style=\"font-weight: 700;\">function<\/span> <span class=\"hljs-title\" style=\"color: rgb(136, 0, 0); font-weight: 700;\">clearMessage<\/span>(<span class=\"hljs-params\"><\/span>)<\/span>{ messages.innerHTML = <span class=\"hljs-string\" style=\"color: rgb(136, 0, 0);\">''<\/span>; }\r\n\r\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\" style=\"font-weight: 700;\">function<\/span> <span class=\"hljs-title\" style=\"color: rgb(136, 0, 0); font-weight: 700;\">validateClient<\/span>(<span class=\"hljs-params\">name, email<\/span>)<\/span>{\r\n        <span class=\"hljs-keyword\" style=\"font-weight: 700;\">const<\/span> errors = [];\r\n        <span class=\"hljs-keyword\" style=\"font-weight: 700;\">const<\/span> n = name.trim();\r\n        <span class=\"hljs-keyword\" style=\"font-weight: 700;\">const<\/span> e = email.trim();\r\n        <span class=\"hljs-keyword\" style=\"font-weight: 700;\">if<\/span>(n.length &lt; <span class=\"hljs-number\" style=\"color: rgb(136, 0, 0);\">2<\/span>) errors.push(<span class=\"hljs-string\" style=\"color: rgb(136, 0, 0);\">'Name must be at least 2 characters.'<\/span>);\r\n        <span class=\"hljs-keyword\" style=\"font-weight: 700;\">const<\/span> emailRegex = <span class=\"hljs-regexp\" style=\"color: rgb(188, 96, 96);\">\/^[^\\s@]+@[^\\s@]+\\.[^\\s@]+$\/<\/span>;\r\n        <span class=\"hljs-keyword\" style=\"font-weight: 700;\">if<\/span>(!emailRegex.test(e)) errors.push(<span class=\"hljs-string\" style=\"color: rgb(136, 0, 0);\">'Please enter a valid email address.'<\/span>);\r\n        <span class=\"hljs-keyword\" style=\"font-weight: 700;\">return<\/span> errors;\r\n    }\r\n\r\n    form.addEventListener(<span class=\"hljs-string\" style=\"color: rgb(136, 0, 0);\">'submit'<\/span>, <span class=\"hljs-keyword\" style=\"font-weight: 700;\">async<\/span> (ev) =&gt; {\r\n        ev.preventDefault();\r\n        clearMessage();\r\n        submitBtn.disabled = <span class=\"hljs-literal\" style=\"color: rgb(120, 169, 96);\">true<\/span>;\r\n\r\n        <span class=\"hljs-keyword\" style=\"font-weight: 700;\">const<\/span> name = form.name.value || <span class=\"hljs-string\" style=\"color: rgb(136, 0, 0);\">''<\/span>;\r\n        <span class=\"hljs-keyword\" style=\"font-weight: 700;\">const<\/span> email = form.email.value || <span class=\"hljs-string\" style=\"color: rgb(136, 0, 0);\">''<\/span>;\r\n\r\n        <span class=\"hljs-keyword\" style=\"font-weight: 700;\">const<\/span> errors = validateClient(name, email);\r\n        <span class=\"hljs-keyword\" style=\"font-weight: 700;\">if<\/span>(errors.length){\r\n            showMessage(errors.map(e =&gt; <span class=\"hljs-string\" style=\"color: rgb(136, 0, 0);\">`&lt;div&gt;<span class=\"hljs-subst\" style=\"color: rgb(68, 68, 68);\">${e}<\/span>&lt;\/div&gt;`<\/span>).join(<span class=\"hljs-string\" style=\"color: rgb(136, 0, 0);\">''<\/span>), <span class=\"hljs-string\" style=\"color: rgb(136, 0, 0);\">'error'<\/span>);\r\n            submitBtn.disabled = <span class=\"hljs-literal\" style=\"color: rgb(120, 169, 96);\">false<\/span>;\r\n            <span class=\"hljs-keyword\" style=\"font-weight: 700;\">return<\/span>;\r\n        }\r\n\r\n        <span class=\"hljs-keyword\" style=\"font-weight: 700;\">const<\/span> data = <span class=\"hljs-keyword\" style=\"font-weight: 700;\">new<\/span> FormData();\r\n        data.append(<span class=\"hljs-string\" style=\"color: rgb(136, 0, 0);\">'name'<\/span>, name);\r\n        data.append(<span class=\"hljs-string\" style=\"color: rgb(136, 0, 0);\">'email'<\/span>, email);\r\n\r\n        <span class=\"hljs-keyword\" style=\"font-weight: 700;\">try<\/span>{\r\n            <span class=\"hljs-keyword\" style=\"font-weight: 700;\">const<\/span> res = <span class=\"hljs-keyword\" style=\"font-weight: 700;\">await<\/span> fetch(<span class=\"hljs-string\" style=\"color: rgb(136, 0, 0);\">'subscribe.php'<\/span>, { method: <span class=\"hljs-string\" style=\"color: rgb(136, 0, 0);\">'POST'<\/span>, body: data });\r\n            <span class=\"hljs-keyword\" style=\"font-weight: 700;\">const<\/span> json = <span class=\"hljs-keyword\" style=\"font-weight: 700;\">await<\/span> res.json();\r\n            <span class=\"hljs-keyword\" style=\"font-weight: 700;\">if<\/span>(json.success){\r\n                showMessage(json.message, <span class=\"hljs-string\" style=\"color: rgb(136, 0, 0);\">'success'<\/span>);\r\n                form.reset();\r\n            } <span class=\"hljs-keyword\" style=\"font-weight: 700;\">else<\/span> {\r\n                <span class=\"hljs-comment\" style=\"color: rgb(136, 136, 136);\">\/\/ validation or server errors (array or string)<\/span>\r\n                <span class=\"hljs-keyword\" style=\"font-weight: 700;\">if<\/span>(json.errors &amp;&amp; <span class=\"hljs-built_in\" style=\"color: rgb(57, 115, 0);\">Array<\/span>.isArray(json.errors)){\r\n                    showMessage(json.errors.map(e =&gt; <span class=\"hljs-string\" style=\"color: rgb(136, 0, 0);\">`&lt;div&gt;<span class=\"hljs-subst\" style=\"color: rgb(68, 68, 68);\">${e}<\/span>&lt;\/div&gt;`<\/span>).join(<span class=\"hljs-string\" style=\"color: rgb(136, 0, 0);\">''<\/span>), <span class=\"hljs-string\" style=\"color: rgb(136, 0, 0);\">'error'<\/span>);\r\n                } <span class=\"hljs-keyword\" style=\"font-weight: 700;\">else<\/span> <span class=\"hljs-keyword\" style=\"font-weight: 700;\">if<\/span>(json.message){\r\n                    showMessage(json.message, <span class=\"hljs-string\" style=\"color: rgb(136, 0, 0);\">'error'<\/span>);\r\n                } <span class=\"hljs-keyword\" style=\"font-weight: 700;\">else<\/span> {\r\n                    showMessage(<span class=\"hljs-string\" style=\"color: rgb(136, 0, 0);\">'An unexpected error occurred.'<\/span>, <span class=\"hljs-string\" style=\"color: rgb(136, 0, 0);\">'error'<\/span>);\r\n                }\r\n            }\r\n        } <span class=\"hljs-keyword\" style=\"font-weight: 700;\">catch<\/span>(err){\r\n            showMessage(<span class=\"hljs-string\" style=\"color: rgb(136, 0, 0);\">'Network error. Please try again later.'<\/span>, <span class=\"hljs-string\" style=\"color: rgb(136, 0, 0);\">'error'<\/span>);\r\n        } <span class=\"hljs-keyword\" style=\"font-weight: 700;\">finally<\/span> {\r\n            submitBtn.disabled = <span class=\"hljs-literal\" style=\"color: rgb(120, 169, 96);\">false<\/span>;\r\n        }\r\n    });\r\n})();<\/span>\r\n<span class=\"hljs-tag\" style=\"color: rgb(186, 98, 54);\">&lt;\/<span class=\"hljs-name\" style=\"color: rgb(186, 98, 54);\">script<\/span>&gt;<\/span><\/span><\/pre>\n<h2>\u2699\ufe0f Step 5: Handle Subscription Request and Send Verification Email<\/h2>\n<p>Create a <code>subscribe.php<\/code> file to handle the form submission, perform server-side validation, store subscriber data in the database (MySQL), and send a verification email.<\/p>\n<ul>\n<li>Load the configuration and database connection files.<\/li>\n<li>Retrieve and sanitize the form data (name and email).<\/li>\n<li>Perform server-side validation to ensure the name and email are valid.<\/li>\n<li>Check if the email is already subscribed.<\/li>\n<li>If not subscribed, generate a unique verification token and store the subscriber data in the database.<\/li>\n<li>Send a verification email to the subscriber with a unique link containing the token (<code>verify.php<\/code>) using PHP mail() function.<\/li>\n<li>Return a JSON response indicating success or failure.<\/li>\n<\/ul>\n<pre><span style=\"color: #0000BB\">&lt;?php <br \/><\/span><span style=\"color: #FF8000\">\/\/&nbsp;set&nbsp;response&nbsp;type&nbsp;to&nbsp;JSON <br \/><\/span><span style=\"color: #0000BB\">header<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #DD0000\">'Content-Type:&nbsp;application\/json;&nbsp;charset=utf-8'<\/span><span style=\"color: #007700\">); <br \/> <br \/><\/span><span style=\"color: #FF8000\">\/\/&nbsp;only&nbsp;accept&nbsp;POST&nbsp;requests <br \/><\/span><span style=\"color: #007700\">if&nbsp;(<\/span><span style=\"color: #0000BB\">$_SERVER<\/span><span style=\"color: #007700\">[<\/span><span style=\"color: #DD0000\">'REQUEST_METHOD'<\/span><span style=\"color: #007700\">]&nbsp;!==&nbsp;<\/span><span style=\"color: #DD0000\">'POST'<\/span><span style=\"color: #007700\">)&nbsp;{ <br \/>&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">http_response_code<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">405<\/span><span style=\"color: #007700\">); <br \/>&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;<\/span><span style=\"color: #0000BB\">json_encode<\/span><span style=\"color: #007700\">([<\/span><span style=\"color: #DD0000\">'success'&nbsp;<\/span><span style=\"color: #007700\">=&gt;&nbsp;<\/span><span style=\"color: #0000BB\">false<\/span><span style=\"color: #007700\">,&nbsp;<\/span><span style=\"color: #DD0000\">'message'&nbsp;<\/span><span style=\"color: #007700\">=&gt;&nbsp;<\/span><span style=\"color: #DD0000\">'Method&nbsp;not&nbsp;allowed'<\/span><span style=\"color: #007700\">]); <br \/>&nbsp;&nbsp;&nbsp;&nbsp;exit; <br \/>} <br \/> <br \/><\/span><span style=\"color: #FF8000\">\/\/&nbsp;load&nbsp;config&nbsp;and&nbsp;database&nbsp;connection <br \/><\/span><span style=\"color: #007700\">require_once&nbsp;<\/span><span style=\"color: #0000BB\">__DIR__&nbsp;<\/span><span style=\"color: #007700\">.&nbsp;<\/span><span style=\"color: #DD0000\">'\/db.php'<\/span><span style=\"color: #007700\">; <br \/> <br \/><\/span><span style=\"color: #FF8000\">\/\/&nbsp;get&nbsp;input&nbsp;from&nbsp;POST&nbsp;data <br \/><\/span><span style=\"color: #0000BB\">$name&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;isset(<\/span><span style=\"color: #0000BB\">$_POST<\/span><span style=\"color: #007700\">[<\/span><span style=\"color: #DD0000\">'name'<\/span><span style=\"color: #007700\">])&nbsp;?&nbsp;<\/span><span style=\"color: #0000BB\">trim<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$_POST<\/span><span style=\"color: #007700\">[<\/span><span style=\"color: #DD0000\">'name'<\/span><span style=\"color: #007700\">])&nbsp;:&nbsp;<\/span><span style=\"color: #DD0000\">''<\/span><span style=\"color: #007700\">; <br \/><\/span><span style=\"color: #0000BB\">$email&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;isset(<\/span><span style=\"color: #0000BB\">$_POST<\/span><span style=\"color: #007700\">[<\/span><span style=\"color: #DD0000\">'email'<\/span><span style=\"color: #007700\">])&nbsp;?&nbsp;<\/span><span style=\"color: #0000BB\">trim<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$_POST<\/span><span style=\"color: #007700\">[<\/span><span style=\"color: #DD0000\">'email'<\/span><span style=\"color: #007700\">])&nbsp;:&nbsp;<\/span><span style=\"color: #DD0000\">''<\/span><span style=\"color: #007700\">; <br \/> <br \/><\/span><span style=\"color: #0000BB\">$errors&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;[]; <br \/><\/span><span style=\"color: #FF8000\">\/\/&nbsp;server-side&nbsp;validation <br \/><\/span><span style=\"color: #007700\">if&nbsp;(<\/span><span style=\"color: #0000BB\">mb_strlen<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$name<\/span><span style=\"color: #007700\">)&nbsp;&lt;&nbsp;<\/span><span style=\"color: #0000BB\">2<\/span><span style=\"color: #007700\">)&nbsp;{ <br \/>&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">$errors<\/span><span style=\"color: #007700\">[]&nbsp;=&nbsp;<\/span><span style=\"color: #DD0000\">'Name&nbsp;must&nbsp;be&nbsp;at&nbsp;least&nbsp;2&nbsp;characters.'<\/span><span style=\"color: #007700\">; <br \/>} <br \/>if&nbsp;(!<\/span><span style=\"color: #0000BB\">filter_var<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$email<\/span><span style=\"color: #007700\">,&nbsp;<\/span><span style=\"color: #0000BB\">FILTER_VALIDATE_EMAIL<\/span><span style=\"color: #007700\">))&nbsp;{ <br \/>&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">$errors<\/span><span style=\"color: #007700\">[]&nbsp;=&nbsp;<\/span><span style=\"color: #DD0000\">'Please&nbsp;provide&nbsp;a&nbsp;valid&nbsp;email&nbsp;address.'<\/span><span style=\"color: #007700\">; <br \/>} <br \/> <br \/><\/span><span style=\"color: #FF8000\">\/\/&nbsp;if&nbsp;validation&nbsp;errors,&nbsp;return <br \/><\/span><span style=\"color: #007700\">if&nbsp;(!empty(<\/span><span style=\"color: #0000BB\">$errors<\/span><span style=\"color: #007700\">))&nbsp;{ <br \/>&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;<\/span><span style=\"color: #0000BB\">json_encode<\/span><span style=\"color: #007700\">([<\/span><span style=\"color: #DD0000\">'success'&nbsp;<\/span><span style=\"color: #007700\">=&gt;&nbsp;<\/span><span style=\"color: #0000BB\">false<\/span><span style=\"color: #007700\">,&nbsp;<\/span><span style=\"color: #DD0000\">'errors'&nbsp;<\/span><span style=\"color: #007700\">=&gt;&nbsp;<\/span><span style=\"color: #0000BB\">$errors<\/span><span style=\"color: #007700\">]); <br \/>&nbsp;&nbsp;&nbsp;&nbsp;exit; <br \/>} <br \/> <br \/><\/span><span style=\"color: #FF8000\">\/\/&nbsp;sanitize&nbsp;for&nbsp;storage <br \/><\/span><span style=\"color: #0000BB\">$name_for_db&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">mb_substr<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">strip_tags<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$name<\/span><span style=\"color: #007700\">),&nbsp;<\/span><span style=\"color: #0000BB\">0<\/span><span style=\"color: #007700\">,&nbsp;<\/span><span style=\"color: #0000BB\">255<\/span><span style=\"color: #007700\">); <br \/><\/span><span style=\"color: #0000BB\">$email_for_db&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">mb_substr<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$email<\/span><span style=\"color: #007700\">,&nbsp;<\/span><span style=\"color: #0000BB\">0<\/span><span style=\"color: #007700\">,&nbsp;<\/span><span style=\"color: #0000BB\">255<\/span><span style=\"color: #007700\">); <br \/> <br \/><\/span><span style=\"color: #FF8000\">\/\/&nbsp;check&nbsp;if&nbsp;email&nbsp;already&nbsp;exists <br \/><\/span><span style=\"color: #0000BB\">$stmt&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">$mysqli<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">prepare<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #DD0000\">'SELECT&nbsp;id,&nbsp;is_verified&nbsp;FROM&nbsp;subscriptions&nbsp;WHERE&nbsp;email&nbsp;=&nbsp;?&nbsp;LIMIT&nbsp;1'<\/span><span style=\"color: #007700\">); <br \/>if&nbsp;(!<\/span><span style=\"color: #0000BB\">$stmt<\/span><span style=\"color: #007700\">)&nbsp;{ <br \/>&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">error_log<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #DD0000\">'Prepare&nbsp;failed:&nbsp;'&nbsp;<\/span><span style=\"color: #007700\">.&nbsp;<\/span><span style=\"color: #0000BB\">$mysqli<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">error<\/span><span style=\"color: #007700\">); <br \/>&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;<\/span><span style=\"color: #0000BB\">json_encode<\/span><span style=\"color: #007700\">([<\/span><span style=\"color: #DD0000\">'success'&nbsp;<\/span><span style=\"color: #007700\">=&gt;&nbsp;<\/span><span style=\"color: #0000BB\">false<\/span><span style=\"color: #007700\">,&nbsp;<\/span><span style=\"color: #DD0000\">'message'&nbsp;<\/span><span style=\"color: #007700\">=&gt;&nbsp;<\/span><span style=\"color: #DD0000\">'Server&nbsp;error.'<\/span><span style=\"color: #007700\">]); <br \/>&nbsp;&nbsp;&nbsp;&nbsp;exit; <br \/>} <br \/><\/span><span style=\"color: #0000BB\">$stmt<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">bind_param<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #DD0000\">'s'<\/span><span style=\"color: #007700\">,&nbsp;<\/span><span style=\"color: #0000BB\">$email_for_db<\/span><span style=\"color: #007700\">); <br \/><\/span><span style=\"color: #0000BB\">$stmt<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">execute<\/span><span style=\"color: #007700\">(); <br \/><\/span><span style=\"color: #0000BB\">$stmt<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">bind_result<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$existing_id<\/span><span style=\"color: #007700\">,&nbsp;<\/span><span style=\"color: #0000BB\">$existing_verified<\/span><span style=\"color: #007700\">); <br \/>if&nbsp;(<\/span><span style=\"color: #0000BB\">$stmt<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">fetch<\/span><span style=\"color: #007700\">())&nbsp;{ <br \/>&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">$stmt<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">close<\/span><span style=\"color: #007700\">(); <br \/>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(<\/span><span style=\"color: #0000BB\">$existing_verified&nbsp;<\/span><span style=\"color: #007700\">==&nbsp;<\/span><span style=\"color: #0000BB\">1<\/span><span style=\"color: #007700\">)&nbsp;{ <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;<\/span><span style=\"color: #0000BB\">json_encode<\/span><span style=\"color: #007700\">([<\/span><span style=\"color: #DD0000\">'success'&nbsp;<\/span><span style=\"color: #007700\">=&gt;&nbsp;<\/span><span style=\"color: #0000BB\">false<\/span><span style=\"color: #007700\">,&nbsp;<\/span><span style=\"color: #DD0000\">'message'&nbsp;<\/span><span style=\"color: #007700\">=&gt;&nbsp;<\/span><span style=\"color: #DD0000\">'This&nbsp;email&nbsp;is&nbsp;already&nbsp;subscribed&nbsp;and&nbsp;verified.'<\/span><span style=\"color: #007700\">]); <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;} <br \/>&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #FF8000\">\/\/&nbsp;existing&nbsp;but&nbsp;not&nbsp;verified:&nbsp;we&nbsp;will&nbsp;re-send&nbsp;verification <br \/><\/span><span style=\"color: #007700\">}&nbsp;else&nbsp;{ <br \/>&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">$stmt<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">close<\/span><span style=\"color: #007700\">(); <br \/>} <br \/> <br \/><\/span><span style=\"color: #FF8000\">\/\/&nbsp;create&nbsp;or&nbsp;update&nbsp;token <br \/><\/span><span style=\"color: #007700\">try&nbsp;{ <br \/>&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">$token&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">bin2hex<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">random_bytes<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">16<\/span><span style=\"color: #007700\">)); <br \/>}&nbsp;catch&nbsp;(<\/span><span style=\"color: #0000BB\">Exception&nbsp;$e<\/span><span style=\"color: #007700\">)&nbsp;{ <br \/>&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">$token&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">bin2hex<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">openssl_random_pseudo_bytes<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">16<\/span><span style=\"color: #007700\">)); <br \/>} <br \/> <br \/><\/span><span style=\"color: #FF8000\">\/\/&nbsp;insert&nbsp;or&nbsp;update&nbsp;subscription <br \/><\/span><span style=\"color: #007700\">if&nbsp;(isset(<\/span><span style=\"color: #0000BB\">$existing_id<\/span><span style=\"color: #007700\">)&nbsp;&amp;&amp;&nbsp;<\/span><span style=\"color: #0000BB\">$existing_id<\/span><span style=\"color: #007700\">)&nbsp;{ <br \/>&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #FF8000\">\/\/&nbsp;update&nbsp;token&nbsp;and&nbsp;name <br \/>&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">$up&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">$mysqli<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">prepare<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #DD0000\">'UPDATE&nbsp;subscriptions&nbsp;SET&nbsp;name&nbsp;=&nbsp;?,&nbsp;token&nbsp;=&nbsp;?,&nbsp;created_at&nbsp;=&nbsp;NOW()&nbsp;WHERE&nbsp;id&nbsp;=&nbsp;?'<\/span><span style=\"color: #007700\">); <br \/>&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">$up<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">bind_param<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #DD0000\">'ssi'<\/span><span style=\"color: #007700\">,&nbsp;<\/span><span style=\"color: #0000BB\">$name_for_db<\/span><span style=\"color: #007700\">,&nbsp;<\/span><span style=\"color: #0000BB\">$token<\/span><span style=\"color: #007700\">,&nbsp;<\/span><span style=\"color: #0000BB\">$existing_id<\/span><span style=\"color: #007700\">); <br \/>&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">$ok&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">$up<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">execute<\/span><span style=\"color: #007700\">(); <br \/>&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">$up<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">close<\/span><span style=\"color: #007700\">(); <br \/>}&nbsp;else&nbsp;{ <br \/>&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #FF8000\">\/\/&nbsp;insert&nbsp;new&nbsp;subscription <br \/>&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">$ins&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">$mysqli<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">prepare<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #DD0000\">'INSERT&nbsp;INTO&nbsp;subscriptions&nbsp;(name,&nbsp;email,&nbsp;token,&nbsp;is_verified,&nbsp;created_at)&nbsp;VALUES&nbsp;(?,&nbsp;?,&nbsp;?,&nbsp;0,&nbsp;NOW())'<\/span><span style=\"color: #007700\">); <br \/>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(!<\/span><span style=\"color: #0000BB\">$ins<\/span><span style=\"color: #007700\">)&nbsp;{ <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">error_log<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #DD0000\">'Insert&nbsp;prepare&nbsp;failed:&nbsp;'&nbsp;<\/span><span style=\"color: #007700\">.&nbsp;<\/span><span style=\"color: #0000BB\">$mysqli<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">error<\/span><span style=\"color: #007700\">); <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;<\/span><span style=\"color: #0000BB\">json_encode<\/span><span style=\"color: #007700\">([<\/span><span style=\"color: #DD0000\">'success'&nbsp;<\/span><span style=\"color: #007700\">=&gt;&nbsp;<\/span><span style=\"color: #0000BB\">false<\/span><span style=\"color: #007700\">,&nbsp;<\/span><span style=\"color: #DD0000\">'message'&nbsp;<\/span><span style=\"color: #007700\">=&gt;&nbsp;<\/span><span style=\"color: #DD0000\">'Server&nbsp;error.'<\/span><span style=\"color: #007700\">]); <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;} <br \/>&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">$ins<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">bind_param<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #DD0000\">'sss'<\/span><span style=\"color: #007700\">,&nbsp;<\/span><span style=\"color: #0000BB\">$name_for_db<\/span><span style=\"color: #007700\">,&nbsp;<\/span><span style=\"color: #0000BB\">$email_for_db<\/span><span style=\"color: #007700\">,&nbsp;<\/span><span style=\"color: #0000BB\">$token<\/span><span style=\"color: #007700\">); <br \/>&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">$ok&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">$ins<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">execute<\/span><span style=\"color: #007700\">(); <br \/>&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">$ins<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">close<\/span><span style=\"color: #007700\">(); <br \/>} <br \/> <br \/><\/span><span style=\"color: #FF8000\">\/\/&nbsp;check&nbsp;if&nbsp;DB&nbsp;operation&nbsp;was&nbsp;successful <br \/><\/span><span style=\"color: #007700\">if&nbsp;(empty(<\/span><span style=\"color: #0000BB\">$ok<\/span><span style=\"color: #007700\">))&nbsp;{ <br \/>&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">error_log<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #DD0000\">'Insert\/update&nbsp;failed:&nbsp;'&nbsp;<\/span><span style=\"color: #007700\">.&nbsp;<\/span><span style=\"color: #0000BB\">$mysqli<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">error<\/span><span style=\"color: #007700\">); <br \/>&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;<\/span><span style=\"color: #0000BB\">json_encode<\/span><span style=\"color: #007700\">([<\/span><span style=\"color: #DD0000\">'success'&nbsp;<\/span><span style=\"color: #007700\">=&gt;&nbsp;<\/span><span style=\"color: #0000BB\">false<\/span><span style=\"color: #007700\">,&nbsp;<\/span><span style=\"color: #DD0000\">'message'&nbsp;<\/span><span style=\"color: #007700\">=&gt;&nbsp;<\/span><span style=\"color: #DD0000\">'Could&nbsp;not&nbsp;save&nbsp;subscription.'<\/span><span style=\"color: #007700\">]); <br \/>&nbsp;&nbsp;&nbsp;&nbsp;exit; <br \/>} <br \/> <br \/><\/span><span style=\"color: #FF8000\">\/\/&nbsp;build&nbsp;verification&nbsp;link <br \/><\/span><span style=\"color: #0000BB\">$protocol&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;(!empty(<\/span><span style=\"color: #0000BB\">$_SERVER<\/span><span style=\"color: #007700\">[<\/span><span style=\"color: #DD0000\">'HTTPS'<\/span><span style=\"color: #007700\">])&nbsp;&amp;&amp;&nbsp;<\/span><span style=\"color: #0000BB\">$_SERVER<\/span><span style=\"color: #007700\">[<\/span><span style=\"color: #DD0000\">'HTTPS'<\/span><span style=\"color: #007700\">]&nbsp;!==&nbsp;<\/span><span style=\"color: #DD0000\">'off'<\/span><span style=\"color: #007700\">)&nbsp;?&nbsp;<\/span><span style=\"color: #DD0000\">'https'&nbsp;<\/span><span style=\"color: #007700\">:&nbsp;<\/span><span style=\"color: #DD0000\">'http'<\/span><span style=\"color: #007700\">; <br \/><\/span><span style=\"color: #0000BB\">$host&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">$_SERVER<\/span><span style=\"color: #007700\">[<\/span><span style=\"color: #DD0000\">'HTTP_HOST'<\/span><span style=\"color: #007700\">]; <br \/><\/span><span style=\"color: #0000BB\">$path&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">rtrim<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">dirname<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$_SERVER<\/span><span style=\"color: #007700\">[<\/span><span style=\"color: #DD0000\">'PHP_SELF'<\/span><span style=\"color: #007700\">]),&nbsp;<\/span><span style=\"color: #DD0000\">'\/\\\\'<\/span><span style=\"color: #007700\">); <br \/><\/span><span style=\"color: #0000BB\">$verifyUrl&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">$protocol&nbsp;<\/span><span style=\"color: #007700\">.&nbsp;<\/span><span style=\"color: #DD0000\">':\/\/'&nbsp;<\/span><span style=\"color: #007700\">.&nbsp;<\/span><span style=\"color: #0000BB\">$host&nbsp;<\/span><span style=\"color: #007700\">.&nbsp;<\/span><span style=\"color: #0000BB\">$path&nbsp;<\/span><span style=\"color: #007700\">.&nbsp;<\/span><span style=\"color: #DD0000\">'\/verify.php?token='&nbsp;<\/span><span style=\"color: #007700\">.&nbsp;<\/span><span style=\"color: #0000BB\">urlencode<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$token<\/span><span style=\"color: #007700\">); <br \/> <br \/><\/span><span style=\"color: #FF8000\">\/\/&nbsp;professional&nbsp;HTML&nbsp;email&nbsp;template&nbsp;(simple,&nbsp;clean) <br \/><\/span><span style=\"color: #0000BB\">$subject&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #DD0000\">'Confirm&nbsp;your&nbsp;subscription'<\/span><span style=\"color: #007700\">; <br \/> <br \/><\/span><span style=\"color: #0000BB\">$message&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #DD0000\">\"&lt;!doctype&nbsp;html&gt; <br \/>&lt;html&gt; <br \/>&lt;head&gt; <br \/>&nbsp;&nbsp;&lt;meta&nbsp;charset=\\\"utf-8\\\"&gt;&nbsp; <br \/>&nbsp;&nbsp;&lt;meta&nbsp;name=\\\"viewport\\\"&nbsp;content=\\\"width=device-width,initial-scale=1\\\"&gt;&nbsp; <br \/>&nbsp;&nbsp;&lt;style&gt;body{font-family:Arial,Helvetica,sans-serif;color:#111}&nbsp;a.button{display:inline-block;padding:12px&nbsp;18px;background:#1366d6;color:#fff;border-radius:6px;text-decoration:none}&lt;\/style&gt; <br \/>&lt;\/head&gt; <br \/>&lt;body&gt; <br \/>&nbsp;&nbsp;&lt;table&nbsp;width=\\\"100%\\\"&nbsp;cellpadding=\\\"0\\\"&nbsp;cellspacing=\\\"0\\\"&nbsp;role=\\\"presentation\\\"&gt; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&lt;tr&gt;&lt;td&nbsp;align=\\\"center\\\"&gt; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;table&nbsp;width=\\\"600\\\"&nbsp;cellpadding=\\\"0\\\"&nbsp;cellspacing=\\\"0\\\"&nbsp;style=\\\"background:#ffffff;border-radius:8px;padding:24px;\\\"&nbsp;role=\\\"presentation\\\"&gt; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;tr&gt;&lt;td&gt; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;h2&nbsp;style=\\\"color:#123;\\\"&gt;Confirm&nbsp;your&nbsp;email&nbsp;address&lt;\/h2&gt; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;p&gt;Hi&nbsp;\"&nbsp;<\/span><span style=\"color: #007700\">.&nbsp;<\/span><span style=\"color: #0000BB\">htmlspecialchars<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$name_for_db<\/span><span style=\"color: #007700\">,&nbsp;<\/span><span style=\"color: #0000BB\">ENT_QUOTES&nbsp;<\/span><span style=\"color: #007700\">|&nbsp;<\/span><span style=\"color: #0000BB\">ENT_HTML5<\/span><span style=\"color: #007700\">,&nbsp;<\/span><span style=\"color: #DD0000\">'UTF-8'<\/span><span style=\"color: #007700\">)&nbsp;.&nbsp;<\/span><span style=\"color: #DD0000\">\",&lt;\/p&gt; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;p&gt;Thank&nbsp;you&nbsp;for&nbsp;subscribing&nbsp;to&nbsp;our&nbsp;newsletter.&nbsp;Please&nbsp;click&nbsp;the&nbsp;button&nbsp;below&nbsp;to&nbsp;confirm&nbsp;your&nbsp;subscription&nbsp;and&nbsp;start&nbsp;receiving&nbsp;updates.&lt;\/p&gt; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;p&nbsp;style=\\\"text-align:center;\\\"&gt;&lt;a&nbsp;class=\\\"button\\\"&nbsp;href=\\\"\"&nbsp;<\/span><span style=\"color: #007700\">.&nbsp;<\/span><span style=\"color: #0000BB\">$verifyUrl&nbsp;<\/span><span style=\"color: #007700\">.&nbsp;<\/span><span style=\"color: #DD0000\">\"\\\"&gt;Confirm&nbsp;my&nbsp;subscription&lt;\/a&gt;&lt;\/p&gt; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;p&gt;If&nbsp;the&nbsp;button&nbsp;doesn't&nbsp;work,&nbsp;copy&nbsp;and&nbsp;paste&nbsp;the&nbsp;following&nbsp;URL&nbsp;into&nbsp;your&nbsp;browser:&lt;\/p&gt; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;p&nbsp;style=\\\"word-break:break-all;\\\"&gt;&lt;a&nbsp;href=\\\"\"&nbsp;<\/span><span style=\"color: #007700\">.&nbsp;<\/span><span style=\"color: #0000BB\">$verifyUrl&nbsp;<\/span><span style=\"color: #007700\">.&nbsp;<\/span><span style=\"color: #DD0000\">\"\\\"&gt;\"&nbsp;<\/span><span style=\"color: #007700\">.&nbsp;<\/span><span style=\"color: #0000BB\">$verifyUrl&nbsp;<\/span><span style=\"color: #007700\">.&nbsp;<\/span><span style=\"color: #DD0000\">\"&lt;\/a&gt;&lt;\/p&gt; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;hr&gt; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;p&nbsp;style=\\\"font-size:0.9rem;color:#666;\\\"&gt;If&nbsp;you&nbsp;did&nbsp;not&nbsp;request&nbsp;this&nbsp;email,&nbsp;you&nbsp;can&nbsp;safely&nbsp;ignore&nbsp;it.&lt;\/p&gt; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;\/td&gt;&lt;\/tr&gt; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;\/table&gt; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&lt;\/td&gt;&lt;\/tr&gt; <br \/>&nbsp;&nbsp;&lt;\/table&gt; <br \/>&lt;\/body&gt; <br \/>&lt;\/html&gt;\"<\/span><span style=\"color: #007700\">; <br \/> <br \/><\/span><span style=\"color: #0000BB\">$headers&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #DD0000\">\"MIME-Version:&nbsp;1.0\\r\\n\"<\/span><span style=\"color: #007700\">; <br \/><\/span><span style=\"color: #0000BB\">$headers&nbsp;<\/span><span style=\"color: #007700\">.=&nbsp;<\/span><span style=\"color: #DD0000\">\"Content-type:&nbsp;text\/html;&nbsp;charset=utf-8\\r\\n\"<\/span><span style=\"color: #007700\">; <br \/><\/span><span style=\"color: #0000BB\">$headers&nbsp;<\/span><span style=\"color: #007700\">.=&nbsp;<\/span><span style=\"color: #DD0000\">\"From:&nbsp;\"&nbsp;<\/span><span style=\"color: #007700\">.&nbsp;<\/span><span style=\"color: #0000BB\">addslashes<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$config<\/span><span style=\"color: #007700\">[<\/span><span style=\"color: #DD0000\">'mail_from_name'<\/span><span style=\"color: #007700\">])&nbsp;.&nbsp;<\/span><span style=\"color: #DD0000\">\"&nbsp;&lt;\"&nbsp;<\/span><span style=\"color: #007700\">.&nbsp;<\/span><span style=\"color: #0000BB\">$config<\/span><span style=\"color: #007700\">[<\/span><span style=\"color: #DD0000\">'mail_from'<\/span><span style=\"color: #007700\">]&nbsp;.&nbsp;<\/span><span style=\"color: #DD0000\">\"&gt;\\r\\n\"<\/span><span style=\"color: #007700\">; <br \/> <br \/><\/span><span style=\"color: #FF8000\">\/\/&nbsp;send&nbsp;email <br \/><\/span><span style=\"color: #0000BB\">$mailSent&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;@<\/span><span style=\"color: #0000BB\">mail<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$email_for_db<\/span><span style=\"color: #007700\">,&nbsp;<\/span><span style=\"color: #0000BB\">$subject<\/span><span style=\"color: #007700\">,&nbsp;<\/span><span style=\"color: #0000BB\">$message<\/span><span style=\"color: #007700\">,&nbsp;<\/span><span style=\"color: #0000BB\">$headers<\/span><span style=\"color: #007700\">); <br \/> <br \/>if&nbsp;(<\/span><span style=\"color: #0000BB\">$mailSent<\/span><span style=\"color: #007700\">)&nbsp;{ <br \/>&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #FF8000\">\/\/&nbsp;return&nbsp;success <br \/>&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #007700\">echo&nbsp;<\/span><span style=\"color: #0000BB\">json_encode<\/span><span style=\"color: #007700\">([<\/span><span style=\"color: #DD0000\">'success'&nbsp;<\/span><span style=\"color: #007700\">=&gt;&nbsp;<\/span><span style=\"color: #0000BB\">true<\/span><span style=\"color: #007700\">,&nbsp;<\/span><span style=\"color: #DD0000\">'message'&nbsp;<\/span><span style=\"color: #007700\">=&gt;&nbsp;<\/span><span style=\"color: #DD0000\">'Subscription&nbsp;saved.&nbsp;A&nbsp;verification&nbsp;email&nbsp;has&nbsp;been&nbsp;sent&nbsp;to&nbsp;your&nbsp;inbox.'<\/span><span style=\"color: #007700\">]); <br \/>}&nbsp;else&nbsp;{ <br \/>&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #FF8000\">\/\/&nbsp;still&nbsp;a&nbsp;success&nbsp;from&nbsp;DB&nbsp;perspective,&nbsp;but&nbsp;warn&nbsp;user&nbsp;that&nbsp;delivery&nbsp;may&nbsp;fail <br \/>&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #007700\">echo&nbsp;<\/span><span style=\"color: #0000BB\">json_encode<\/span><span style=\"color: #007700\">([<\/span><span style=\"color: #DD0000\">'success'&nbsp;<\/span><span style=\"color: #007700\">=&gt;&nbsp;<\/span><span style=\"color: #0000BB\">true<\/span><span style=\"color: #007700\">,&nbsp;<\/span><span style=\"color: #DD0000\">'message'&nbsp;<\/span><span style=\"color: #007700\">=&gt;&nbsp;<\/span><span style=\"color: #DD0000\">'Subscription&nbsp;saved.&nbsp;We&nbsp;could&nbsp;not&nbsp;send&nbsp;the&nbsp;verification&nbsp;email&nbsp;automatically;&nbsp;please&nbsp;contact&nbsp;support.'<\/span><span style=\"color: #007700\">]); <br \/>} <br \/><\/span><span style=\"color: #0000BB\">?&gt;<\/span><\/pre>\n<h2>\u2699\ufe0f Step 6: Verify Email Address<\/h2>\n<p>Create a <code>verify.php<\/code> file to handle email verification. This file will check the verification token sent to the user&#8217;s email and activate their account.<\/p>\n<ul>\n<li>Get the token from the query parameter.<\/li>\n<li>Check if the token exists in the database.<\/li>\n<li>If valid, update the user&#8217;s status to <code>verified<\/code>.<\/li>\n<li>Display a success or error message based on the verification result.<\/li>\n<\/ul>\n<pre><span style=\"color: #0000BB\">&lt;?php <br \/><\/span><span style=\"color: #FF8000\">\/\/&nbsp;include&nbsp;database&nbsp;connection <br \/><\/span><span style=\"color: #007700\">require_once&nbsp;<\/span><span style=\"color: #0000BB\">__DIR__&nbsp;<\/span><span style=\"color: #007700\">.&nbsp;<\/span><span style=\"color: #DD0000\">'\/db.php'<\/span><span style=\"color: #007700\">; <br \/> <br \/><\/span><span style=\"color: #FF8000\">\/\/&nbsp;get&nbsp;token&nbsp;from&nbsp;query&nbsp;parameter <br \/><\/span><span style=\"color: #0000BB\">$token&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;isset(<\/span><span style=\"color: #0000BB\">$_GET<\/span><span style=\"color: #007700\">[<\/span><span style=\"color: #DD0000\">'token'<\/span><span style=\"color: #007700\">])&nbsp;?&nbsp;<\/span><span style=\"color: #0000BB\">trim<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$_GET<\/span><span style=\"color: #007700\">[<\/span><span style=\"color: #DD0000\">'token'<\/span><span style=\"color: #007700\">])&nbsp;:&nbsp;<\/span><span style=\"color: #DD0000\">''<\/span><span style=\"color: #007700\">; <br \/><\/span><span style=\"color: #0000BB\">?&gt;<\/span>\r\n<span style=\"color: rgb(95, 94, 78);\"><span class=\"hljs-meta\" style=\"color: rgb(174, 115, 19);\">&lt;!doctype html&gt;<\/span>\r\n<span class=\"hljs-tag\" style=\"color: rgb(186, 98, 54);\">&lt;<span class=\"hljs-name\" style=\"color: rgb(186, 98, 54);\">html<\/span>&gt;<\/span>\r\n<span class=\"hljs-tag\" style=\"color: rgb(186, 98, 54);\">&lt;<span class=\"hljs-name\" style=\"color: rgb(186, 98, 54);\">head<\/span>&gt;<\/span>\r\n    <span class=\"hljs-tag\" style=\"color: rgb(186, 98, 54);\">&lt;<span class=\"hljs-name\" style=\"color: rgb(186, 98, 54);\">meta<\/span> <span class=\"hljs-attr\">charset<\/span>=<span class=\"hljs-string\" style=\"color: rgb(125, 151, 38);\">\"utf-8\"<\/span>&gt;<\/span>\r\n    <span class=\"hljs-tag\" style=\"color: rgb(186, 98, 54);\">&lt;<span class=\"hljs-name\" style=\"color: rgb(186, 98, 54);\">meta<\/span> <span class=\"hljs-attr\">name<\/span>=<span class=\"hljs-string\" style=\"color: rgb(125, 151, 38);\">\"viewport\"<\/span> <span class=\"hljs-attr\">content<\/span>=<span class=\"hljs-string\" style=\"color: rgb(125, 151, 38);\">\"width=device-width,initial-scale=1\"<\/span>&gt;<\/span>\r\n    <span class=\"hljs-tag\" style=\"color: rgb(186, 98, 54);\">&lt;<span class=\"hljs-name\" style=\"color: rgb(186, 98, 54);\">title<\/span>&gt;<\/span>Subscription Verification - CodexWorld<span class=\"hljs-tag\" style=\"color: rgb(186, 98, 54);\">&lt;\/<span class=\"hljs-name\" style=\"color: rgb(186, 98, 54);\">title<\/span>&gt;<\/span>\r\n\r\n    <span class=\"hljs-comment\" style=\"color: rgb(108, 107, 90);\">&lt;!-- stylesheet file --&gt;<\/span>\r\n    <span class=\"hljs-tag\" style=\"color: rgb(186, 98, 54);\">&lt;<span class=\"hljs-name\" style=\"color: rgb(186, 98, 54);\">link<\/span> <span class=\"hljs-attr\">rel<\/span>=<span class=\"hljs-string\" style=\"color: rgb(125, 151, 38);\">\"stylesheet\"<\/span> <span class=\"hljs-attr\">href<\/span>=<span class=\"hljs-string\" style=\"color: rgb(125, 151, 38);\">\"css\/style.css\"<\/span>&gt;<\/span>\r\n<span class=\"hljs-tag\" style=\"color: rgb(186, 98, 54);\">&lt;\/<span class=\"hljs-name\" style=\"color: rgb(186, 98, 54);\">head<\/span>&gt;<\/span>\r\n<span class=\"hljs-tag\" style=\"color: rgb(186, 98, 54);\">&lt;<span class=\"hljs-name\" style=\"color: rgb(186, 98, 54);\">body<\/span>&gt;<\/span>\r\n<span class=\"hljs-tag\" style=\"color: rgb(186, 98, 54);\">&lt;<span class=\"hljs-name\" style=\"color: rgb(186, 98, 54);\">main<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\" style=\"color: rgb(125, 151, 38);\">\"container\"<\/span>&gt;<\/span>\r\n&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"color: #0000BB\">&lt;?php <br \/>&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #FF8000\">\/\/&nbsp;validate&nbsp;token <br \/>&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #007700\">if&nbsp;(empty(<\/span><span style=\"color: #0000BB\">$token<\/span><span style=\"color: #007700\">))&nbsp;{ <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;<\/span><span style=\"color: #DD0000\">'&lt;h1&gt;Invalid&nbsp;verification&nbsp;link&lt;\/h1&gt;&lt;p&nbsp;class=\"msg&nbsp;error\"&gt;No&nbsp;token&nbsp;provided.&lt;\/p&gt;'<\/span><span style=\"color: #007700\">; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;} <br \/> <br \/>&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #FF8000\">\/\/&nbsp;look&nbsp;up&nbsp;subscription&nbsp;by&nbsp;token <br \/>&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">$stmt&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">$mysqli<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">prepare<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #DD0000\">'SELECT&nbsp;id,&nbsp;email,&nbsp;name,&nbsp;is_verified&nbsp;FROM&nbsp;subscriptions&nbsp;WHERE&nbsp;token&nbsp;=&nbsp;?&nbsp;LIMIT&nbsp;1'<\/span><span style=\"color: #007700\">); <br \/>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(!<\/span><span style=\"color: #0000BB\">$stmt<\/span><span style=\"color: #007700\">)&nbsp;{ <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;<\/span><span style=\"color: #DD0000\">'&lt;h1&gt;Server&nbsp;error&lt;\/h1&gt;&lt;p&nbsp;class=\"msg&nbsp;error\"&gt;Please&nbsp;try&nbsp;again&nbsp;later.&lt;\/p&gt;'<\/span><span style=\"color: #007700\">; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;} <br \/>&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">$stmt<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">bind_param<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #DD0000\">'s'<\/span><span style=\"color: #007700\">,&nbsp;<\/span><span style=\"color: #0000BB\">$token<\/span><span style=\"color: #007700\">); <br \/>&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">$stmt<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">execute<\/span><span style=\"color: #007700\">(); <br \/>&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">$stmt<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">bind_result<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$id<\/span><span style=\"color: #007700\">,&nbsp;<\/span><span style=\"color: #0000BB\">$email<\/span><span style=\"color: #007700\">,&nbsp;<\/span><span style=\"color: #0000BB\">$name<\/span><span style=\"color: #007700\">,&nbsp;<\/span><span style=\"color: #0000BB\">$is_verified<\/span><span style=\"color: #007700\">); <br \/>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(!<\/span><span style=\"color: #0000BB\">$stmt<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">fetch<\/span><span style=\"color: #007700\">())&nbsp;{ <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;<\/span><span style=\"color: #DD0000\">'&lt;h1&gt;Invalid&nbsp;or&nbsp;expired&nbsp;link&lt;\/h1&gt;&lt;p&nbsp;class=\"msg&nbsp;error\"&gt;We&nbsp;could&nbsp;not&nbsp;find&nbsp;a&nbsp;matching&nbsp;subscription.&lt;\/p&gt;'<\/span><span style=\"color: #007700\">; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">$stmt<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">close<\/span><span style=\"color: #007700\">(); <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;} <br \/>&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">$stmt<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">close<\/span><span style=\"color: #007700\">(); <br \/> <br \/>&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #FF8000\">\/\/&nbsp;check&nbsp;if&nbsp;already&nbsp;verified <br \/>&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #007700\">if&nbsp;(<\/span><span style=\"color: #0000BB\">$is_verified&nbsp;<\/span><span style=\"color: #007700\">==&nbsp;<\/span><span style=\"color: #0000BB\">1<\/span><span style=\"color: #007700\">)&nbsp;{ <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;<\/span><span style=\"color: #DD0000\">'&lt;h1&gt;Already&nbsp;verified&lt;\/h1&gt;&lt;p&nbsp;class=\"msg&nbsp;info\"&gt;This&nbsp;email&nbsp;('&nbsp;<\/span><span style=\"color: #007700\">.&nbsp;<\/span><span style=\"color: #0000BB\">htmlspecialchars<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$email<\/span><span style=\"color: #007700\">)&nbsp;.&nbsp;<\/span><span style=\"color: #DD0000\">')&nbsp;has&nbsp;already&nbsp;been&nbsp;verified.&lt;\/p&gt;'<\/span><span style=\"color: #007700\">; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;} <br \/> <br \/>&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #FF8000\">\/\/&nbsp;update&nbsp;database&nbsp;to&nbsp;set&nbsp;verified <br \/>&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">$up&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">$mysqli<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">prepare<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #DD0000\">'UPDATE&nbsp;subscriptions&nbsp;SET&nbsp;is_verified&nbsp;=&nbsp;1,&nbsp;verified_at&nbsp;=&nbsp;NOW()&nbsp;WHERE&nbsp;id&nbsp;=&nbsp;?'<\/span><span style=\"color: #007700\">); <br \/>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(!<\/span><span style=\"color: #0000BB\">$up<\/span><span style=\"color: #007700\">)&nbsp;{ <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;<\/span><span style=\"color: #DD0000\">'&lt;h1&gt;Server&nbsp;error&lt;\/h1&gt;&lt;p&nbsp;class=\"msg&nbsp;error\"&gt;Could&nbsp;not&nbsp;verify&nbsp;at&nbsp;this&nbsp;time.&lt;\/p&gt;'<\/span><span style=\"color: #007700\">; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;} <br \/>&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">$up<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">bind_param<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #DD0000\">'i'<\/span><span style=\"color: #007700\">,&nbsp;<\/span><span style=\"color: #0000BB\">$id<\/span><span style=\"color: #007700\">); <br \/>&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">$ok&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">$up<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">execute<\/span><span style=\"color: #007700\">(); <br \/>&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">$up<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">close<\/span><span style=\"color: #007700\">(); <br \/> <br \/>&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #FF8000\">\/\/&nbsp;show&nbsp;result <br \/>&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #007700\">if&nbsp;(<\/span><span style=\"color: #0000BB\">$ok<\/span><span style=\"color: #007700\">)&nbsp;{ <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;<\/span><span style=\"color: #DD0000\">'&lt;h1&gt;Subscription&nbsp;confirmed&lt;\/h1&gt;&lt;p&nbsp;class=\"msg&nbsp;success\"&gt;Thank&nbsp;you,&nbsp;'&nbsp;<\/span><span style=\"color: #007700\">.&nbsp;<\/span><span style=\"color: #0000BB\">htmlspecialchars<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$name<\/span><span style=\"color: #007700\">)&nbsp;.&nbsp;<\/span><span style=\"color: #DD0000\">'.&nbsp;Your&nbsp;email&nbsp;('&nbsp;<\/span><span style=\"color: #007700\">.&nbsp;<\/span><span style=\"color: #0000BB\">htmlspecialchars<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$email<\/span><span style=\"color: #007700\">)&nbsp;.&nbsp;<\/span><span style=\"color: #DD0000\">')&nbsp;has&nbsp;been&nbsp;verified.&nbsp;You&nbsp;will&nbsp;now&nbsp;receive&nbsp;our&nbsp;newsletter.&lt;\/p&gt;'<\/span><span style=\"color: #007700\">; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;else&nbsp;{ <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;<\/span><span style=\"color: #DD0000\">'&lt;h1&gt;Verification&nbsp;failed&lt;\/h1&gt;&lt;p&nbsp;class=\"msg&nbsp;error\"&gt;Could&nbsp;not&nbsp;update&nbsp;verification&nbsp;status.&nbsp;Please&nbsp;try&nbsp;again&nbsp;later.&lt;\/p&gt;'<\/span><span style=\"color: #007700\">; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;} <br \/>&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">?&gt;<\/span>\r\n<span class=\"hljs-tag\" style=\"color: rgb(186, 98, 54);\">&lt;\/<span class=\"hljs-name\" style=\"color: rgb(186, 98, 54);\">main<\/span>&gt;<\/span>\r\n<span class=\"hljs-tag\" style=\"color: rgb(186, 98, 54);\">&lt;\/<span class=\"hljs-name\" style=\"color: rgb(186, 98, 54);\">body<\/span>&gt;<\/span>\r\n<span class=\"hljs-tag\" style=\"color: rgb(186, 98, 54);\">&lt;\/<span class=\"hljs-name\" style=\"color: rgb(186, 98, 54);\">html<\/span>&gt;<\/span><\/span>\r\n<\/pre>\n<h2>\ud83d\udee1\ufe0f Security and Best Practices<\/h2>\n<ul>\n<li>Always sanitize and validate user inputs to prevent SQL injection and XSS attacks.<\/li>\n<li>Use prepared statements (PDO or MySQLi) for database queries to prevent SQL injection.<\/li>\n<li>Validate email format using <code>filter_var($email, FILTER_VALIDATE_EMAIL)<\/code>.<\/li>\n<li>Use SMTP for sending emails to ensure reliable delivery (consider using libraries like PHPMailer).<\/li>\n<li>Add CAPTCHA (like Google reCAPTCHA) to your subscription form to prevent bot sign-ups.<\/li>\n<li>Implement error handling to manage exceptions gracefully.<\/li>\n<li>Consider using HTTPS to secure data transmission.<\/li>\n<\/ul>\n<h2>\ud83c\udf89 Conclusion<\/h2>\n<p>With this tutorial, you\u2019ve built a complete email newsletter subscription system with PHP and MySQL, including email verification. You can now connect this system to email marketing platforms or use it to send periodic newsletters manually.<\/p>\n<p>Feel free to expand upon this basic system by adding features like unsubscribe functionality, user management, or analytics. Happy coding! \ud83d\ude80<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A newsletter subscription is the most used feature on the website or blog. The email subscription functionality provides an option for the visitor to receive updates via email. The email subscription feature is the most <\/p>\n","protected":false},"author":1,"featured_media":5930,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[4],"tags":[28,23,225,392,14,341],"class_list":["post-4289","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-php","tag-ajax","tag-email","tag-form","tag-newsletter","tag-php","tag-subscription","cat-4-id","has_thumb"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Email Newsletter Subscription System with PHP &amp; MySQL - CodexWorld<\/title>\n<meta name=\"description\" content=\"Learn how to create an Email Newsletter Subscription System using PHP and MySQL. Step-by-step tutorial with code for email verification, database setup, and secure form submission.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.codexworld.com\/newsletter-email-subscription-with-php-mysql\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Email Newsletter Subscription System with PHP &amp; MySQL - CodexWorld\" \/>\n<meta property=\"og:description\" content=\"Learn how to create an Email Newsletter Subscription System using PHP and MySQL. Step-by-step tutorial with code for email verification, database setup, and secure form submission.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.codexworld.com\/newsletter-email-subscription-with-php-mysql\/\" \/>\n<meta property=\"og:site_name\" content=\"CodexWorld\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/codexworld\" \/>\n<meta property=\"article:author\" content=\"https:\/\/www.facebook.com\/codexworld\" \/>\n<meta property=\"article:published_time\" content=\"2020-02-04T19:27:03+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-11-06T14:14:22+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.codexworld.com\/wp-content\/uploads\/2020\/02\/email-newsletter-subscription-system-with-php-mysql-codexworld.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1920\" \/>\n\t<meta property=\"og:image:height\" content=\"1080\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"CodexWorld\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@codexworldblog\" \/>\n<meta name=\"twitter:site\" content=\"@codexworldweb\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"CodexWorld\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/newsletter-email-subscription-with-php-mysql\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/newsletter-email-subscription-with-php-mysql\\\/\"},\"author\":{\"name\":\"CodexWorld\",\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/#\\\/schema\\\/person\\\/9da51d8fa3cdefeb5ec9c69136d4baf0\"},\"headline\":\"Email Newsletter Subscription System with PHP &#038; MySQL\",\"datePublished\":\"2020-02-04T19:27:03+00:00\",\"dateModified\":\"2025-11-06T14:14:22+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/newsletter-email-subscription-with-php-mysql\\\/\"},\"wordCount\":761,\"commentCount\":9,\"publisher\":{\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/newsletter-email-subscription-with-php-mysql\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.codexworld.com\\\/wp-content\\\/uploads\\\/2020\\\/02\\\/email-newsletter-subscription-system-with-php-mysql-codexworld.png\",\"keywords\":[\"Ajax\",\"Email\",\"Form\",\"Newsletter\",\"PHP\",\"Subscription\"],\"articleSection\":[\"PHP\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.codexworld.com\\\/newsletter-email-subscription-with-php-mysql\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/newsletter-email-subscription-with-php-mysql\\\/\",\"url\":\"https:\\\/\\\/www.codexworld.com\\\/newsletter-email-subscription-with-php-mysql\\\/\",\"name\":\"Email Newsletter Subscription System with PHP & MySQL - CodexWorld\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/newsletter-email-subscription-with-php-mysql\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/newsletter-email-subscription-with-php-mysql\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.codexworld.com\\\/wp-content\\\/uploads\\\/2020\\\/02\\\/email-newsletter-subscription-system-with-php-mysql-codexworld.png\",\"datePublished\":\"2020-02-04T19:27:03+00:00\",\"dateModified\":\"2025-11-06T14:14:22+00:00\",\"description\":\"Learn how to create an Email Newsletter Subscription System using PHP and MySQL. Step-by-step tutorial with code for email verification, database setup, and secure form submission.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/newsletter-email-subscription-with-php-mysql\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.codexworld.com\\\/newsletter-email-subscription-with-php-mysql\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/newsletter-email-subscription-with-php-mysql\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.codexworld.com\\\/wp-content\\\/uploads\\\/2020\\\/02\\\/email-newsletter-subscription-system-with-php-mysql-codexworld.png\",\"contentUrl\":\"https:\\\/\\\/www.codexworld.com\\\/wp-content\\\/uploads\\\/2020\\\/02\\\/email-newsletter-subscription-system-with-php-mysql-codexworld.png\",\"width\":1920,\"height\":1080,\"caption\":\"email-newsletter-subscription-system-with-php-mysql-codexworld\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/newsletter-email-subscription-with-php-mysql\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.codexworld.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Email Newsletter Subscription System with PHP &#038; MySQL\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/#website\",\"url\":\"https:\\\/\\\/www.codexworld.com\\\/\",\"name\":\"CodexWorld\",\"description\":\"Web &amp; Mobile App Development Company\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.codexworld.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/#organization\",\"name\":\"CodexWorld\",\"url\":\"https:\\\/\\\/www.codexworld.com\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.codexworld.com\\\/wp-content\\\/uploads\\\/2014\\\/09\\\/codexworld-logo.png\",\"contentUrl\":\"https:\\\/\\\/www.codexworld.com\\\/wp-content\\\/uploads\\\/2014\\\/09\\\/codexworld-logo.png\",\"width\":200,\"height\":19,\"caption\":\"CodexWorld\"},\"image\":{\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/codexworld\",\"https:\\\/\\\/x.com\\\/codexworldweb\",\"https:\\\/\\\/www.linkedin.com\\\/company\\\/codexworld\",\"https:\\\/\\\/www.youtube.com\\\/codexworld\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/#\\\/schema\\\/person\\\/9da51d8fa3cdefeb5ec9c69136d4baf0\",\"name\":\"CodexWorld\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/cf4999db3b409de559f80677afa01729bb2eeda19be273c254e8b2c22729e386?s=96&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/cf4999db3b409de559f80677afa01729bb2eeda19be273c254e8b2c22729e386?s=96&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/cf4999db3b409de559f80677afa01729bb2eeda19be273c254e8b2c22729e386?s=96&r=g\",\"caption\":\"CodexWorld\"},\"description\":\"CodexWorld is a programming blog, one-stop destination for web professionals \u2014 developers, programmers, freelancers, and site owners.\",\"sameAs\":[\"http:\\\/\\\/www.codexworld.com\",\"https:\\\/\\\/www.facebook.com\\\/codexworld\",\"https:\\\/\\\/x.com\\\/codexworldblog\"],\"url\":\"https:\\\/\\\/www.codexworld.com\\\/author\\\/nitya192265\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Email Newsletter Subscription System with PHP & MySQL - CodexWorld","description":"Learn how to create an Email Newsletter Subscription System using PHP and MySQL. Step-by-step tutorial with code for email verification, database setup, and secure form submission.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.codexworld.com\/newsletter-email-subscription-with-php-mysql\/","og_locale":"en_US","og_type":"article","og_title":"Email Newsletter Subscription System with PHP & MySQL - CodexWorld","og_description":"Learn how to create an Email Newsletter Subscription System using PHP and MySQL. Step-by-step tutorial with code for email verification, database setup, and secure form submission.","og_url":"https:\/\/www.codexworld.com\/newsletter-email-subscription-with-php-mysql\/","og_site_name":"CodexWorld","article_publisher":"https:\/\/www.facebook.com\/codexworld","article_author":"https:\/\/www.facebook.com\/codexworld","article_published_time":"2020-02-04T19:27:03+00:00","article_modified_time":"2025-11-06T14:14:22+00:00","og_image":[{"width":1920,"height":1080,"url":"https:\/\/www.codexworld.com\/wp-content\/uploads\/2020\/02\/email-newsletter-subscription-system-with-php-mysql-codexworld.png","type":"image\/png"}],"author":"CodexWorld","twitter_card":"summary_large_image","twitter_creator":"@codexworldblog","twitter_site":"@codexworldweb","twitter_misc":{"Written by":"CodexWorld","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.codexworld.com\/newsletter-email-subscription-with-php-mysql\/#article","isPartOf":{"@id":"https:\/\/www.codexworld.com\/newsletter-email-subscription-with-php-mysql\/"},"author":{"name":"CodexWorld","@id":"https:\/\/www.codexworld.com\/#\/schema\/person\/9da51d8fa3cdefeb5ec9c69136d4baf0"},"headline":"Email Newsletter Subscription System with PHP &#038; MySQL","datePublished":"2020-02-04T19:27:03+00:00","dateModified":"2025-11-06T14:14:22+00:00","mainEntityOfPage":{"@id":"https:\/\/www.codexworld.com\/newsletter-email-subscription-with-php-mysql\/"},"wordCount":761,"commentCount":9,"publisher":{"@id":"https:\/\/www.codexworld.com\/#organization"},"image":{"@id":"https:\/\/www.codexworld.com\/newsletter-email-subscription-with-php-mysql\/#primaryimage"},"thumbnailUrl":"https:\/\/www.codexworld.com\/wp-content\/uploads\/2020\/02\/email-newsletter-subscription-system-with-php-mysql-codexworld.png","keywords":["Ajax","Email","Form","Newsletter","PHP","Subscription"],"articleSection":["PHP"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.codexworld.com\/newsletter-email-subscription-with-php-mysql\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.codexworld.com\/newsletter-email-subscription-with-php-mysql\/","url":"https:\/\/www.codexworld.com\/newsletter-email-subscription-with-php-mysql\/","name":"Email Newsletter Subscription System with PHP & MySQL - CodexWorld","isPartOf":{"@id":"https:\/\/www.codexworld.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.codexworld.com\/newsletter-email-subscription-with-php-mysql\/#primaryimage"},"image":{"@id":"https:\/\/www.codexworld.com\/newsletter-email-subscription-with-php-mysql\/#primaryimage"},"thumbnailUrl":"https:\/\/www.codexworld.com\/wp-content\/uploads\/2020\/02\/email-newsletter-subscription-system-with-php-mysql-codexworld.png","datePublished":"2020-02-04T19:27:03+00:00","dateModified":"2025-11-06T14:14:22+00:00","description":"Learn how to create an Email Newsletter Subscription System using PHP and MySQL. Step-by-step tutorial with code for email verification, database setup, and secure form submission.","breadcrumb":{"@id":"https:\/\/www.codexworld.com\/newsletter-email-subscription-with-php-mysql\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.codexworld.com\/newsletter-email-subscription-with-php-mysql\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.codexworld.com\/newsletter-email-subscription-with-php-mysql\/#primaryimage","url":"https:\/\/www.codexworld.com\/wp-content\/uploads\/2020\/02\/email-newsletter-subscription-system-with-php-mysql-codexworld.png","contentUrl":"https:\/\/www.codexworld.com\/wp-content\/uploads\/2020\/02\/email-newsletter-subscription-system-with-php-mysql-codexworld.png","width":1920,"height":1080,"caption":"email-newsletter-subscription-system-with-php-mysql-codexworld"},{"@type":"BreadcrumbList","@id":"https:\/\/www.codexworld.com\/newsletter-email-subscription-with-php-mysql\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.codexworld.com\/"},{"@type":"ListItem","position":2,"name":"Email Newsletter Subscription System with PHP &#038; MySQL"}]},{"@type":"WebSite","@id":"https:\/\/www.codexworld.com\/#website","url":"https:\/\/www.codexworld.com\/","name":"CodexWorld","description":"Web &amp; Mobile App Development Company","publisher":{"@id":"https:\/\/www.codexworld.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.codexworld.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.codexworld.com\/#organization","name":"CodexWorld","url":"https:\/\/www.codexworld.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.codexworld.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.codexworld.com\/wp-content\/uploads\/2014\/09\/codexworld-logo.png","contentUrl":"https:\/\/www.codexworld.com\/wp-content\/uploads\/2014\/09\/codexworld-logo.png","width":200,"height":19,"caption":"CodexWorld"},"image":{"@id":"https:\/\/www.codexworld.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/codexworld","https:\/\/x.com\/codexworldweb","https:\/\/www.linkedin.com\/company\/codexworld","https:\/\/www.youtube.com\/codexworld"]},{"@type":"Person","@id":"https:\/\/www.codexworld.com\/#\/schema\/person\/9da51d8fa3cdefeb5ec9c69136d4baf0","name":"CodexWorld","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/cf4999db3b409de559f80677afa01729bb2eeda19be273c254e8b2c22729e386?s=96&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/cf4999db3b409de559f80677afa01729bb2eeda19be273c254e8b2c22729e386?s=96&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/cf4999db3b409de559f80677afa01729bb2eeda19be273c254e8b2c22729e386?s=96&r=g","caption":"CodexWorld"},"description":"CodexWorld is a programming blog, one-stop destination for web professionals \u2014 developers, programmers, freelancers, and site owners.","sameAs":["http:\/\/www.codexworld.com","https:\/\/www.facebook.com\/codexworld","https:\/\/x.com\/codexworldblog"],"url":"https:\/\/www.codexworld.com\/author\/nitya192265\/"}]}},"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/www.codexworld.com\/wp-content\/uploads\/2020\/02\/email-newsletter-subscription-system-with-php-mysql-codexworld.png","jetpack_shortlink":"https:\/\/wp.me\/p6bxIh-17b","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.codexworld.com\/wp-json\/wp\/v2\/posts\/4289","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.codexworld.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.codexworld.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.codexworld.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.codexworld.com\/wp-json\/wp\/v2\/comments?post=4289"}],"version-history":[{"count":8,"href":"https:\/\/www.codexworld.com\/wp-json\/wp\/v2\/posts\/4289\/revisions"}],"predecessor-version":[{"id":5931,"href":"https:\/\/www.codexworld.com\/wp-json\/wp\/v2\/posts\/4289\/revisions\/5931"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.codexworld.com\/wp-json\/wp\/v2\/media\/5930"}],"wp:attachment":[{"href":"https:\/\/www.codexworld.com\/wp-json\/wp\/v2\/media?parent=4289"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.codexworld.com\/wp-json\/wp\/v2\/categories?post=4289"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.codexworld.com\/wp-json\/wp\/v2\/tags?post=4289"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}