与Php& amp; Ajax发送部分电子邮件

时间:2017-04-17 14:10:57

标签: javascript php jquery html ajax

我设计了以下光滑表格并将其与PhP& ajax提交...其工作正常,包括发送电子邮件到目标电子邮件ID ..总共有七(07)个字段,假设由访问者填写。问题是,当有人提交包含所有字段的表单时,我只能收到三个(03)字段(姓名,电子邮件和消息)....其余的四(04)字段(电话,国家,预算&选择)没有在电子邮件中显示...我相信我的Php肯定存在一些问题。请告诉我在下面的Php中我错误的地方。

<?php

// Define some constants
define( "RECIPIENT_NAME", "John Smith" );
define( "RECIPIENT_EMAIL", "example@example.com" );
define( "EMAIL_SUBJECT", "Visitor Message" );

// Read the form values
$success = false;
$senderName = isset( $_POST['senderName'] ) ? preg_replace( "/[^\.\-\' a-zA-Z0-9]/", "", $_POST['senderName'] ) : "";
$senderEmail = isset( $_POST['senderEmail'] ) ? preg_replace( "/[^\.\-\_\@a-zA-Z0-9]/", "", $_POST['senderEmail'] ) : "";
$senderPhone = isset( $_POST['senderPhone'] ) ? preg_replace( "/[^\.\-\_\@a-zA-Z0-9]/", "", $_POST['senderPhone'] ) : "";
$senderCountry = isset( $_POST['senderCountry'] ) ? preg_replace( "/[^\.\-\_\@a-zA-Z0-9]/", "", $_POST['senderCountry'] ) : "";
$senderBudget = isset( $_POST['senderBudget'] ) ? preg_replace( "/[^\.\-\_\@a-zA-Z0-9]/", "", $_POST['senderBudget'] ) : "";
$senderSelect = isset( $_POST['senderSelect'] ) ? preg_replace( "/[^\.\-\_\@a-zA-Z0-9]/", "", $_POST['senderSelect'] ) : "";
$message = isset( $_POST['message'] ) ? preg_replace( "/(From:|To:|BCC:|CC:|Subject:|Content-Type:)/", "", $_POST['message'] ) : "";

// If all values exist, send the email
if ( $senderName && $senderEmail && $senderPhone && $senderCountry && $senderBudget && $senderSelect && $message ) {
  $recipient = RECIPIENT_NAME . " <" . RECIPIENT_EMAIL . ">";
  $headers = "From: " . $senderName . " <" . $senderEmail . ">";
  $success = mail( $recipient, EMAIL_SUBJECT, $message, $headers );
}

// Return an appropriate response to the browser
if ( isset($_GET["ajax"]) ) {
  echo $success ? "success" : "error";
} else {
?>
<html>
  <head>
    <title>Thanks!</title>
  </head>
  <body>
  <?php if ( $success ) echo "<p>Thanks for sending your message! We'll get back to you shortly.</p>" ?>
  <?php if ( !$success ) echo "<p>There was a problem sending your message. Please try again.</p>" ?>
  <p>Click your browser's Back button to return to the page.</p>
  </body>
</html>
<?php
}
?>

1 个答案:

答案 0 :(得分:1)

您在邮寄时未添加电话,国家/地区,预算和选择字段。试试这个:

if ( $senderName && $senderEmail && $senderPhone && $senderCountry && $senderBudget && $senderSelect && $message ) {

    $msgToSend = "Name: ".$senderName."\r\n";
    $msgToSend .= "Email: ".$senderEmail."\r\n";
    $msgToSend .= "Phone: ".$senderPhone."\r\n";
    $msgToSend .= "Sender Country: ".$senderCountry."\r\n";
    $msgToSend .= "Sender Budget: ".$senderBudget."\r\n";
    $msgToSend .= "Sender Select: ".$senderSelect."\r\n";
    $msgToSend .= "Message: ".$message;

    $recipient = RECIPIENT_NAME . " <" . RECIPIENT_EMAIL . ">";
    $headers = "From: " . $senderName . " <" . $senderEmail . ">";
    $success = mail( $recipient, EMAIL_SUBJECT, $msgToSend, $headers );
}