【问题标题】:unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING error mail意外的 T_ENCAPSED_AND_WHITESPACE,期待 T_STRING 或 T_VARIABLE 或 T_NUM_STRING 错误邮件
【发布时间】:2017-04-08 13:59:27
【问题描述】:

我完全不明白为什么我会在这段代码中收到此错误:

$finalmessage = "
From:$_POST['name']
Email:$_POST['email']
Message:$_POST['message']
";

下面是整个邮件 php 代码:

<?php

$name = $_POST['name'];
$surname = $_POST['surname'];
$email = $_POST['email'];
$message = $_POST['message'];
$support_address = "info@bkslegal";
$headers = "From: ".$email;
$header2 = "From: ".$support_address;
$finalmessage = "
From:$_POST['name']
Email:$_POST['email']
Message:$_POST['message']
";

if ( $name == "")

{   
}
else
{
mail("$support_address","finalmessage",$headers);
$result = "Your message has been sent succesfully!" 

mail("$email","Thank you for contacting us!","We will soon be in contact with you!",$header2);  
}
?>

【问题讨论】:

标签: php html ajax


【解决方案1】:

我看到你的参数写错了。你写道:

mail("$support_address","finalmessage",$headers);

应该是:

mail ($support_address, $subject, $message, $headers);

并删除此行

//mail("$email","Thank you for contacting us!","We will soon be in contact with you!",$header2);

PHP 邮件SYNTAX 是:

mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] )

最终代码:

<?php
$name = $_POST['name'];
$surname = $_POST['surname'];
$email = $_POST['email'];
$support_address = "info@bkslegal";

if($name == "") {
   //show somting error message
}
else
{
    $Message = "From:".$name."<br />";
    $Message .= "Email:".$email."<br />";
    $Message .= "Message:". $_POST['message'];

    $to = $support_address;
    $subject = "new message";

    // To send HTML mail, the Content-type header must be set
    $headers  = 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

    // Additional headers
    $headers .= 'To: Name <$to>' . "\r\n";
    $headers .= 'From:  $name <$email>' . "\r\n";
    $headers .= 'Cc: birthdayarchive@example.com' . "\r\n";
    $headers .= 'Bcc: birthdaycheck@example.com' . "\r\n";


    if(mail($to, $subject, $Message, $headers)) {
       echo "Your message has been  Sent";
    } else {
       echo "Mesage Error";
    }  
}
?>

注意:使用任何邮件库来防止像PHPMailer这样的标头注入漏洞

【讨论】:

    猜你喜欢
    • 2012-03-23
    • 2012-01-14
    • 1970-01-01
    • 2011-11-07
    • 2014-07-03
    • 2023-03-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多