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

时间:2014-03-20 10:44:44

标签: php forms notifications phpmailer

我有一个表单,用于在我的网站用户之间发送消息 同时我想向用户发送一封电子邮件,通知他/她收件箱中有新的消息。我可以添加到我的数据库,但电子邮件没有发送到目前为止我的代码,

    <?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);
?> 

1 个答案:

答案 0 :(得分:0)

设置$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")

但这是可选的。