表单发送电子邮件和存储在数据库的帮助需要


form sends email and stores in data base help needed

我有一个表单,这是我的网站的用户之间发送消息同时,我想向用户发送一封电子邮件,通知他/她的收件箱中有一个新的MSG。有了这些,我可以添加到数据库中但是邮件不会发送到我这里的代码是,

    <?php
    if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form")) {
    require_once('mail/class.phpmailer.php');
    //include("mail/class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded
    $mail             = new PHPMailer();
    $body             = 'You have A new message in your xxxx inbox ';
    $body             = eregi_replace("[']",'',$body);
    $mail->IsSMTP(); // telling the class to use SMTP
    $mail->Host       = "xxxx"; // SMTP server
    $mail->SMTPDebug  = 0;                     // enables SMTP debug information (for testing)
                                           // 1 = errors and messages
                                           // 2 = messages only
    $mail->SMTPAuth   = true;                  // enable SMTP authentication
    $mail->Host       = "xxxx"; // sets the SMTP server
    $mail->Port       = xxxx;                    // set the SMTP port for the GMAIL server
    $mail->Username   = "xxxx"; // SMTP account username
    $mail->Password   = "xxxx";        // SMTP account password
    $mail->SetFrom('xxxx', 'xxxx');
    $mail->AddReplyTo("xxxx","xxxx");
    $mail->Subject    = "hello";
    $mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
    $mail->MsgHTML($body);
    $address = $row_reciver['Email']; 
    $mail->AddAddress($address, "XXXX");
    $mail->AddAttachment("xxxx");      // attachment
    $mail->AddAttachment("xxxx"); // attachment
    $mail->Send();
      $insertSQL = sprintf("INSERT INTO messages (sender, reciever, `time`, ip, subject, message, nameofsender, timefarsi) VALUES (%s, %s, %s, %s, %s, %s, %s, %s)",
                       GetSQLValueString($_POST['sender'], "text"),
                       GetSQLValueString($_POST['reciver'], "text"),
                       GetSQLValueString($_POST['signupdate'], "date"),
                       GetSQLValueString($_POST['ip'], "text"),
                       GetSQLValueString($_POST['subject'], "text"),
                       GetSQLValueString($_POST['message'], "text"),
                       GetSQLValueString($_POST['sendername'], "text"),
                       GetSQLValueString($_POST['farsi'], "text"));
      mysql_select_db($database_tehranichcore, $tehranichcore);
      $Result1 = mysql_query($insertSQL, $tehranichcore) or die(mysql_error());
      $insertGoTo = "../messagesent.php";
      if (isset($_SERVER['QUERY_STRING'])) {
    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
    $insertGoTo .= $_SERVER['QUERY_STRING'];
    } header(sprintf("Location: %s", $insertGoTo));
    }
    if (isset($_SESSION['MM_Username'])) {
      $colname_sender = $_SESSION['MM_Username'];
    }
    mysql_select_db($database_localohost, $localohost);
    $query_sender = sprintf("SELECT FirstName, busname, LastName, UserName, hash FROM users WHERE              UserName = %s", GetSQLValueString($colname_sender, "text"));
    $sender = mysql_query($query_sender, $localohost) or die(mysql_error());
    $row_sender = mysql_fetch_assoc($sender);
    $totalRows_sender = mysql_num_rows($sender);
    $colname_reciver = "-1";
    if (isset($_GET['h'])) {
    $colname_reciver = $_GET['h'];
    }
    mysql_select_db($database_xxxx, $xxxx);
    $query_reciver = sprintf("SELECT * FROM users WHERE hash = %s", GetSQLValueString($colname_reciver,         "text"));
    $reciver = mysql_query($query_reciver, $xxx) or die(mysql_error());
    $row_reciver = mysql_fetch_assoc($reciver);
    $totalRows_reciver = mysql_num_rows($reciver);
    $ipadd = $_SERVER['REMOTE_ADDR']; 
    $signup = date('Y-m-d H:i');
    ?>
    <body>
    <form name="form" action="<?php echo $editFormAction; ?>" method="POST"dir="RTL">
    <p>&nbsp;</p>
    <p>reciver<?php echo $row_reciver['busname']; ?><?php echo $row_reciver['FirstName']; ?> <?php echo 
    $row_reciver['LastName']; ?></p>
    <span id="sprytextfield1">
    <input type="text" name="subject" id="subject" placeholder="tittle">
    <br>
    <span class="textfieldRequiredMsg">A value is required.</span></span>
    <p><span id="sprytextarea1"><br>
    <textarea name="message" id="message" cols="45" rows="5" placeholder="متن پیام"></textarea>
    <span class="textareaRequiredMsg">A value is required.</span></span></p>
    <input type="hidden" name="ip" id="ip" value="<?php echo $ipadd ?>">
    <input type="hidden" name="signupdate" id="signupdate"value="<?php echo $signup ?>">
    <input name="sender" type="hidden" value="<?php echo $_SESSION['MM_Username']; ?>">
    <input name="reciver" type="hidden" value="<?php echo $row_reciver['UserName']; ?>">
    <input name="sendername" type="hidden" value="<?php echo $row_sender['busname']; ?><?php echo 
    $row_sender['FirstName']; ?> <?php echo $row_sender['LastName']; ?>">
    <input type="submit" name="send" id="send" value="send">
    <input type="hidden" name="MM_insert" value="form">
    </form> 
    </div>
    </div>
    </body>
    <script type="text/javascript">
    var sprytextfield1 = new Spry.Widget.ValidationTextField("sprytextfield1");
    var sprytextarea1 = new Spry.Widget.ValidationTextarea("sprytextarea1");
    </script>
    <?php
    mysql_free_result($sender);
    mysql_free_result($reciver);
?> 

设置$mail->SMTPDebug = 2,查看错误显示

其他备注:

  • 你设置了两次$mail->Host = "xxxx";
  • $mail->MsgHTML改为$mail->Body;

在if语句中也有不必要的括号。你可以修改

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form"))

if(isset($_POST["MM_insert"]) && $_POST["MM_insert"] == "form")

但这是可选的