【问题标题】:Sending mail with PHP not working with my present code使用 PHP 发送邮件不适用于我目前的代码
【发布时间】:2013-04-10 12:50:21
【问题描述】:

我编写了这段代码,但它没有发送到我的电子邮件中。有什么问题?

这是联系表格中的代码:

<!-- send mail configuration -->
<input type="hidden" value="send-mail.php" name="to" id="to" />
<input type="hidden" value="Enter the subject here" name="subject" id="subject" />
<input type="hidden" value="send-mail.php" name="sendMailUrl" id="sendMailUrl" />
<!-- ENDS send mail configuration --> 

这是 send-mail.php 的代码

<?php
//vars
$subject = $_POST['subject'];
$to = explode(',', $_POST['to'] );

$from = $_POST['kurtfarrugia92@gmail.com'];

//data
$msg = "NAME: "  .$_POST['name']    ."<br>\n";
$msg .= "EMAIL: "  .$_POST['email']    ."<br>\n";
$msg .= "WEBSITE: "  .$_POST['web']    ."<br>\n";
$msg .= "COMMENTS: "  .$_POST['comments']    ."<br>\n";

//Headers
$headers  = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=UTF-8\r\n";
$headers .= "From: <".$from. ">" ;


//send for each mail
foreach($to as $mail){
mail($mail, $subject, $msg, $headers);
}

?>

【问题讨论】:

  • 这里在做什么 ?
  • 让我再次发布到整个表单
  • $from = $_POST['kurtfarrugia92@gmail.com'];也不应该有 $_POST[] 位
  • 表单输入不正确。在“收件人”输入字段中,我们将使用逗号分隔电子邮件地址。
  • 表格看起来不对。如果您粘贴整个 html 表单,我相信我们可以更快地发现问题。另外,我认为$headers .= "From: $from &lt;$from&gt;" ; 会更好

标签: php forms email contact


【解决方案1】:

基本的 PHP 邮件脚本。

文件mail.php

    <html>
    <body>

    <?php
    if (isset($_REQUEST['email']))
    //if "email" is filled out, send email
      {
      //send email
      $email = $_REQUEST['email'] ;
      $subject = $_REQUEST['subject'] ;
      $message = $_REQUEST['message'] ;
      mail("someone@example.com", $subject,
      $message, "From:" . $email);
      echo "Thank you for using our mail form";
      }
    else
    //if "email" is not filled out, display the form
      {
      echo "<form method='post' action='mail.php'>
      Email: <input name='email' type='text'><br>
      Subject: <input name='subject' type='text'><br>
      Message:<br>
      <textarea name='message' rows='15' cols='40'>
      </textarea><br>
      <input type='submit'>
      </form>";
      }
    ?>

    </body>
    </html> 

【讨论】:

    【解决方案2】:

    您有错误报告吗?获取有关发生/错误的更多信息可能会很方便。

    error_reporting(E_ALL);
    

    试试:

    echo "mail($mail, $subject, $msg, $headers)";

    查看您发送到 mail() 的内容是否与您认为的完全相同。

    【讨论】:

    • 注意:未定义的索引:主题在 C:\xampp\htdocs\thirdLogin\html\send-mail.php 第 5 行 注意:未定义的索引:到 C:\xampp\htdocs\thirdLogin\ html\send-mail.php 在第 6 行 注意:未定义索引:名称在 C:\xampp\htdocs\thirdLogin\html\send-mail.php 在第 11 行 注意:未定义索引:电子邮件在 C:\xampp\htdocs\第 12 行的thirdLogin\html\send-mail.php 注意:未定义的索引:C:\xampp\htdocs\thirdLogin\html\send-mail.php 中的第 13 行的 web 注意:未定义的索引:C:\xampp\ 中的 cmets htdocs\thirdLogin\html\send-mail.php 在第 14 行
    • 您发布的表格,是您的真实表格吗?因为它需要包含您想要在 send-mail.php 脚本中抓取的所有字段...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-06
    • 1970-01-01
    • 2011-11-28
    • 1970-01-01
    相关资源
    最近更新 更多