Create a simple PHP application that accepts a visitor’s email address and emails them a GitHub timeline update every five minutes.
Approach to build this application
- Using Model View Controller (MVC) architecture for better organization and sepration of concerns.
- MySQL database is used for persisting data. Using
PDO
for interaction with database. Only usingprepared statements
for preventingSQL injection
.
Project Workflow Overview
User Interaction
- User visits
https://host/
. - User enters their email.
- User clicks the subscribe button.
- Form data (email) is submitted via
POST
method tohttps://host/subscribe
.
Subscription Process
POST
method tohttps://host/subscribe
action:- Email is validated.
- verification token is generated.
- Email and verification token is inserted in to
verify
table. - Email with a verification link (
https://host/verify?token=abc
) is sent to the user.
Verification Process
- User clicks on the verification link.
GET
method tohttps://host/verify?token=abd
.- Verification token is validated.
- Token is searched in
verify
table. - If token is found.
- Unsubscribe token is generated.
- Email and unsubscribe token is inserted in to the
subscribed
table. - Row is deleted from the
verify
table.
Email Delivery
- All subscribed emails are fetched form
subscribed
table. https://github.com/timeline
is fetched and returnsXML
data.XML
data is parsed and required fields are extracted.- Message is created using email template for each email.
mail()
is used to send the each email.
Unsubscription Process
- Each email contains an unsubscribe link at the bottom (
https://host/unsubscribe?token=abc
). - If user clicks the unsubscribe link.
subscribed
table is searched for unsubscribe token.- If found then the row is deleted from
subscribed
table.
Database Schemas
verify
table
CREATE TABLE verify (
id INT(10) AUTO_INCREMENT PRIMARY KEY,
email VARCHAR(50) NOT NULL UNIQUE,
token varchar(50) NOT NULL
)
subscribed
table
CREATE TABLE subscribed (
id INT(10) AUTO_INCREMENT PRIMARY KEY,
email VARCHAR(50) NOT NULL UNIQUE,
token varchar(50) NOT NULL
)
Project File Structure
.
└── app/
├── public/
│ └── index.php
├── sql/
│ └── local.sql
├── src/
│ ├── Controllers/
│ │ ├── HomeController.php
│ │ └── SubscriptionController.php
│ ├── Models/
│ │ ├── Datbase.php
│ │ ├── Mail.php
│ │ └── Subscription.php
│ ├── Views/
│ │ ├── 404.html
│ │ ├── Home.php
│ │ └── Message.php
│ ├── cronjobs/
│ │ ├── cron_email_template.php
│ │ ├── send_emails.php
│ │ └── xmlhelper.php
│ ├── templates/
│ │ └── email_templates.php
│ ├── autoloader.php
│ ├── Controller.php
│ ├── Router.php
│ └── routes.php
├── .gitignore
├── phpcs.xml
└── README.md
Leave a Reply