Jump to content

I'm writing a variable to a file with fwrite() but can't pass \n on mail() messg


00stuff

Recommended Posts

I"m trying to write this variable on a php page with fwrite() but can't pass the \n in the mail() message. It removes the "\" and a letter n comes out.

Can someone help please?

 

My Code:

$stringData2 = <<<EOF
<?php
\$name_first = \$_POST['name_first'];
\$name_last = \$_POST['name_last'];
\$phone = \$_POST['phone'];
\$email = \$_POST['email'];
\$to = "$receive_email";
\$subject = "Registration to $company_name";
\$message = "First Name: \$name_first \n Last Name: \$name_last \n Phone: \$phone \n Email: \$email";
\$from = "$receive_email";
\$headers = "From: \$from";
mail(\$to,\$subject,\$message,\$headers);
echo "Mail Sent. Click <a href='register.php'>here</a> to go back!";
?>
EOF;

 

 

How can I make sure the "\n" stays there?

I have no idea what you are trying to do, but I'm pretty sure you are going about it the wrong way. You are putting PHP code into a variable for what purpose?

 

Anyway, the problem is that you are escaping the "n" in the creation of that variable. But, you need the "n" escaped in the code that is eventually created. But, your results don't make sense to me. You are using the heredoc method of creating the string and the escape codes should be interpreted. Therefore, I would expect that the result of $stringData2 would include a literal linefeed (i.e. line break) so your code when written to a page would look something like

$message = "First Name: $name_first 
Last Name: $name_last 
Phone: $phone 
Email: $email";

 

It doesn't make sense to me that you would get an "n" character. But, to get the "\n" to be in the resulting variable you would need to escape the backslash by using "\\n". Then the variable is created with "\n".

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.