在电子邮件中显示PHP变量

时间:2011-02-13 22:15:30

标签: php email

我正在尝试在电子邮件末尾显示用户的姓名。 $ firstname和$ lastname存储为会话变量。这是代码:

//get user info from SESSION
$firstname = $_SESSION['firstname'];
$lastname = $_SESSION['lastname'];
$email = $_SESSION['email'];

//get mail function data
$case = $_POST['case'];
$to = addslashes(strip_tags($_POST['to']));
$subject = addslashes(strip_tags($_POST['subject']));
$from = "confirmation@domain.com";
$headers = "From: $from\r\n";
$message = 

"
Thanks!

$firstname $lastname
$email

";

邮件中的名字,姓氏和电子邮件都是空白的。有什么想法吗?

邮件功能:

//send email
        if (mail($to, $subject, $message, $headers, "-f".$from)){
            //register into database
            $register_email = mysql_query 
            ("INSERT INTO `email` VALUES ('','$case','$userid','$to','$from','$subject','$message','$sent','$read','')");
            //formatting for error message
            $emailSent = "block";
            $emailFailed = "none";
        }
        else //if the email fails to send
        {
            $emailSent = "none";
            $emailFailed = "block";
        }
?>

2 个答案:

答案 0 :(得分:0)

您是否尝试过调试会话数据:

$firstname = $_SESSION['firstname'];
$lastname = $_SESSION['lastname'];
$email = $_SESSION['email'];

很可能是空白。

如果您尝试var_dump($_SESSION)并且数据显示,请将其添加到您的帖子中,以便我们验证是否正确选择了数据。

此外,您应该仔细检查您的邮件功能,以确保您发送正确的变量。

如果不是上述工作,请发布您的整个脚本以供审核。

答案 1 :(得分:0)

而不是这样做:

 $message = 

    "
    Thanks!

    $firstname $lastname
    $email

    ";

在Linux上使用\ n或在Windows服务器上使用\ r \ n格式化换行符,或者可以使用PHP PHP_EOL

$message = "Thanks!\n$firstname $lastname\n$emal\n"; //liux 
$message = "Thanks!\r\n$firstname $lastname\r\n$emal\r\n"; //windows
// In case any of our lines are larger than 70 characters, we should use wordwrap()
$message = wordwrap($message, 70);

PHP Mail function

要发送的消息。

每行应使用LF(\ n)分隔。行不应超过70个字符。