联系表格不发送电子邮件

发布于 2024-11-01 23:12:04 字数 2779 浏览 5 评论 0原文

我正在编写我的第一个 PHP 联系表单。我已经为此工作了一整天了。在谷歌上查找所有内容,我都找不到答案。联系人转到成功页面,但我的收件箱中仍然没有任何内容......

PHP:

<?php
  // Define some constants
  define( "RECIPIENT_NAME", "name" );
  define( "RECIPIENT_EMAIL", "[email protected]" );
  define( "EMAIL_SUBJECT", "Message" );

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

  //If all values exist, send the email 
  if ( $sender && $email && $message ) {
    $recipient = RECIPIENT_NAME . " <" . RECIPENT_EMAIL . $email . ">";
    $headers = "From: " . $sender . " <" . $email . ">";
    $success = mail( $recipient, EMAIL_SUBJECT, $message, $headers );
  }

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

有什么问题吗?

这是 HTML

<form id="contactForm" action="process.php" method="post"> 
  <label for="sender">Your Name</label> 
  <input type="text" name="sender" id="sender" placeholder="Your Name" required="required" maxlength="40"> 

  <label for="email">Your Email Address</label> 
  <input type="email" name="email" id="email" placeholder="Please type your email address"
  required="required" maxlength="50"> 

  <label for="message">Your Message</label> 
      <textarea name="message" id="message" placeholder="Please type your message" required="required"
       cols="50" rows="5" maxlength="10000"></textarea>     

  <div id="form-buttons"> 
    <input type="submit" id="send-message" name="send-message" value="Send Email" /> 
    <input type="button" id="cancel" name="cancel" value="Cancel" /> 
  </div><!-- #form-buttons --> 

有人能看一下吗?我确信我做的一切都是对的......

I'm coding my first ever PHP contact form. I've been working on it all day. Looking up everything on Google and I can't get a answer. The contact from goes to the success page and still nothing goes into my inbox...........

PHP:

<?php
  // Define some constants
  define( "RECIPIENT_NAME", "name" );
  define( "RECIPIENT_EMAIL", "[email protected]" );
  define( "EMAIL_SUBJECT", "Message" );

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

  //If all values exist, send the email 
  if ( $sender && $email && $message ) {
    $recipient = RECIPIENT_NAME . " <" . RECIPENT_EMAIL . $email . ">";
    $headers = "From: " . $sender . " <" . $email . ">";
    $success = mail( $recipient, EMAIL_SUBJECT, $message, $headers );
  }

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

Is there anything wrong with it?

Heres the HTML

<form id="contactForm" action="process.php" method="post"> 
  <label for="sender">Your Name</label> 
  <input type="text" name="sender" id="sender" placeholder="Your Name" required="required" maxlength="40"> 

  <label for="email">Your Email Address</label> 
  <input type="email" name="email" id="email" placeholder="Please type your email address"
  required="required" maxlength="50"> 

  <label for="message">Your Message</label> 
      <textarea name="message" id="message" placeholder="Please type your message" required="required"
       cols="50" rows="5" maxlength="10000"></textarea>     

  <div id="form-buttons"> 
    <input type="submit" id="send-message" name="send-message" value="Send Email" /> 
    <input type="button" id="cancel" name="cancel" value="Cancel" /> 
  </div><!-- #form-buttons --> 

Could someone take a look at this? I'm sure I did everything right.....

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

短暂陪伴 2024-11-08 23:12:04

将此行:更改

$recipient = RECIPIENT_NAME . " <" . RECIPENT_EMAIL . $email . ">";

为:

$recipient = RECIPIENT_NAME . " <" . RECIPENT_EMAIL . ">";

这可能是问题所在

change this line:

$recipient = RECIPIENT_NAME . " <" . RECIPENT_EMAIL . $email . ">";

to:

$recipient = RECIPIENT_NAME . " <" . RECIPENT_EMAIL . ">";

that might be the issue

未蓝澄海的烟 2024-11-08 23:12:04

至少你的行

$success mail( $recipient, EMAIL_SUBJECT, $message, $headers );

看起来是错误的,mail 之前应该有一个等号。

At least your line

$success mail( $recipient, EMAIL_SUBJECT, $message, $headers );

seems wrong, there should be an equality sign before mail.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文