Email Subscription with PHP and HTML
Introduction
This PDF shows how to create a basic email subscription form using HTML and PHP. When a visitor submits
their email, a confirmation message is sent to their inbox.
Technologies Used:
- HTML for the form
- PHP for backend processing and sending email
Make sure to replace 'yourname@yourdomain.com' with your real email address. Also, ensure that your
server supports PHP mail functions or configure SMTP on localhost.
HTML Form Code
<form action="subscribe.php" method="post">
<input type="email" name="email" placeholder="Enter your email" required>
<button type="submit" name="submit">Subscribe</button>
</form>
PHP Script (subscribe.php)
<?php
if (isset($_POST['submit'])) {
$email = $_POST['email'];
$to = $email;
$subject = "Subscription Confirmation";
$message = "Thank you for subscribing! You'll now receive updates from us.";
$headers = "From: yourname@yourdomain.com\r\n";
$headers .= "Reply-To: yourname@yourdomain.com\r\n";
$headers .= "Content-Type: text/plain; charset=UTF-8\r\n";
Email Subscription with PHP and HTML
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
if (mail($to, $subject, $message, $headers)) {
echo " Subscription email sent to $email";
} else {
echo " Failed to send email. Try again later.";
} else {
echo " Invalid email format.";
?>
Notes
- This script uses PHP's built-in mail() function.
- It does not store emails in a database.
- To use on localhost (XAMPP/WAMP), configure sendmail or use PHPMailer.
- For production, make sure your hosting server allows email sending.