-1

I am trying to add several variables to this email script. The email itself sends every time, and is received. However, the email contains the variable itself and not the information. How can I get the variables to print the data they represent? (I know the variable exists.)

      <?//Send Welcome Email
             $to = '//Email Removed for security';
             $subject = 'Application Received';
             $msg = '
                    We have received your application. You can login at  
                     anytime by visiting:<br />
             <a href=\"$siteURL/return-Login.php" title="$siteName - Return Login\">$siteURL/return-Login.php</a><br /><br />
                    ';
             sendEmail($to,$subject,$msg,$siteName,$siteEmail,$siteURL,$account,$ip);
            ?>
  • 4
    Possible duplicate of [What is the difference between single-quoted and double-quoted strings in PHP?](http://stackoverflow.com/questions/3446216/what-is-the-difference-between-single-quoted-and-double-quoted-strings-in-php) – miken32 May 30 '16 at 22:20
  • It doesn't explain how to put quotes --> inside of quotes. – Jeremy Huskey May 30 '16 at 22:22
  • I have tried the normal '".$variable."' and also ."'$variable'". etc. – Jeremy Huskey May 30 '16 at 22:23
  • 1
    Then I suggest you start learning how to use PHP. Printing strings is about as basic as you can get in any language. http://php.net/manual/en/language.types.string.php – miken32 May 30 '16 at 22:23
  • I understand how to use strings. Alternatively, I have exhausted every variation that made since in this equation. And the variables won't print. I expected a professional to explain "This is what you're doing wrong." as opposed to patronizing me. I must be in the wrong place. – Jeremy Huskey May 30 '16 at 22:26
  • @JeremyHuskey I don't think you do understand how to use strings. If you did this wouldn't be a problem. I don't think miken32 was being patronizing at all, in fact he linked you to a page that explains all about strings and string escaping which you apparently refused to read before complaining he hadn't helped you. – Chris May 30 '16 at 22:33

3 Answers3

2

When you use single quote in a string php will not read variable values, it just assumes it is text, if you put your string in double quotes, variable will be interpreted as variable.

To be more clear

$my_var = "some text";
$my_body = "this is $my_var output";
echo $my_body;

will output

this is some text output

BUT

$my_var = "some text";
$my_body = 'this is $my_var output';
echo $my_body;

will output

this is $my_var output

if you want to use single quotes for any reason you must link strings like this

$my_var = "some text";
$my_body = 'this is '.$my_var.' output';
echo $my_body;

in this case your output will be again

this is some text output

To be even more specific, your $msg should look like

$msg = 'We have received your application. You can login at  
        anytime by visiting:<br />
        <a href=\"'.$siteURL.'/return-Login.php" title="'.$siteName.' - Return Login\">'.$siteURL.'/return-Login.php</a><br /><br />
        ';
Igor S Om
  • 735
  • 3
  • 12
0

I see one variable that will not display correctly. Perhaps this will apply to each variable you are getting printed incorrectly.

Take a look at the email body string. You are using single quotes to make the string and the $siteURL variable is in there somewhere. Variables included directly in a string will not parse unless the string is declared with double quotes.

Here are two ways to fix your issue:

  1. $msg = 'The site URL is' . $siteURL . '. You can see I interrupted the single quotes to add that site uri.';
  2. $msg = "The site URL is $siteURL. Putting the variable in curly brackets also works: {$siteURL}!";

As you can see the variable gets parsed in either of those circumstances, but not in single quotes within the string. If you want to use single quotes you have to interrupt the string with a period (.) to concatenate them together. :)

Matthew
  • 309
  • 4
  • 12
0

Your issue is because you need to use two different kinds of quotes in order to have your HTML AND your variables render correctly.

You need to nest the quotations.

Try the following:

$msg = 'We have received your application.  You can login at anytime by visiting:<br /><a href="'.$siteURL.'/return-Login.php" title="'.$siteName.' - Return Login">'.$siteURL.'/return-Login.php</a><br /><br />

The first single quote indicates the beginning of a string. Anywhere you see a single quote, you are starting and stopping the string and inserting PHP into it.

Anywhere you see double quotes, these are ignored and considered part of the string.

Lucha Laura Hardie
  • 429
  • 1
  • 3
  • 10