php寫的郵件訂閱與郵件發送簡單實例

一,新建數據庫表:email,以下:php

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

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)
sql

二:數據庫鏈接及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);
}
?>
post

將這些代碼寫於文件:include.php中;fetch

三:郵件訂閱與取消訂閱文件:spa

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

//$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>";
ip

}
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,此文件主要是實現郵件發送的功能;

相關文章
相關標籤/搜索