Home > Article > Backend Development > PHP email blacklist: block or filter emails from specific mailboxes.
PHP Email Blacklist: Block or filter emails from specific mailboxes
In the Internet era, email has become an indispensable part of people's daily life and work. . However, as spam and malicious emails continue to increase, we sometimes experience unnecessary harassment and annoyance. In order to solve this problem, we can use the PHP programming language to implement an email blacklist system to block or filter emails from specific mailboxes.
First, we need a database to store the email addresses in the blacklist. You can use MySQL or other relational databases to create a table named "email_blacklist", which contains a field "email" for storing email addresses in the blacklist.
The following is a simple MySQL table structure example:
CREATE TABLE email_blacklist ( id INT AUTO_INCREMENT PRIMARY KEY, email VARCHAR(255) NOT NULL );
Next, we use PHP code to implement a simple email blacklist system. First, connect to the database server and select the database:
<?php $servername = "localhost"; $username = "your_username"; $password = "your_password"; $dbname = "your_database"; $conn = new mysqli($servername, $username, $password, $dbname); if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } ?>
Then, we write a function to check whether the email address is in the blacklist:
<?php function checkEmailBlacklist($email) { global $conn; $sql = "SELECT * FROM email_blacklist WHERE email = '$email'"; $result = $conn->query($sql); if ($result->num_rows > 0) { return true; // 邮箱地址在黑名单中 } else { return false; // 邮箱地址不在黑名单中 } } ?>
Next, we add before sending the email A piece of code to check whether the email address is in the blacklist:
<?php $to = "recipient@example.com"; $subject = "Example Email"; $body = "This is an example email."; if (!checkEmailBlacklist($to)) { // 发送邮件 $headers = "From: sender@example.com "; $headers .= "Reply-To: sender@example.com "; if (mail($to, $subject, $body, $headers)) { echo "Email sent successfully."; } else { echo "Failed to send email."; } } else { echo "Email blocked."; } ?>
The above code will first check whether the recipient's email address is in the blacklist. If it is in the blacklist, it will prevent the email from being sent, otherwise it will continue to send the email. .
When receiving an email, we can also use a similar method to check whether the sender's email address is in the blacklist and decide whether to receive the email.
It should be noted that the above code is only an example. In actual applications, more detailed filtering and inspection of email content may be required, as well as more complex logic processing.
Summary: Through the PHP programming language, we can easily implement an email blacklist system to block or filter emails from specific mailboxes. This reduces the likelihood that we receive spam and malicious emails and provides better email filtering and management capabilities.
The above is the detailed content of PHP email blacklist: block or filter emails from specific mailboxes.. For more information, please follow other related articles on the PHP Chinese website!