php 邮件订阅,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检查包含文件:

//连接数据库与检测邮件地址的包含文件:

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中;

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

include_once("include.php");

if(!$_POST)

{

//$display为页面要显示的内容,没有点击提交前显示此模块内容:

$display = "

Subscribe/Unsubscribe to a Mailling List

Your E-Mail Address

subscribeunsubscribe

";

}

else

{

//email为空时提示不能为空,重新访问页面文件停止程序继续执行:

if(trim($_POST['email'])=="")

{

echo "";

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 = "

You're unscribed!

";

mysql_free_result($result);

mysql_close($conn);

}

}

}

}

echo $display;

?>

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

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

include_once("include.php");

if(!$_POST)

{

echo "

Send a Newsletter

Subject

Mail Body

";

}

elseif($_POST)

{

if($_POST['subject']==""||$_POST['message']=="")

{

echo "";

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!
");

echo "newsletter sent to: ".$mail."
";

}

mysql_free_result($query);

mysql_close($conn);

}

?>

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值