Displaying PHP variables in an email
I am trying to display a users first and last name at the end of an email message. The $firstname and $lastname are stored as session variables. This is the code:
//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
";
firstname, lastname and email are all blank in the message. Any ideas?
Mail function:
//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";
}
?>
Have you tried debugging the session data:
$firstname = $_SESSION['firstname'];
$lastname = $_SESSION['lastname'];
$email = $_SESSION['email'];
more than likely will be blank.
if the you try var_dump($_SESSION)
and the data shows up, please apppend that to your post so we can verify that the data is being selected correctly.
Also you should double check your mail function to make sure that your sending the correct variable.
if non of the above work then please post your entire script for review.
instead of doing this:
$message =
"
Thanks!
$firstname $lastname
$email
";
format newline with \n on linux or \r\n on windows server or you can use 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
Message to be sent.
Each line should be separated with a LF (\n). Lines should not be larger than 70 characters.
精彩评论