SitePoint Sponsor

User Tag List

Results 1 to 10 of 10

Thread: Email wont "\n"

  1. #1
    SitePoint Zealot
    Join Date
    Mar 2011
    Posts
    146
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Email wont "\n"

    I am trying to get an email to generate

    and i get as far as the next line command

    any ideas?

    $body = "Client Name: " ."{$Fname} " . "{$Lname}" . "\n"X-------stops working here
    "Country: " . "{$country}" . "<br />";
    "Phone: " . "{$Phone}" . "<br />";
    "Email: " . "{$Email}" . "<br />";
    "Message: " . "{$Message}" . "<br />";

  2. #2
    It's all Geek to me silver trophybronze trophy
    ralph.m's Avatar
    Join Date
    Mar 2009
    Location
    Melbourne, AU
    Posts
    24,117
    Mentioned
    448 Post(s)
    Tagged
    8 Thread(s)
    You haven't concateneated those bits properly. Try something like

    Code:
    $body = "Client Name: " ."{$Fname} " . "{$Lname}" . "\n" .
    "Country: " . "{$country}" . "<br />" .
    "Phone: " . "{$Phone}" . "<br />" .
    "Email: " . "{$Email}" . "<br />" .
    "Message: " . "{$Message}" . "<br />";
    or perhaps just make it even simpler:

    Code:
    $body = "Client Name: $Fname $Lname\n" .
            "Country: $country\n" .
            "Phone: $Phone\n" .
            "Email: $Email\n" .
            "Message: $Message";
    Facebook | Google+ | Twitter | Web Design Tips | Free Contact Form

    Forum Usage: Tips on posting code samples, images and more

    Forrest Gump: "IE is like a box of chocolates: you never know what you're gonna get."

  3. #3
    SitePoint Zealot
    Join Date
    Mar 2011
    Posts
    146
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks

    do you have any tips on making the resulting email a bit nicer eg colours, images etc

  4. #4
    It's all Geek to me silver trophybronze trophy
    ralph.m's Avatar
    Join Date
    Mar 2009
    Location
    Melbourne, AU
    Posts
    24,117
    Mentioned
    448 Post(s)
    Tagged
    8 Thread(s)
    Quote Originally Posted by minusten View Post
    thanks
    Did it work?

    do you have any tips on making the resulting email a bit nicer eg colours, images etc
    That's a different kettle of fish, as you are talking about an HTML email. Someone else will have to help you with that.
    Facebook | Google+ | Twitter | Web Design Tips | Free Contact Form

    Forum Usage: Tips on posting code samples, images and more

    Forrest Gump: "IE is like a box of chocolates: you never know what you're gonna get."

  5. #5
    SitePoint Zealot
    Join Date
    Mar 2011
    Posts
    146
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    worked very well thanks heaps

    html email , you say.

    thanks again

  6. #6
    It's all Geek to me silver trophybronze trophy
    ralph.m's Avatar
    Join Date
    Mar 2009
    Location
    Melbourne, AU
    Posts
    24,117
    Mentioned
    448 Post(s)
    Tagged
    8 Thread(s)
    Yes, adding colors to text is basically talking HTML email. The mail() function only deals with plain text email. You can send HTML email (as shown here: www.php.net/manual/en/function.mail.php ), but that method doesn't provide a plain text version, which is good to include for those who can't view the HTML version. You could check out PHPM@iler or Zend_Mail to send both versions.
    Facebook | Google+ | Twitter | Web Design Tips | Free Contact Form

    Forum Usage: Tips on posting code samples, images and more

    Forrest Gump: "IE is like a box of chocolates: you never know what you're gonna get."

  7. #7
    SitePoint Enthusiast
    Join Date
    Feb 2012
    Location
    United Kingdom
    Posts
    78
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    @minusten, your script is rather cumbersome. You are concatenating variables, dispite them already being in double quotes (to interpolate them). You also could use the predefined constant PHP_EOL, so that there will be a carriage return regardless of what architecture you are working on.

    Here is a script that is a lot more optimised:
    PHP Code:
    $body 'Client Name: ',$Fname,' ',$Lname,PHP_EOL,
    'Country: ',$country,PHP_EOL,
    'Phone: ',$Phone,PHP_EOL,
    'Email: ',$Email,PHP_EOL,
    'Message: ',$Message,PHP_EOL
    What you will notice in my script above is that I do not bother concatenating or interpolating variables. This means there is no resource overhang for php when try to parse the variable $body.

  8. #8
    It's all Geek to me silver trophybronze trophy
    ralph.m's Avatar
    Join Date
    Mar 2009
    Location
    Melbourne, AU
    Posts
    24,117
    Mentioned
    448 Post(s)
    Tagged
    8 Thread(s)
    Hi modernW. Welcome to the forums.

    That code looks much neater. Thanks for posting it. (I pop over here from the design forums now and then, and I always learn something good. )
    Facebook | Google+ | Twitter | Web Design Tips | Free Contact Form

    Forum Usage: Tips on posting code samples, images and more

    Forrest Gump: "IE is like a box of chocolates: you never know what you're gonna get."

  9. #9
    Non-Member bronze trophy
    Join Date
    Nov 2009
    Location
    Keene, NH
    Posts
    3,760
    Mentioned
    23 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by modernW View Post
    @minusten, your script is rather cumbersome. You are concatenating variables, dispite them already being in double quotes (to interpolate them). You also could use the predefined constant PHP_EOL, so that there will be a carriage return regardless of what architecture you are working on.
    First off, you're code won't actually run... $body is a variable/string, not echo, so those commas won't work as you HAVE to use string addition there.

    Second, one could use single quotes as you did and then OMIT PHP EOL and just use actual carriage returns in the code instead of screwing with extra vars or escape codes.

    Code:
    $body = 'Client Name: '.$Fname.' '.$Lname.'
    Country: '.$country.'
    Phone: '.$Phone.'
    Email: '.$Email.'
    Message: '.$Message;
    Functional equivalent, and even simpler. (and this one would actually run).

    Oh, and @ the OP, adding HTML to e-mails is just a great way to get them flagged as spam; you want colors and bold and the like, then you don't want e-mails. Stick to plain-text, then your mails might actually have a chance of reaching people.

  10. #10
    SitePoint Enthusiast
    Join Date
    Feb 2012
    Location
    United Kingdom
    Posts
    78
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by deathshadow60 View Post
    First off, you're code won't actually run... $body is a variable/string, not echo, so those commas won't work as you HAVE to use string addition there.

    Second, one could use single quotes as you did and then OMIT PHP EOL and just use actual carriage returns in the code instead of screwing with extra vars or escape codes.

    Code:
    $body = 'Client Name: '.$Fname.' '.$Lname.'
    Country: '.$country.'
    Phone: '.$Phone.'
    Email: '.$Email.'
    Message: '.$Message;
    Whoops, yes you are correct (im not sure why but i thought i was writing about echoing out content...). Using commas for stacking can only be done in echo, and yes seen as it is a variable there is no need to use the constant PHP_EOL.

    And alternative to your code above would be using heredoc:
    PHP Code:
    $body = <<< string
    Client Name: $Fname $Lname
    Country: 
    $country
    Phone: 
    $Phone
    Email: 
    $Email
    Message: 
    $Message
    string; 


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •