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.


  $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.

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
" (double quotes are a necessity for this one)
If it is part of a webpage, can you show us how the information gets rendered?

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?

PHP_EOL is a constant that stands for End of Line. It writes the \r
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);

Could you give me an example in code please of how PHP_EOL works?

Sure thing

$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

$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\
";  

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
(CR/LF) sequence; however if it’s Os x, then only \r (CR) is required, and for Linux only
(LF) is needed. So it’s an abstract constant that allows us to make our code cross-compatible.

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
try
{
  $pdo = new PDO('mysql:host=localhost;dbname=xxxxxxxxxxx', 'xxxxxxxxxx', 'xxxxxxxxxxxxxx');
  $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::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

<!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($remindDate, ENT_QUOTES, 'UTF-8');
?> </p>
</blockquote>
<?php endforeach; ?>
</body>
</html>

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?

Yep, here is an idea using your previous code

<?php 
try 
{ 
  $pdo = new PDO('mysql:host=localhost;dbname=xxxxxxxxxxx', 'xxxxxxxxxx', 'xxxxxxxxxxxxxx'); 
  $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::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.

Thanks for your time, but it didn’t work for me - just threw up a blank browser page.

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.