SitePoint Sponsor

User Tag List

Results 1 to 14 of 14
  1. #1
    SitePoint Zealot
    Join Date
    Apr 2003
    Location
    Perth W.A.
    Posts
    131
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Formatting control within a string

    Hi Folks,
    Can someone please explain how I can do some basic formatting within a string?
    I've tried concatenating <br /> tags in quotes and other basic HTML markup all to no avail.
    The parser just spits it out verbatim as non rendered code.
    PHP Code:
      $remindDates[] = 'Dear ' $row['firstname'] . ',' ' This is just to let you know that your Web Hosting account registered with us for the domain: ' $row['domain'] . ' expires in one month\'s time on: ' $row['expireDate']; 
    I'd like to break this big string onto a new line and maybe bold some words.

    Any help appreciated.

  2. #2
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    4,810
    Mentioned
    141 Post(s)
    Tagged
    0 Thread(s)
    Is this part of an email or shown on a webpage?
    If it is part of an email, I am assuming your e-mail is sent in plan text (not HTML), so you will want to use PHP_EOL for line breaks or ."\r\n" (double quotes are a necessity for this one)
    If it is part of a webpage, can you show us how the information gets rendered?

  3. #3
    SitePoint Zealot
    Join Date
    Apr 2003
    Location
    Perth W.A.
    Posts
    131
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry, I could have been a little more explicit.
    I have a php mail script which will catch the array information and send an email (Hosting renewal reminder) using a cron job via the server.

    Prior to connecting the mail script I am testing it in Firefox and it renders as per the attached screenshot, but the plan is to ultimately have an email sent.

    What is PHP_EOL please?
    render.jpg

  4. #4
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    4,810
    Mentioned
    141 Post(s)
    Tagged
    0 Thread(s)
    PHP_EOL is a constant that stands for End of Line. It writes the \r\n characters on your behalf thus allowing a new line to be created. When viewing it in a browser, the PHP_EOL will not be visible unless you perform a view source or run the output through nl2br($output);

  5. #5
    SitePoint Zealot
    Join Date
    Apr 2003
    Location
    Perth W.A.
    Posts
    131
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Could you give me an example in code please of how PHP_EOL works?

  6. #6
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    4,810
    Mentioned
    141 Post(s)
    Tagged
    0 Thread(s)
    Sure thing

    PHP Code:
    $remindDates[] = 'Dear ' $row['firstname'] . ',' ' This is just to let you know that your Web Hosting account registered with us for the domain: ' $row['domain'] . ' expires in one month\'s time on: ' $row['expireDate'] . PHP_EOL
    Or if you get an error
    PHP Code:
    $remindDates[] = 'Dear ' $row['firstname'] . ',' ' This is just to let you know that your Web Hosting account registered with us for the domain: ' $row['domain'] . ' expires in one month\'s time on: ' $row['expireDate'] . "\r\n"

  7. #7
    SitePoint Enthusiast
    Join Date
    Feb 2012
    Location
    United Kingdom
    Posts
    77
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cpradio View Post
    PHP_EOL is a constant that stands for End of Line. It writes the \r\n characters on your behalf thus allowing a new line to be created.
    Just to clarify, the PHP_EOL constant will use the correct set of escape sequences for the architecture it's working on. If this is Windows, then it'll use the \r\n (CR/LF) sequence; however if it's Os x, then only \r (CR) is required, and for Linux only \n (LF) is needed. So it's an abstract constant that allows us to make our code cross-compatible.

  8. #8
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    8,892
    Mentioned
    138 Post(s)
    Tagged
    2 Thread(s)
    As said before by default PHP sends email as plain-text. If you want to send html you need to set the correct headers.

    A nice intro to how this works is over here: http://css-tricks.com/sending-nice-html-email-with-php/
    Rémon - Hosting Advisor

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  9. #9
    SitePoint Zealot
    Join Date
    Apr 2003
    Location
    Perth W.A.
    Posts
    131
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ScallioXTX View Post
    As said before by default PHP sends email as plain-text. If you want to send html you need to set the correct headers.

    A nice intro to how this works is over here: http://css-tricks.com/sending-nice-html-email-with-php/
    Thanks for your reply.
    Can I just layout my objective in full, as I probably started 'half way up the ladder' with my initial post.
    I think I have the send mail concept down now, however my ultimate objective is to send individual email reminders to hosting clients based on a MySQL query of 'remindDate' being CURDATE().

    What I have done so far:

    I have successfully pulled the required data from the database and output it in the browser.
    This has resulted in multiple matches. My question is: How do I send individual reminder emails to these matched results?.

    Please note: I have commented out the CURDATE() condition at this time to simplify debugging.

    Any help greatly appreciated!
    PHP Code:
    <?php
    try
    {
      
    $pdo = new PDO('mysql:host=localhost;dbname=xxxxxxxxxxx''xxxxxxxxxx''xxxxxxxxxxxxxx');
      
    $pdo->setAttribute(PDO::ATTR_ERRMODEPDO::ERRMODE_EXCEPTION);
      
    $pdo->exec('SET NAMES "utf8"');
    }
    catch (
    PDOException $e)
    {
      
    $error 'Unable to connect to the database server.';
      include 
    'error.html.php';
      exit();
    }

    try
    {
      
    //$sql = 'SELECT * FROM renewals WHERE remindDate = CURDATE()';
      
    $sql 'SELECT renewals.id, domain, expireDate, remindDate, firstname
    FROM renewals INNER JOIN subscriber
    ON subscriberid = subscriber.id'
    ;

      
    $result $pdo->query($sql);
    }
    catch (
    PDOException $e)
    {
      
    $error 'Error fetching expiry dates: ' $e->getMessage();
      include 
    'error.html.php';
      exit();
    }

    foreach (
    $result as $row)

    {
      
    $remindDates[] = $row['firstname'] .' '$row['domain'] . ' ' $row['expireDate'];
      }
    include 
    'remindNow.html.php';
    remindNow.html.php

    PHP Code:
    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="utf-8">
    <title>List of Today's Hosting Renewals</title>
    </head>
    <body>
    <?php
         
    foreach ($remindDates as $remindDate): ?>
    <blockquote>
      <p><?php echo htmlspecialchars($remindDateENT_QUOTES'UTF-8');
    ?> </p>
    </blockquote>
    <?php endforeach; ?>
    </body>
    </html>

  10. #10
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    4,810
    Mentioned
    141 Post(s)
    Tagged
    0 Thread(s)
    Well, you have a few ways you can go about it. 1) you can call the mail() command within your foreach, sending the email to each recipient. or 2) If you have a generic enough message, you can Blind Carbon Copy all recipients and use only 1 mail command

  11. #11
    SitePoint Zealot
    Join Date
    Apr 2003
    Location
    Perth W.A.
    Posts
    131
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cpradio View Post
    Well, you have a few ways you can go about it. 1) you can call the mail() command within your foreach, sending the email to each recipient. or 2) If you have a generic enough message, you can Blind Carbon Copy all recipients and use only 1 mail command
    Yes, I'd like it to be personalised rather than generic, can you possibly show me how the code would look calling the mail() command from within the foreach loop?

  12. #12
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    4,810
    Mentioned
    141 Post(s)
    Tagged
    0 Thread(s)
    Yep, here is an idea using your previous code
    PHP Code:
    <?php 
    try 

      
    $pdo = new PDO('mysql:host=localhost;dbname=xxxxxxxxxxx''xxxxxxxxxx''xxxxxxxxxxxxxx'); 
      
    $pdo->setAttribute(PDO::ATTR_ERRMODEPDO::ERRMODE_EXCEPTION); 
      
    $pdo->exec('SET NAMES "utf8"'); 

    catch (
    PDOException $e

      
    $error 'Unable to connect to the database server.'
      include 
    'error.html.php'
      exit(); 


    try 

      
    //$sql = 'SELECT * FROM renewals WHERE remindDate = CURDATE()'; 
      
    $sql 'SELECT renewals.id, domain, expireDate, remindDate, firstname 
    FROM renewals INNER JOIN subscriber 
    ON subscriberid = subscriber.id'


      
    $result $pdo->query($sql); 

    catch (
    PDOException $e

      
    $error 'Error fetching expiry dates: ' $e->getMessage(); 
      include 
    'error.html.php'
      exit(); 


    foreach (
    $result as $row

      
    $emailMessage = <<<MESSAGE
    Put the email message and any variables in this section!
    MESSAGE;

      
    $remindDates[] = $row['firstname'] .' '$row['domain'] . ' ' $row['expireDate']; 
      
    mail($row['to_email_address'], 'Subject of your E-mail Here'$emailMessage'Your from address here');

    ?>
    Few things to note,
    1) if testing, DO NOT use $row['to_email_address'], instead hard code an email address so your subscribers do not get your test emails.
    2) You will need to eventually return the email address from your database for your subscriber, so I just guessed and called it to_email_address

    Let me know if you need anything else.

  13. #13
    SitePoint Zealot
    Join Date
    Apr 2003
    Location
    Perth W.A.
    Posts
    131
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for your time, but it didn't work for me - just threw up a blank browser page.

  14. #14
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    4,810
    Mentioned
    141 Post(s)
    Tagged
    0 Thread(s)
    well, yes, it would show a blank page, as I didn't output anything, it would send an email if you replaced the variables correctly.


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
  •