【问题标题】:Html/php email send [duplicate]Html / php电子邮件发送[重复]
【发布时间】:2016-11-25 12:41:34
【问题描述】:

我即将编写一个小 html/php 脚本。但我无法将 HTML 表单输入中的数据发送到任何电子邮件地址或任何文本文件。我已经在寻找可能的解决方案,但对我没有任何帮助。浏览器重放了 php 脚本。但是没有邮件发送。任何帮助将不胜感激。谢谢你。

【问题讨论】:

  • 那么,您要发送电子邮件吗?
  • 你在本地服务器上吗?
  • 你的代码在哪里?
  • 请再次阅读您的问题,并尝试意识到只有这几行字没有人可以帮助您....

标签: php html email input


【解决方案1】:

我推荐你用PHPMailer他用起来很方便

【讨论】:

    【解决方案2】:

    不要忘记<form> 标签中的actionmethod 属性。

    html文件的内容

    <form action="send.php" method="POST">
        <input type="text" name="name" placeholder="Typ your name..." />
        <input type="email" name="from" placeholder="Typ your e-mailaddress..." />
        <textarea name="message" placeholder="Typ your message..."></textarea>
        <button type="submit">Send E-mail</button>
    </form>
    

    send.php 的内容

    <?
    if(isset($_POST)) {
        $name = $_POST['name'];
        $message = $_POST['message'];
        $from = $_POSST['from'];
    
        if(!empty($name) && !empty($message) {
            $subject = 'message from '.$name;
    
            $headers = "From: " . strip_tags($from) . "\r\n";
            $headers .= "Reply-To: ". strip_tags($from) . "\r\n";
            //$headers .= "CC: susan@example.com\r\n";
            $headers .= "MIME-Version: 1.0\r\n";
            $headers .= "Content-Type: text/html; charset=UTF-8\r\n";
    
            // @SEE: http://php.net/manual/en/function.mail.php
            if(mail('[YOUR-ADDRESS]', $subject, $message, $headers)) {
                echo 'Thx 4 msg!';
            }
            else {
                echo 'Oh nooos, The msg was not send.';
            }
        }
        else {
            echo 'You should provide the fields with some data..';
        }
    }
    ?>
    

    首先应该清楚地清理用户输入。

    【讨论】:

      【解决方案3】:

      我认为您的问题是如何使用一些代码处理表单数据,因此您可以发送电子邮件或将表单数据写入文件。这是您看到客户端服务器端之间的区别的地方。 HTML 是一种用于描述文档的语言,这里是您的表单:文本输入 name 将描述一个名称,表单将在 POST 方法中发送其数据,等等。描述 HTML 的文件在您的浏览器中处理。而且您的浏览器不会发送电子邮件或写入数据...这就是为什么您应该使用服务器端语言(例如 PHP)来完成工作。 PHP 非常适合帮助您处理数据并处理不同的事件...在您的情况下,非常适合接收数据、分析数据然后通过邮件发送数据或将数据保存到文件中。

      所以现在您可能想了解如何执行此操作... 邮件有点棘手,因为您可能需要配置邮件服务器、身份验证等内容。也许一个好的解决方案是尝试使用 Google 帐户或类似的东西发送邮件......当它完成后,您可以简单地发送一封电子邮件,如下所示:

      <?php
      
      $to      = 'your@email.here';
      $subject = 'Mail test';
      $data    = $_POST['name']; // if a `name` field exist and your form send its data through POST method
      mail($to, $subject, $data);
      

      将内容写入文件更简单,它只请求读取和/或写入文件的权限。

      <?php
      
      $file = 'path/to/file';
      file_put_contents($file, $_POST['name'] . ' for example');
      

      所以这就是全局的一切:

      index.htmlHTML 文件:

      <!DOCTYPE html>
      <html lang="en">
          <head>
              <meta charset="utf-8" />
              <title>Form</title>
          </head>
          <body>
              <form action="process.php" method="post">
                  <input name="name" type="text" placeholder="Name" />
                  <input type="submit" value="Process">
              </form>
          </body>
      </html>
      

      process.php PHP 文件

      <?php
      
      /**
       * Testing data
       */
      
      if (!isset($_POST['name'])) {
          die('No value `name` found');
      }
      
      /**
       * Configuring process
       */
      
      $to      = 'your@email.here';
      $subject = 'Mail test';
      $data    = $_POST['name'];
      
      /**
       * Saving data
       */
      
      $res = file_put_contents(
          'data.txt',
          $data."\r\n"
      );
      
      if ($res !== false) {
          echo 'data saved'.PHP_EOL;
      } else {
          echo 'error while saving data'.PHP_EOL;
      }
      
      /**
       * Sending email
       */
      
      $res = mail(
          $to,
          $subject,
          $data
      );
      
      if ($res === true) {
          echo 'mail sent';
      } else {
          echo 'error while sending mail'.PHP_EOL;
      }
      

      我建议您阅读 mail()file_put_contents() 文档以了解它们在出现错误时的行为... :)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-09-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-09-08
        • 2011-03-04
        相关资源
        最近更新 更多