php写的邮件订阅与邮件发送简单实例

一,新建数据库表:email,如下:

mysql> use adamli;
Database changed
mysql> create table email(id int(4) not null auto_increment primary key,email va
rchar(150) unique not null);
Query OK, 0 rows affected (0.01 sec)

mysql> desc email;
+-------+--------------+------+-----+---------+----------------+
| Field | Type         | Null | Key | Default | Extra          |
+-------+--------------+------+-----+---------+----------------+
| id    | int(4)       | NO   | PRI | NULL    | auto_increment |
| email | varchar(150) | NO   | UNI |         |                |
+-------+--------------+------+-----+---------+----------------+
2 rows in set (0.00 sec)

二:数据库连接及email检查包含文件:

<?php

//连接数据库与检测邮件地址的包含文件:
function db_Connect()
{
global $conn;
$conn = mysql_connect("localhost","root","") or die("Connect mysql fail!");
mysql_select_db("adamli");
mysql_query("set names utf8");
}
function checkEmail($email)
{
global $conn;
global $result;
$sql = "select * from email where email='".$email."'";
$result = mysql_query($sql);
}
?>

将这些代码写于文件:include.php中;

三:邮件订阅与取消订阅文件:

<?php
include_once("include.php");
if(!$_POST)
{

//$display为页面要显示的内容,没有点击提交前显示此模块内容:
$display = "<form action=\"index.php\" method=\"post\" />
<h1>Subscribe/Unsubscribe to a Mailling List</h1>
<p>Your E-Mail Address</p>
<P><input type=\"text\" name=\"email\" /></p>
<p><input type=\"radio\" name=\"check_sub\" value=\"subscribe\" checked />subscribe<input type=\"radio\" name=\"check_sub\" value=\"unsubscribe\" />unsubscribe
<p><input type=\"submit\" name=\"sub\" value=\"Submit Form\" /></p>
</form>";

}
else
{

//email为空时提示不能为空,重新访问页面文件停止程序继续执行:
if(trim($_POST['email'])=="")
{
echo "<script>alert('Please input email address!');window.location.href='index.php'</script>";
exit;
}
else
{
db_Connect();
checkEmail($_POST['email']);
if($_POST['check_sub']=="subscribe")
{

//如果是订阅的:
if(mysql_num_rows($result)<1)
{

//如果用户输入的email在数据库中没记录的,将用户输入的email地址插入数据库中,成功页面显示Subdecribe successfully!
$sql = "insert into email values('','$_POST[email]')";
mysql_query($sql) or die("Add a record into mysql fail!");
$display = "Subdecribe successfully!";
mysql_free_result($result);
mysql_close($conn);
}
else
{

//如果客户输入的email地址已在数据库中存在的,提示客户已经订阅,释放mysql_query占用的内存,关闭数据库:
$display = "You're already subscribed!";
mysql_free_result($result);
mysql_close($conn);
}
}
if($_POST['check_sub']=="unsubscribe")
{

//如果是要取消订阅的:
if(mysql_num_rows($result)<1)
{

//如果用户输入的email地址不存在于数据库email表中的,提示用户还没订阅,不能取消订阅:
$display = "Coudn't find your email address!";
mysql_free_result($result);
mysql_close($conn);
}
else
{

//取消订阅即将用户输入的email与数据库表中对应的email的该条记录删除:
$sql = "delete from email where email='".$_POST['email']."'";
mysql_query($sql) or die("Delete email fail!");
$display = "<p>You're unscribed!</p>";
mysql_free_result($result);
mysql_close($conn);
}
}
}
}
echo $display;
?>

将这些代码保存为:index.php,此文件主要是邮件订阅与取消订阅的功能;

四:邮件发送:(主要是mail($email,$subject,$message)的使用,注意你的服务器要有邮件发送功能,并设置好php.ini文件相关内容,否则不能正常发送)

<?php
include_once("include.php");
if(!$_POST)
{
echo "<form action=\"sendmail.php\" method=\"post\">
<h1>Send a Newsletter</h1>
<p><strong>Subject</strong></p>
<p><input type=\"text\" name=\"subject\" /></p>
<p><strong>Mail Body</strong></p>
<p><textarea name=\"message\" rows=\"5\" cols=\"60\"></textarea></p>
<p><input type=\"submit\" name=\"sub\" value=\"Send mail\" /></p>
</form>";
}
elseif($_POST)
{
if($_POST['subject']==""||$_POST['message']=="")
{
echo "<script>alert('Please input your subject and message!');window.location.href='sendmail.php';</script>";
exit;
}
db_Connect();
$sql = "select email from email";
$query = mysql_query($sql) or die("query fail,can not get email from mysql!");
while($result = mysql_fetch_object($query))
{
set_time_limit(0);
$mail = $result->email;

//发送邮件:
mail($mail,$_POST['subject'],$_POST[message]) or die("mail to ".$result->email." fail!<br />");
echo "newsletter sent to: ".$mail."<br />";

}
mysql_free_result($query);
mysql_close($conn);
}
?>

将这些代码保存为sendmail.php,此文件主要是实现邮件发送的功能;

转载于:https://my.oschina.net/adamboy/blog/32165

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值