Odd subject line in php e-mail

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rando1000
    New Member
    • Aug 2007
    • 80

    Odd subject line in php e-mail

    I've added the following code to a php page that's capturing data. In the end, I'd like to assign variables to the e-mail, but for now it's just text.

    When I send an e-mail with this code, the subject line in the received e-mail is "Lac Date". Since that's nowhere in the code, I'm at a loss as to where it's coming from. Any ideas?

    Code:
    $to = "rando@stolze.com";
    $subject = "New Quote from ";
    $body = "Hello world";
    if (mail($to, $subject, $body)) {
      echo("<p>Message successfully sent!</p>");
     } else {
      echo("<p>Message delivery failed...</p>");
     }
  • dgreenhouse
    Recognized Expert Contributor
    • May 2008
    • 250

    #2
    I don't see where that would be happening, but I'd recommend also sending "headers" along with the message.

    i.e.

    Code:
    <?php
    $to      = 'nobody@example.com';
    $subject = 'the subject';
    $message = 'hello';
    $headers = 'From: webmaster@example.com' . "\r\n" .
        'Reply-To: webmaster@example.com' . "\r\n" .
        'X-Mailer: PHP/' . phpversion();
    
    mail($to, $subject, $message, $headers);
    ?>
    Last edited by Dormilich; Mar 1 '11, 10:01 PM.

    Comment

    • rando1000
      New Member
      • Aug 2007
      • 80

      #3
      Thanks. I've added the headers per your suggestion. I'm not sure where the characters were coming from, but I figured out why. When I was inserting a variable after my text, I was doing it visual basic style, like "New Quote from " $Person. Obviously this is incorrect; the variable should be located inside the quote like "New Quote from $Person".

      Since I do much more application programming than web programming, this wasn't intuitive to me and I had to look at it a while to figure it out.

      Comment

      • HaLo2FrEeEk
        Contributor
        • Feb 2007
        • 404

        #4
        Actually the proper way to insert a variable into a string is either by using sprint(), or like this:

        $str = "This is a string with a " . $variable;

        End the string with a quotation mark, then put a . (dot) to indicate that you want to append the variable to the string, then the variable name. If you wanted to put a variable in the middle of a string, you could do this:

        $str = "There is a " . $variable . " in this string";

        If you wanted to use sprintf(), then you'd do this:

        $str = sprintf("There is a %s in this string", $variable);

        The %s gets replaced with the $variable parameter.

        Comment

        • rando1000
          New Member
          • Aug 2007
          • 80

          #5
          That makes a lot more sense to me, coming from a VB background; basically just substitute "&" for "." and it's the same. However, the example I gave of putting the variable within the quotes works as well.

          "This is $variable" where $variable = MyString will produce "This is MyString".

          Comment

          • HaLo2FrEeEk
            Contributor
            • Feb 2007
            • 404

            #6
            Sure it works, but it's bad practice and makes code less readable. My PHP Editor has syntax highlighting, strings are colored red. Putting a variable within the quotes causes it to be colored red as well, separating it from the quotes makes it easier to spot.

            Plus, like I said, it's just bad practice. A good example of why is the fact that putting the variable inside the quotes only works with double quotes, not single quotes. Doing something wrong just because it works is bad, you should learn to do it the right way from the start, to avoid potential headaches later.

            Comment

            Working...