简体   繁体   English

php电子邮件批准

[英]php email approval

I have a small web app where people can send a message via email to a group. 我有一个小型网络应用程序,人们可以在其中通过电子邮件向群组发送消息。 Because of spam I will have to make an approval procedure. 由于垃圾邮件,我将必须进行批准程序。

The messages are being sent via PHP. 消息是通过PHP发送的。 How am I doing so I have to accept the message before its send to an email that forward it to the group? 我该怎么做,我必须先接受该消息,然后再将其发送到将其转发给该组的电子邮件中?

My PHP: 我的PHP:

<?php 
$errors = '';
$myemail = 'whatever@gmail.com';//<-----Put Your email address here.
if(empty($_POST['name'])  || 
   empty($_POST['email']) || 
   empty($_POST['message']))
{
    $errors .= "\n FEJL: Alle felter skal udfyldes";
}

$name = $_POST['name']; 
$email_address = $_POST['email']; 
$message = $_POST['message']; 
$email_subject = "Form request";
$times = $_POST["timeslots"];

if (!preg_match(
"/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i", 
$email_address))
{
    $errors .= "\n FEJL: Ugyldig email adresse";
}
$strTimes = implode($times);
if( empty($errors))
{
$to = $myemail; 
$email_subject = "$message \n ";
$email_body = "\n Code: $strTimes \n Navn: $name \n Email: $email_address \n"; 

$headers = "From: $email_address\n"; 
$headers .= "Reply-To: $email_address";

mail($to,$email_subject,$email_body,$headers);
//redirect to the 'thank you' page
header('Location: thx.html');
} 
?>

ok so this is a fair size project in itself if you want to use a data queue. 好的,因此,如果您要使用数据队列,那么它本身就是一个相当大的项目。

ASSUMING you want to use a database you will need to know the basics of setting one up, how to use and setup tables etc. (as long as you have access to a database anyway). 假设您要使用数据库,则需要了解设置数据库的基础知识,如何使用和设置表等(只要您仍然可以访问数据库)。

Here is some connection code: 这是一些连接代码:

change peter and abc123 to whatever username and password your database has associated with it. peterabc123更改为数据库与之关联的任何用户名和密码。

$con = mysql_connect("localhost","peter","abc123");
if (!$con){
  die('Could not connect: ' . mysql_error());
}
mysql_select_db("my_db");

I will give you some sample code for inserting rows to the database, selecting rows from the database, acting on that info and deleting rows from the database. 我将为您提供一些示例代码,用于将行插入数据库,从数据库中选择行,对该信息执行操作并从数据库中删除行。

Ok lets start with the mail submission: 好,让我们从邮件提交开始:

This section near the bottom of your code is the data we need to manipulate 代码底部附近的部分是我们需要处理的数据

$to = $myemail; 
$email_subject = "$message \n ";
$email_body = "\n Code: $strTimes \n Navn: $name \n Email: $email_address \n"; 
$headers = "From: $email_address\n"; 
$headers .= "Reply-To: $email_address";

So assuming you have a mysql database connection with a table setup in the database this is how you would insert it: (please note this is a basic insert query) 因此,假设您在数据库中建立了带有数据库设置的mysql数据库连接,这就是您将其插入的方式:(请注意,这是一个基本的插入查询)

$query = "INSERT INTO queuemails (to, subject, body, headers) VALUES ('" . $to . "', '" . $email_subject . "', '" . $email_body . "', '" . $headers . "')";
mysql_query($query);

Ok so those 2 lines would be added BELOW your set variables. 好了,所以那些2线将低于设定的变量加入。

You then need to provide a method of approval page. 然后,您需要提供一种批准方法页面。

This can be done on the same page but you have to seperate out your mail() function from the rest of the script. 可以在同一页面上完成此操作,但是必须将mail()函数与脚本的其余部分分开。

Ok so here is a select script now to be able to VIEW your queue for approval. 好的,这是一个选择脚本,现在可以查看您的队列以进行批准。 Please note that I have added an auto increment column to the table that stored your queued mail. 请注意,我已向存储您排队的邮件的表中添加了一个自动递增列。 This is to be able to select a line in the table more easily as is generates a unique number for that line of data. 这样可以更轻松地在表中选择一行,因为它会为该行数据生成唯一编号。 This column is called mid (standing for "mail identity"). 此列称为“ mid (代表“邮件身份”)。

ok so here is the selection script: 好的,这是选择脚本:

$query = "SELECT * FROM queuemail"; //this is only good if you know you wont get millions else you need to limit it
//limited select:
//$query = "SELECT * FROM queuemail LIMIT 0,10"; //selects the first 10
while($m = mysql_fetch_assoc(mysql_query($query))){
  echo $m['to'] . " ";
  echo $m['subject'] . " ";
  echo '<a href=approve.php?mid=' . $m['mid'] . '>Approve</a><br>';
}

Then finally to clean up afterwards, after you have used your mail() function you should delete the line from the database that you have sent. 后来终于清理之后,你已经使用了您之后 mail()函数,你应该删除已发送的数据库行。

Here is the code (including the $_GET variable, this is an unsafe method but is sufficient to display the code you would be using). 这是代码(包括$_GET变量,这是一种不安全的方法,但足以显示您将使用的代码)。

mail(); //data can be added either from a new select statement or from POSTING it with the form
$query = "DELETE FROM mailqueue WHERE mid='" . $_GET['mid'] . "'";
mysql_query($query);
echo 'Your mail has been sent and deleted from the queue';

Hope this helps. 希望这可以帮助。

You can add timestamps to another column in the database automatically so that you can verify spam posting with something like: 您可以将时间戳自动添加到数据库中的另一列,以便可以通过以下方式验证垃圾邮件的发布:

if($_SERVER['REQUEST_TIME'] > ($oldtimeofpost + 100)){ //time is in seconds
  //do something
}
else{
  //warning
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM