【问题标题】:Add Subject to SENDMAIL.PHP file将主题添加到 SENDMAIL.PHP 文件
【发布时间】:2016-04-15 06:38:14
【问题描述】:

我有以下代码运行我的联系表单。该代码运行良好,但我想在收到的电子邮件中添加一个强制主题,因为它目前显示为“(无主题)”。有什么建议吗?

<?php
// Email Submit
// Note: filter_var() requires PHP >= 5.2.0
if ( isset($_POST['first_name']) && isset($_POST['last_name']) && isset($_POST['email']) && isset($_POST['project_name']) && isset($_POST['description'])  && filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) ) {
    // detect & prevent header injections
    $test = "/(content-type|bcc:|cc:|to:)/i";
    foreach ( $_POST as $key => $val ) {
        if ( preg_match( $test, $val ) ) {
        exit;
    }
}

$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$email = $_POST['email'];
$project_name = $_POST['project_name'];
$description = $_POST['description'];

$message = "
First Name: $first_name \n
Last Name: $last_name\n
Project Name: $project_name\n
Description: $description
";

mail( "email@email.com", $_POST['date'], $message, "From:" . $_POST['email'] );
  //            ^
  //  Replace with your email
}
?>

【问题讨论】:

  • 您希望主题行包含什么值?
  • 由于您是 StackOverflow 的新手:如果它解决了您的问题,请务必通过单击复选框来接受答案。

标签: php forms sendmail contact-form


【解决方案1】:

require 属性添加到生成的联系表单(用于主题输入字段)html 标记。它会强制在您提交之前填写主题字段。

Read a little more about the "required" attribute

【讨论】:

    【解决方案2】:

    mail函数的第二个参数是$subject参数。 可能是您的 $_POST['date'] 的值未定义导致收件人看到“(无主题)”

    尝试将邮件功能更改为:

    mail( "email@email.com", 'New Message sent on ' . $_POST['date'], $message, "From:" . $_POST['email'] );
    

    在此处阅读更多信息:http://php.net/manual/en/function.mail.php

    用法 bool邮件(字符串$to,字符串$subject,字符串$message [,字符串$additional_headers [,字符串$additional_parameters]])

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-09-21
      • 2015-02-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-31
      相关资源
      最近更新 更多