Email Registration: Users submit an email via a web form (index.php), receive a 6-digit verification code, and verify it to subscribe. Unsubscription: Users can unsubscribe via a web form (unsubscribe.php), receiving a verification code to confirm. Daily Comic Delivery: A scheduled task (cron.php) sends a random XKCD comic to subscribers daily. Local Email Testing: MailHog captures emails for testing without sending to real inboxes. No Database: Registered emails are stored in registered_emails.txt. HTML Emails: Emails use specified HTML formats with From: [email protected].
Project Requirements
PHP 8.3 with mail() function. Web server (XAMPP’s Apache). Forms always visible with correct name and id attributes. Emails in specified HTML formats: Verification:
Your verification code is: code
Unsubscribe:To confirm un-subscription, use this code: code
Comic:Daily task automation (Task Scheduler on Windows, replacing CRON). All code in src/ directory.
File Structure The project resides in the src/ directory:
index.php: Handles email registration and verification forms. Users submit an email, receive a verification code, and verify it to subscribe. unsubscribe.php: Manages unsubscription. Users submit an email, receive a verification code, and confirm to unsubscribe. functions.php: Contains core logic: generateVerificationCode(): Generates a 6-digit code. registerEmail(): Adds an email to registered_emails.txt. unsubscribeEmail(): Removes an email from registered_emails.txt. sendVerificationEmail(): Sends verification code emails. verifyCode(): Validates verification codes using sessions. fetchAndFormatXKCDData(): Fetches and formats XKCD comic data. sendXKCDUpdatesToSubscribers(): Sends comic emails to subscribers.
cron.php: Executes sendXKCDUpdatesToSubscribers() to send daily comics. setup_task.ps1: PowerShell script to configure a daily Task Scheduler job for cron.php. registered_emails.txt: Stores subscribed email addresses (created automatically).
Prerequisites
Windows: Tested on Windows 10/11. XAMPP: Provides Apache and PHP 8.3. PHP 8.3: Included in XAMPP or installed manually. MailHog: Local email server for testing. Git: For version control and submission. Internet Access: To fetch XKCD comics.
Setup Instructions Follow these steps to set up and run the project on Windows.
- Install XAMPP
Download XAMPP from https://www.apachefriends.org/. Install to C:\xampp (default path). Start XAMPP Control Panel and verify Apache runs (port 80).
- Install PHP 8.3 (if not using XAMPP)
If not using XAMPP, download PHP 8.3 from https://windows.php.net/download/. Extract to C:\php. Add C:\php to the system PATH. Verify: php --version (should show PHP 8.3.x).
- Install MailHog
Download MailHog for Windows from https://github.com/mailhog/MailHog/releases (e.g., MailHog_windows_amd64.exe). Save to C:\MailHog\MailHog.exe. Run MailHog:cd C:\MailHog MailHog.exe
Access the UI at http://localhost:8025. Keep MailHog running during testing.
- Configure PHP for Email Sending
Open C:\xampp\php\php.ini (or C:\php\php.ini if manual PHP). Update the [mail function] section:[mail function] SMTP=localhost smtp_port=1025 sendmail_from=[email protected] mail.log=C:\xampp\php\logs\mail.log
Enable extension=openssl (uncomment if needed). Save and restart Apache (XAMPP Control Panel: Stop/Start Apache).
- Place Project Files
Create C:\xampp\htdocs\project-root\src. Copy the following files to C:\xampp\htdocs\project-root\src: index.php unsubscribe.php functions.php cron.php setup_task.ps1
Ensure files match the provided code (with debugging enabled for testing). Set permissions:icacls C:\xampp\htdocs\project-root\src /grant Users:F
- Configure Task Scheduler
Update setup_task.ps1 to use the correct PHP path:$PhpPath = "C:\xampp\php\php.exe"
Run as Administrator:cd C:\xampp\htdocs\project-root\src .\setup_task.ps1
Verify in Task Scheduler (taskschd.msc): Task: XKCDDailyComic Trigger: Daily at 12:00 AM Action: Run C:\xampp\php\php.exe with C:\xampp\htdocs\project-root\src\cron.php
- Verify Session Storage
In php.ini, ensure:session.save_path="C:\xampp\tmp"
Grant permissions:icacls C:\xampp\tmp /grant Users:F
How to Run the Project Follow these steps to run and test the application.
- Start Services
Apache: Start in XAMPP Control Panel. MailHog:cd C:\MailHog MailHog.exe
- Test Email Registration
Go to http://localhost/project-root/src/index.php. Verify forms: Email: , Verification: ,
Enter an email (e.g., [email protected]), click “Submit.” Check MailHog (http://localhost:8025): Subject: Your Verification Code Body:
Your verification code is: 123456
Enter the code, click “Verify.” Confirm: “Email [email protected] successfully registered!” Check registered_emails.txt contains the email.
- Test Unsubscription
Go to http://localhost/project-root/src/unsubscribe.php. Verify forms: Unsubscribe: ,
Verification: ,Enter the registered email, click “Unsubscribe.” Check MailHog: Subject: Confirm Un-subscription Body:
To confirm un-subscription, use this code: 654321
Enter the code, click “Verify.” Confirm: “Email [email protected] successfully unsubscribed!” Check registered_emails.txt is empty or excludes the email.
- Test Comic Sending
Register an email. Run cron.php manually:cd C:\xampp\htdocs\project-root\src C:\xampp\php\php.exe cron.php
Check MailHog: Subject: Your XKCD Comic Body:
alt="XKCD Comic">Click the unsubscribe link to confirm it opens unsubscribe.php.
- Test Task Scheduler
Open Task Scheduler (taskschd.msc). Right-click “XKCDDailyComic,” select “Run.” Check MailHog for a comic email. The task runs automatically at midnight daily.
Troubleshooting
No Emails in MailHog: Verify MailHog is running (http://localhost:8025). Check php.ini settings (smtp_port=1025). Review C:\xampp\php\logs\php_error_log and mail.log. Test with test_mail.php:Test email from PHP
"; $headers = "From: [email protected]\r\nContent-Type: text/html; charset=UTF-8\r\n"; if (mail($to, $subject, $body, $headers)) { echo "Email sent!"; } else { echo "Email failed."; } ?>Session Issues: Ensure C:\xampp\tmp is writable. Clear sessions: del C:\xampp\tmp\sess_*.
File Permissions: Reapply: icacls C:\xampp\htdocs\project-root\src /grant Users:F.
Task Scheduler Fails: Verify $PhpPath in setup_task.ps1. Check Task Scheduler’s “History” tab.
Comic Fetch Fails: Ensure allow_url_fopen=On in php.ini. Test: file_get_contents('https://xkcd.com/1/info.0.json').
Notes
Windows Adaptation: Task Scheduler (setup_task.ps1) replaces CRON due to the Windows environment. This is equivalent to the required setup_cron.sh for Linux. MailHog: Used for local testing to capture emails without sending to real inboxes (e.g., @gmail.com). Debugging: Files include error_log statements for troubleshooting. Remove before production if needed. Submission: Only src/ files are submitted. No modifications outside src/.
Submission Instructions
Clone the repository:cd C:\Users\YourUsername\Documents git clone https://github.com/your-repo/xkcd-project.git cd xkcd-project
Create a branch:git checkout -b feature/xkcd-windows
Copy src/:xcopy C:\xampp\htdocs\project-root\src C:\Users\YourUsername\Documents\xkcd-project\src /E /H /C /I
Commit and push:git add src/ git commit -m "Implemented XKCD system for Windows with Task Scheduler" git push origin feature/xkcd-windows
Raise a pull request (PR) against main with description:Implemented XKCD comic subscription system on Windows using PHP 8.3, XAMPP, and Task Scheduler (setup_task.ps1) due to lack of CRON. Emails tested with MailHog (localhost:1025). All requirements met:
- Forms always visible with correct attributes.
- HTML email formats as specified.
- Uses registered_emails.txt.
- Daily task via Task Scheduler.
License This project is for educational purposes and follows the guidelines of the XKCD comic subscription challenge.