邮件发送功能实现:
sql语句:
CREATE TABLE ourmail2 (
email_addr varchar(40) NOT NULL,
create_time varchar(20),
PRIMARY KEY (email_addr),
UNIQUE email_addr (email_addr)
);
1.editemail.php
<?
/********************************
*运行两个外部的程序 *
*其作用是定义一些常用的变量 *
*和CSS的显示类 *
*并进行和goldenwind数据库的连接 *
*********************************/
include "header.inc.php";
include "common.inc.php";
?>
<HTML>
<!--html的头定义-->
<HEAD>
<TITLE>
发送邮件
</TITLE>
</HEAD>
<BODY>
<!--显示网页中的大标题-->
<p align=center>
<font size=5 color=red >
发送一个新的邮件
</font>
</p>
<hr color=red>
<?
/********************************
*下面是用户的输入表单 *
*用户在此填写电子邮件的内容 *
*填写完毕后调用发送程序 *
*********************************/
?>
<FORM ACTION="postemail.php" METHOD="post">
<TABLE width=80% border=1 align=center bgcolor=yellow>
<TR class=myBlack bgcolor=green>
<TD colspan=2 height=20 align=center>
<FONT class=myYellow>
请按照要求填写下列各项!
</FONT>
</td>
</TR>
<!--输入昵称-->
<TR class=myBlack>
<TD height=20 width=20% align=center class=myRed>邮件标题:</TD>
<TD height=20 width=60% align=center>
<input type="text" name="subject" size=50>
</TD>
</TR>
<TR class=myBlack>
<TD height=20 width=20% align=center class=myRed>邮件内容:</TD>
<TD height=20 width=60% align=center>
<textarea name="newemail" cols=50 rows=10 wrap=virtual></textarea>
</TD>
</TR>
<!--输入时的选择按钮-->
<TR class=myBlack>
<TD height=30 width=80% colspan=2 align=center class=myRed><BR>
<INPUT type=reset name=Start value=" 重置 " class=myBlack>
<INPUT type=submit name=Ok value=" 发送 " class=myBlack>
</TD>
</TR>
</TABLE>
</FORM>
<!--显示版权等信息,使用的是CSS标记-->
<DIV class=myGreen align=center>
<? echo $COPYRIGHT; ?>
</DIV>
<BR>
</BODY>
</HTML>
2.maillist.php
<?
include "common.inc.php";
if ($Ok)
{
if ($choice=="sub")
{
if (!ereg('^[-!#$%&/'*+//./0-9=?A-Z^_`a-z{|}~]+'.
'@'.
'[-!#$%&/'*+///0-9=?A-Z^_`a-z{|}~]+/.'.
'[-!#$%&/'*+//./0-9=?A-Z^_`a-z{|}~]+$', $myemail))
{
header("Location: login_error.php?error=邮件地址输入错误/n");
}
$email_check = "select email_addr from ourmail
where email_addr = '$myemail'";
$check_result = mysql_query($email_check)
or die("不能执行地e-mail地址的检查。");
$check_num = mysql_num_rows($check_result);
if ($check_num == 0)
{
$create_date = date("Y-m-d");
$sql = "insert into ourmail
values('$myemail', '$create_date')";
@mysql_query($sql) or die ("Can't add this email address.");
header("Location: on_ok.php?log_id=$log_id/n");
}
else
{
header("Location: login_error.php?error=你已经订阅过了!/n");
}
}
else
if ($choice == "unsub")
{
$email_check = "select email_addr from ourmail
where email_addr = '$myemail'";
$check_result = mysql_query($email_check)
or die("不能执行对e-mail地址的检查。");
$check_num = mysql_num_rows($check_result);
if ($check_num == 0)
{
header("Location: login_error.php?error=输入的e-mail不存在。/n");
}
else
{
$email_dele = "delete from ourmail
where email_addr = '$myemail'";
@mysql_query($email_dele) or die ("Can't delete this email address");
header("Location: on_ok.php?log_id=$log_id/n");
}
}
}
include "header.inc.php";
?>
<BODY>
<FORM METHOD="POST" ACTION="<? echo $PHP_SELF; ?>">
<TABLE width=60% border=0 align=center bgcolor=yellow>
<TR bgcolor=green>
<TD height=20 align=center>
<FONT class=myYellow>
欢迎加入社区的邮件列表!
</FONT>
</TD>
</TR>
<TR >
<TD height=50 align=center>您的邮件地址:
<INPUT type=text name=myemail size=40 maxlength=40>
</TD>
</TR>
<TR>
<td width="50%" align=center class=myRed>
<input type="radio" name="choice" value="sub" checked>
订  阅      
         
<input type="radio" name="choice" value="unsub" >
退  定   </td>
</TR>
<TR>
<TD height=20 align=center>
<INPUT type=submit name=Ok value=" 提交 " class=myBlack>
<INPUT type=reset name=Back value=" 还原 " class=myBlack>
</TD>
</TR>
</TABLE>
</FORM>
<DIV class=myGreen align=center>
<? echo $COPYRIGHT; ?>
</DIV>
<BR>
</BODY>
</HTML>
3. postemail.php
<?
//如果邮件标题或者正文为空则重新输入
if (($subject =="") || ($newemail == ""))
{
header("Location: http://localhost/editemail.php");
exit;
}
else
{
// 连接数据库
$db = mysql_connect("localhost", "root")
or die ("不能连接。");
//选择社区数据库
mysql_select_db("goldenwind", $db) or die ("不能选择数据库。");
//查找邮件地址
$sql = "select email_addr from ourmail";
$res = mysql_query($sql) or die("不能得到邮件地址。");
//定义附加的邮件头
$headers = "From: fecit@fecit.com/n";
/********************************
*使用while循环来多次发送邮件 *
*每次从数据库中取一个地址 *
*然后调用mail函数来发送邮件 *
*********************************/
while ($row = mysql_fetch_array($res))
{
//取出下一个邮件地址
$email_addr = $row[0];
//发送电子邮件
mail("$email_addr", "$subject", $newemail, $headers);
}
//返回成功信息
header("Location: on_ok.php?ok_info=邮件成功发送/n");
}
?>
4. search_mail.php
<html>
<!--html的头定义-->
<head>
<title>
查看邮件列表
</title>
</head>
<body>
<?
/********************************
*运行两个外部的程序 *
*其作用是定义一些常用的变量 *
*和CSS的显示类 *
*并进行和goldenwind数据库的连接 *
*********************************/
include "header.inc.php";
include "common.inc.php";
//从数据库中获取邮件列表的数目
$count=mysql_query("select count(*) from ourmail where email_addr like '%@%'");
$count_row = mysql_fetch_row($count);
?>
<!--显示网页中的大标题-->
<p align=center>
<strong>
<FONT size=5 color=red>
邮件地址列表
</FONT>
</strong>
</p>
<hr color=red>
<?
/********************************
*邮件列表查询的结果放在 *
*表格中。表格有三项: *
*序号、邮件地址和创建时间 *
*********************************/
//显示表格的题头
?>
<table border=1>
<TABLE width=80% border=1 align=center bgcolor=yellow>
<TR class=myBlack bgcolor=green>
<TD colspan=3 height=20 align=center>
<FONT class=myYellow>
<? echo "共有".$count_row[0]."条纪录/n"; ?>
</FONT>
</td>
</TR>
<?
//循环显示邮件列表的每一项
$i=1;
$result=mysql_query("SELECT * FROM ourmail where email_addr like '%@%'");
while ($row=mysql_fetch_array($result))
{ echo "<tr><td height=20 width=5% align=center class=myBlue>"
.$i."</td>/n";
echo "<td height=20 width=20% align=center class=myRed>"
.$row["email_addr"]."</td>/n";
echo "<td height=20 width=20% align=center class=myRed>"
.$row["create_time"]."</td></tr>/n";
$i++;
}
echo "</table>/n";
?>
<!--显示版权信息-->
<DIV class=myGreen align=center>
<? echo $COPYRIGHT; ?>
</DIV>
</body>
</html>