表格自动发送电子邮件

时间:2013-05-14 15:49:40

标签: php mailto

我正在创建一个数据库系统,用户可以在表单中添加详细信息,然后使用php添加到数据库中。我想要做的是发送一封确认电子邮件,该电子邮件还会发送表格中的一些细节。所以,当我添加一个学生的详细信息时,例如我会发送一封电子邮件,上面写着“学生叫”Bob Dabilda“已被”agentname“添加到您的数据库中” - 我没有问题做代理名称部分,因为我可以发送会话名称,我有问题是发送学生姓名。任何帮助赞赏。以下是add student php部分的一个版本。添加学生表格是有效的,电子邮件是单独工作的,但我希望在没有代理人点击“发送电子邮件”或类似内容的按钮的情况下发送电子邮件。数据库是mysql。

require "dbc.php";
session_start();

if(isset($_POST['submit']))
{
$sname = mysql_real_escape_string($_POST['shostfamilyid']);

if($sname && $ssurname && $sgender && $saddress && $sdob && $sblood && $spassport && $shobby1 && $shobby2 && $schildren && $sanimals)
{

    $query = mysql_query("INSERT INTO student VALUES ('','$sname','$ssurname','$sgender','$saddress','$semail','$sdob','$stelephone','$sdiet','$sallergy','$smed','$sblood','$spassport','$shobby1','$shobby2','$shobby3','$ssiblings','$schildren','$srequests','$sanimals','$agent','')");
                header("location: successstudent2.php");
}
    else
    { 
       echo "Check All Required Fields Are Completed"; 
    }
}

这是我工作的电子邮件脚本的副本

session_start();

error_reporting(0);

if($_SESSION['username'])
{

$to = "from@from.com";
$from = "receive <to@to.com>";

$name = $_POST['webmaster'];
$email = $_POST['email@email.com'];
$msg = $_POST['msg'];
//generic subject
$subject = "Student Adding";
//header
$header = 'From: '. $from . "\r\n" . 'Content-Type: text/html; charset=ISO-8859-1';
//email
$htmlEmail = "
<html>
<head>
</head>
<body>
<font face='Arial' style='font-size:14px;'></font><br>
<font face='Arial' style='font-size:14px;'>A Student was added by ".$_SESSION['username']."   
</font><br><br>
</body>
</html>";
//php mail function
mail($to,$subject,$htmlEmail,$header);
header('Location:members.php'); //forwards user to members.php }

任何帮助将不胜感激。谢谢你!

2 个答案:

答案 0 :(得分:0)

您需要仔细检查下面传递的所有变量名称和数据,因为这只是一个示例,并且可能无法正常运行。

在公共文件中创建一个函数,例如(比如这是“functions.php”):

<?php
function send_email ($to, $from, $subject, $body) {
/* you'll want to modify the above variables to match what you need in the function */

$to = "from@from.com";
$from = "receive <to@to.com>";

$name = $_POST['webmaster'];
$email = $_POST['email@email.com'];
$msg = $_POST['msg'];
//generic subject
$subject = "Student Adding";
//header
$header = 'From: '. $from . "\r\n" . 'Content-Type: text/html; charset=ISO-8859-1';
//email
$htmlEmail = "
<html>
<head>
</head>
<body>
<font face='Arial' style='font-size:14px;'></font><br>
<font face='Arial' style='font-size:14px;'>A Student was added by ".$_SESSION['username']."   
</font><br><br>
</body>
</html>";

//php mail function
mail($to, $subject, $htmlEmail, $header);

}
?>

然后更改插入的代码:

<?php
session_start();
require "dbc.php";
require "functions.php";

if (isset($_POST['submit'])) {

  $sname = mysql_real_escape_string($_POST['shostfamilyid']);

  if ($sname && $ssurname && $sgender && $saddress && $sdob && $sblood && $spassport && $shobby1 && $shobby2 && $schildren && $sanimals) {

    $query = mysql_query("INSERT INTO student VALUES ('','$sname','$ssurname','$sgender','$saddress','$semail','$sdob','$stelephone','$sdiet','$sallergy','$smed','$sblood','$spassport','$shobby1','$shobby2','$shobby3','$ssiblings','$schildren','$srequests','$sanimals','$agent','')");

    if ($query) {
      send_email ($to, $from, $subject, $body); /* modify these as well */
      header("location: successstudent2.php");
      exit();
    } else { 
       echo "Check All Required Fields Are Completed"; 
    }
  }
}
?>

答案 1 :(得分:0)

我说忘记函数或单独的页面,只需包含PHP脚本即可直接在INSERT语句下面发送电子邮件。您不应该使用“if($ _ SESSION ['username'])”,这可能会导致您的问题。