数据库连接文件inclulde1.php
<?php
/**
* Created by PhpStorm.
*/
function doDB()
{
global $conn;
$conn = mysqli_connect('localhost','root','','php_project01');
if(mysqli_connect_errno())
{
echo '数据库连接失败'.mysqli_connect_error()."<br>";
exit();
}
}
function emailchecker($email)
{
global $conn,$safe_email,$check_result;
$safe_email = mysqli_real_escape_string($conn,$email); //防止sql注入攻击
$sql = "select id from subcribers where email= '$email'";
$check_result = mysqli_query($conn,$sql) or die(mysqli_error($conn));
}
manage.php
<?php
/**
* Created by PhpStorm.
*/
include_once ('include1.php');
if(!$_POST)
{
$display_block = <<< END_OF_BLOCK
<form method="post" action="$_SERVER[PHP_SELF]">
<p><label for="email">邮件地址:</label><br>
<input type="email" id="email" name="email" size="40" maxlength="150"></p>
<fieldset>
<legend>执行动作:</legend>
<input type="radio" id="action_sub" name="action" value="sub" checked="checked">
<label for="action_sub">订阅</label>
<input type="radio" id="action_unsub" name="action" value="unsub">
<label for="action_unsub">取消订阅</label>
</fieldset>
<button type="submit" name="submit" value="submit">提交</button>
<a href="../index.php">回到首页</a>
<a href="sendmymail.php">发送邮件</a>
</form>
END_OF_BLOCK;
}
else if(($_POST) && ($_POST['action'] == 'sub'))
{
if($_POST['email'] == "")
{
header('location:manage.php');
exit;
}
else
{
doDB();
emailchecker($_POST['email']);
if(mysqli_num_rows($check_result) < 1)
{
mysqli_free_result($check_result);
$sql_add = "insert into subcribers(email) values ('$safe_email')";
$result_add = mysqli_query($conn,$sql_add) or die(mysqli_error($conn));
$display_block = "<p>感谢报名参与,已订阅成功!</p>";
mysqli_close($conn);
}
else
{
$display_block = "<p>已经订阅过了!</p>";
}
}
}
else if(($_POST) && ($_POST['action'] == 'unsub'))
{
if($_POST['email'] == "")
{
header('location:manage.php');
exit;
}
else
{
doDB();
emailchecker($_POST['email']);
if(mysqli_num_rows($check_result) < 1)
{
mysqli_free_result($check_result);
$display_block = "<p>没有找到该邮件地址!</p>";
}
else
{
while($row = mysqli_fetch_array($check_result))
{
$id = $row['id'];
$sql_del = "delete from subcribers where id = $id";
$result_del = mysqli_query($conn,$sql_del);
$display_block = "<p>已取消订阅!</p>";
}
}
mysqli_close($conn);
}
}
?>
<html>
<head>
<title>简单邮件列表的订阅与取消订阅操作</title>
</head>
<body>
<h1>简单邮件列表的订阅与取消订阅操作</h1>
<?php echo "$display_block"; ?>
</body>
</html>
sendmyemail.php
<?php
/**
* Created by PhpStorm.
*/
include_once ('include1.php');
require_once ("src/PHPMailer.php"); //必须引入此3个文件,否则出错
require_once ('src/Exception.php');
require_once ('src/SMTP.php');
if(!$_POST)
{
$display_block = <<< end_of_block
<form method="post" action="$_SERVER[PHP_SELF]">
<p><label for="subject">主题:</label><br>
<input type="text" id="subject" name="subject" size="40"></p>
<p><label for="message">邮件内容:</label><br>
<textarea id="message" name="message" rows="10" cols="50"></textarea></p>
<button type="submit" name="submit" value="submit">提交</button>
<a href="manage.php">回到首页</a>
</form>
end_of_block;
}
else if($_POST)
{
if(($_POST['subject'] == "") || ($_POST['message'] == ""))
{
header('Location:sendmymail.php');
exit;
}
doDB();
if(mysqli_connect_errno())
{
echo '数据库连接失败'.mysqli_connect_error()."<br>";
exit();
}
else
{
$sql = 'select email from subcribers';
$result = mysqli_query($conn,$sql) or die(mysqli_error($conn));
$mail_header = "xxx@163.com"; //记得替换
while ($row = mysqli_fetch_array($result))
{
set_time_limit(0);
$email = $row['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$smtp = 'smtp.163.com';
$port = 465; //一般端口都是465
$user_name = 'xxx@163.com'; // 记得替换
$password = 'xxxx'; // 记得替换
$mail = new \PHPMailer\PHPMailer\PHPMailer();
$mail->isSMTP();
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'ssl';
$mail->Host = $smtp;
$mail->Port = $port;
$mail->CharSet = 'utf-8';
$mail->Username = $user_name;
$mail->Password = $password;
$mail->From = $mail_header;
$mail->FromName = $mail_header;
$mail->Subject = $subject;
$mail->Body = $message;
$mail->isHTML(true);
$mail->addAddress($email);
if(!$mail->send())
{
echo "发送错误:".$mail->ErrorInfo."<br>";
}
else
{
@$display_block .= "已成功发送至:$email"."<br>";
}
}
mysqli_free_result($result);
mysqli_close($conn);
}
}
?>
<html>
<head>
<title>发送一封信件</title>
</head>
<body>
<h1>发送一封信件</h1>
<?php echo $display_block; ?>
</body>
</html>
数据库文件
-- phpMyAdmin SQL Dump
-- version 4.8.4
-- https://www.phpmyadmin.net/
--
-- 主机: 127.0.0.1
-- 生成日期: 2019-05-27
-- 服务器版本: 10.1.37-MariaDB
-- PHP 版本: 7.3.1
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET AUTOCOMMIT = 0;
START TRANSACTION;
SET time_zone = "+00:00";
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;
--
-- 数据库: `php_project01`
--
CREATE DATABASE IF NOT EXISTS `php_project01` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
USE `php_project01`;
-- --------------------------------------------------------
--
-- 表的结构 `subcribers`
--
DROP TABLE IF EXISTS `subcribers`;
CREATE TABLE `subcribers` (
`id` int(11) NOT NULL,
`email` varchar(150) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
--
-- 转储表的索引
--
--
-- 表的索引 `subcribers`
--
ALTER TABLE `subcribers`
ADD PRIMARY KEY (`id`),
ADD UNIQUE KEY `email` (`email`);
--
-- 在导出的表使用AUTO_INCREMENT
--
--
-- 使用表AUTO_INCREMENT `subcribers`
--
ALTER TABLE `subcribers`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=28;
COMMIT;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;