With the widespread use of email, many businesses and individuals rely on email for promotion and marketing. However, sometimes recipients may not be interested in the emails or feel they are receiving too many of them. To better serve our users, we need to provide a feature that allows users to easily unsubscribe from emails. In this article, I will introduce how to implement an email unsubscription feature using PHP and PHPMailer.
First, we need a user database to store users' subscription statuses. We can use a MySQL database and create a table named "subscribers" to store the user ID, email address, and subscription status. Here's a simple SQL query to create the table:
CREATE TABLE subscribers ( id INT(11) AUTO_INCREMENT PRIMARY KEY, email VARCHAR(255) NOT NULL, subscribed TINYINT(1) NOT NULL DEFAULT '1' );
Next, we need to create an unsubscription page where users can enter their email address and choose whether to unsubscribe. Below is an example of an HTML form for this purpose:
<form action="unsubscribe.php" method="post"> <label for="email">Email Address:</label> <input type="email" id="email" name="email" required> <label for="unsubscribe">Unsubscribe from emails:</label> <input type="checkbox" id="unsubscribe" name="unsubscribe" value="1"> <input type="submit" value="Submit"> </form>
On the unsubscription page, we need to write a PHP script to handle the user's input. First, we need to connect to the database, then retrieve the user's email address and unsubscription choice from the POST request.
<?php // Connect to the database $host = "localhost"; $username = "your_username"; $password = "your_password"; $database = "your_database"; $connection = mysqli_connect($host, $username, $password, $database); // Get user input $email = $_POST['email']; $unsubscribe = isset($_POST['unsubscribe']) ? 1 : 0; // Update subscription status $query = "UPDATE subscribers SET subscribed = $unsubscribe WHERE email = '$email'"; mysqli_query($connection, $query); // Close the database connection mysqli_close($connection); ?>
Next, we need to modify the email sending code to check the user's subscription status before sending an email. In the PHPMailer code, we can add a check before sending the email as follows:
<?php // Connect to the database $host = "localhost"; $username = "your_username"; $password = "your_password"; $database = "your_database"; $connection = mysqli_connect($host, $username, $password, $database); // Get the recipient's email address $to = "receiver_email@example.com"; // Query the subscription status of the recipient $query = "SELECT subscribed FROM subscribers WHERE email = '$to'"; $result = mysqli_query($connection, $query); $row = mysqli_fetch_assoc($result); // If the recipient has unsubscribed, do not send the email if ($row['subscribed'] == 0) { exit; } // Code to send the email // ... ?>
By combining PHP and PHPMailer, we can easily implement an email unsubscription feature. First, we need a user database to store users' subscription statuses; then, we create an unsubscription page where users can enter their email address and choose whether to unsubscribe; finally, we modify the email sending code to check the recipient's subscription status before sending the email. By following this process, we can provide better service and respect the user's wishes.