tags:

views:

77

answers:

5

I'm creating a share page for my website. I want the user to be able to send a message along with the formatted html.

so I have.

$message = $_REQUEST['message'];

$page = 'some html code';

mail($TO,$subject,$page, $headers);

I want the $message and $page to be added together.

A: 

You can do $message .= $page;

It is equivalent to "append $page to $message"

Soufiane Hassou
A: 

If you just want do concatinate the two strings?

$body = $message . $page;
mail($TO,$subject,$body, $headers);

Example taken from http://php.net/manual/en/language.operators.string.php

<?php
$a = "Hello ";
$b = $a . "World!"; // now $b contains "Hello World!"

$a = "Hello ";
$a .= "World!";     // now $a contains "Hello World!"
?>
redimp
Well that doesn't seem to be working. How can I insert $message into the html?
creocare
A: 

I would strongly advise you to sanitize the user input beforehand.

just do the following it should work:

$message = $_REQUEST['message'];

$message .= 'some html code';

mail($TO,$subject,$page, $headers);

The . is a concatenation operator and you can read more about it here.

Russell Dias
Can you explain a little "sanitize"?
creocare
Do not trust the user. Assume that every user is malicious(morbid thought but very handy). So essentially, you have to verify all the user input, to prevent things like SQL Injection, XSS attacks etc. Have a browse through the following:http://devzone.zend.com/article/1113
Russell Dias
A: 

I use heredoc to accomplish what (I think) you're trying to do. The example might look like

$message = "I'm a message";

$page = <<<HTML
<p>I'm a $message that's been inserted into html.</p>
HTML;

Like other users have said before me, never, ever use input directly from a user without first sanitizing or filtering it.

Jeremy Kendall
Yes this is what i'd like to do. I tried doing what you've done here but in the email it just displays the variable $message not the message.
creocare
@creocare - Make sure to read the heredoc documentation (http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc) carefully. I had some problems when I first started using it too ;) There are some very significant gotchas that may be causing you some issues. Pay special attention to the rules about the closing delimiter.Would you consider posting the heredoc code that's giving you trouble? That might be helpful.
Jeremy Kendall
$message = $_REQUEST['message'];$page =<<<' <html> <body> <p> $message </p> <img src="print.jpg"/> </body></html>';
creocare
I guess it should look like this?<<<HTML<html> <body> <p>$message</p> <img src="print.jpg"> </body></html>';HTML
creocare
@creocare - It's hard to tell from the comment formatting, but it looks like you're not including the mandatory opening and closing delimiters. heredoc must start with three less than symbols followed by the delimiter, ex <<<ABC. At the end of your heredoc, ABC must appear again, all by itself, with no extra whitespace, etc. You can add a semicolon. Have you tried copying and pasting my heredoc example for test purposes? Or the examples in the php docs? That might help.
Jeremy Kendall
Yes! Make sure you end with HTML as well.
Jeremy Kendall
Yeah I did but it doesn't seem to like it.
creocare
one other question too. Why the the " ' " in that's screw up everything?
creocare
the apostrophe screws up the php code.
creocare
Why do you have a single quote and a colon before the closing HTML? Is the closing HTML on a line all by itself? I understand how confusing this can be, but make sure you've carefully reviewed the docs on this one. Unless there's something really unconventional going on with your development environment, you should be able to use heredoc without a problem.
Jeremy Kendall
So it's working now?
Jeremy Kendall
I should post the whole page somewhere so you can see it.
creocare
isn't there a website called postit or something for that?
creocare
i'm not sure if this is legit for this forum but. check it here. http://paste-it.net/public/v632c98/
creocare
Don't take this the wrong way, but it's obvious from your code sample that you're not taking my suggestions and not reading the php heredoc documentation closely enough. Please review the heredoc documentation (http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc), paying special attention to the section titled WARNING in bold, red font, located in the box with the red border and red background. The problem you're having is addressed in that section.
Jeremy Kendall
No worries. I see the error of what I was doing. I was able to make it work for the first IF statement. If I add a second heredoc it wrecks the whole thing. http://paste-it.net/public/v38693f/
creocare
I figured out a way to make it all work. Thanks for you help.
creocare
You're welcome. I'm glad you were finally able to get it working. Congrats! Also, if one of the answers in this thread was helpful in solving your problem, please accept it as an answer. This is a community - accepting and upvoting answers is part of the deal. You'll be much more likely to get good help with a decent acceptance rate.
Jeremy Kendall
A: 

to answer the question here...

Well that doesn't seem to be working. How can I insert $message into the html? – creocare

    $template = '<html>
            <body>
            <h1>A message from something.com</h1>
            <p>
            {{userMessage}}
            </p>
            </body
            </html>';

$userMessage = 'hey there im the message';
$message = str_replace( '{{userMessage}}', $userMessage, $template );
echo $message;

you can get you dynamic message into your html email template using str_replace

David Morrow