【问题标题】:PHP not sending email from htmlPHP不从html发送电子邮件
【发布时间】:2013-10-24 20:36:23
【问题描述】:

我正在尝试制作一个表单,用户可以在其中介绍数据并将其提交到电子邮件。

但是,它让我进入一个空白页面(file.php 页面),并且没有发送电子邮件。

HTML 代码:

<form action="./sendmail.php" method="post">
    <div class="log">
        <input type="text" id="name" value="name" name="studname" class="form">
        <input type="email" id="mail" value="mail@gmail.com" name="studmail" class="form">
        <input type="tel" id="age" value="age" class="form" name="studage">
        <input type="submit" value="submit!" id="submit">
    </div>
</form>

现在这是我的“sendmail.php”代码:

<?php
    /*  STUDENT PARAMETERS  */
    $studentName = $_POST['studname'];
    $studentMail = $_POST['studmail'];
    $studentAge = $_POST['studage'];

    mail('code@gmail.com', 'Message',
    'NAME: ' . $studentName . '\nMAIL: ' . $studentMail . '\nAge: ' . $studentAge);
?>

我该如何解决这个问题,为什么这不起作用?感谢您的宝贵时间。

【问题讨论】:

  • 如果您想指定前面的所有内容,我相信您需要在 action 的正斜杠之前使用 ..。你还需要在 PHP 中使用句点来连接字符串。
  • onetrickpony 是正确的,使用 .连接字符串,而不是 + - 这是你的第一个问题......
  • 对不起,我的错,但仍然没有收到电子邮件。我已经将 + 更改为 .并且脚本正在被 html 文件访问,因为它进入了 php 脚本页面。但是由于某种原因没有发送电子邮件
  • 也许还有其他错误,这可能是配置问题,或者只是脚本中的语法错误。请查找有关如何从 PHP 获得反馈的资源。您可以启用错误报告以查看语法错误。 mail 函数本身不会返回太多信息,但它至少会返回一个布尔值来表示成功。
  • 试试echo mail(....)看看它是否真的发送。也许也打开错误。

标签: php html email post web


【解决方案1】:

我认为你可以使用 phpmailer 类

【讨论】:

    【解决方案2】:

    @Telmo Vaz 试试这个

    <?php
        $studentName = $_POST['studname'];
            $studentMail = $_POST['studmail'];
            $studentAge = $_POST['studage'];
    
        $recipient='code@gmail.com'; 
        $subject="Message"; 
        $content = "New Message Form Contact us\n From: ".$studentName .", \n Email: ".$studentMail .",\n Age: ".$studentAge;
            $headers = 'From: code@gmail.com' . "\r\n" .
            'Reply-To: code@gmail.com' . "\r\n" .
            'X-Mailer: PHP/' . phpversion();
            $retval=mail($recipient, $subject, $content, $headers);
            if( $retval == true ){do something;}
            else{echo "Error Sending Message";}
    ?>
    

    【讨论】:

      【解决方案3】:

      您需要一个电子邮件地址来发送电子邮件。电子邮件不会从该地址发送,但所选地址将显示在“发件人”部分。试试这个:

      /*  STUDENT PARAMETERS  */
      $studentName = $_POST['studname'];
      $studentMail = $_POST['studmail'];
      $studentAge = $_POST['studage'];
      
      $to = "code@gmail.com";
      $subject = "Message";
      $message = "NAME: " . $studentName . "\nMAIL: " . $studentMail . "\nAge: " . $studentAge;
      $from = $to;
      $headers = "From:" . $from;
      
      mail($to,$subject,$message,$headers);
      
      echo "Mail Sent.";
      

      【讨论】:

      • 即使没有$headers,它也应该发送。它只使用默认值。
      • @putvande 是的,我想是的,但那是品味的问题。
      猜你喜欢
      • 2011-03-04
      • 1970-01-01
      • 2013-04-21
      • 1970-01-01
      • 1970-01-01
      • 2015-02-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多