2

I've been reading up on the Heredoc and Nowdoc syntax, and I'm trying to understanding how to break out of it so I can add other functionality.

I would like to break out of it after this part (if possible):

User::sendNewticket($send_to, 'Maintenance Ticket '.Input::get('st_id'),

but throws a syntax error when I add ?> after 'Maintenance Ticket '.Input::get('st_id'), ?>

Anyways, I gave up on that for the time being and I would be good to go if I could figure out how to echo the following using this syntax:

'.$fn.'<br>
'.$time.'<br>
'.$stc.'<br>

Similar to this echo $fn;

Here is relevant code to my situation.

<?php

include($_SERVER['DOCUMENT_ROOT'] . "/core/init.php");

// new data
$send_to           = $_POST['send_to'];
$rec_message       = $_POST['rec_message'];

//Message
$status            = $_POST['status'];
$st_id             = $_POST['st_id'];
$posted_by         = $_POST['posted_by'];
$posted_on         = $_POST['posted_on'];
$unit              = $_POST['unit'];
$subject           = $_POST['subject'];
$content           = $_POST['content'];

//Loader & Flash Message
echo '<meta http-equiv="refresh" content="5;URL=/admin/maintenance/email-ticket.php?st_id='.$st_id.'">'; 
Session::flash('email-ticket', '<h3 class="orange-tx" align="center">Your Email has been sent!</h3>');

//Replies
//$id                = $_POST['id'];
//$from_name           = $_POST['from_name'];
//$st_time         = $_POST['st_time'];
//$st_content          = $_POST['st_content'];

//Pulling Conversation from database
$st_messages = DB::getInstance()->query("SELECT `id`,`st_id`,`from_name`,`st_content`,`st_time` FROM `st_messages` WHERE `st_id` = $st_id ORDER BY id ASC");


foreach ($st_messages->results() as $mt) {
if($mt->from_name=='Support Team'){
} else {
}

$fn = escape ($mt->from_name);
$time = escape (date("F d, Y - h:i a", strtotime ($mt->st_time)));
$stc = nl2br (escape($mt->st_content));

//START EMAIL
User::sendNewticket($send_to, 'Maintenance Ticket '.Input::get('st_id'), 
'Hello -<br><br>
'.Input::get('rec_message').'<br><br>


<strong>Ticket Info:</strong><br><br>

<strong>Status: </strong>'.Input::get('status').'<br>
<strong>Ticket #: </strong>'.Input::get('st_id').'<br>
<strong>Posted By: </strong>'.Input::get('posted_by').'<br>
<strong>Posted On: </strong>'.Input::get('posted_on').'<br>
<strong>Building Unit: </strong>'.Input::get('unit').'<br>
<strong>Subject: </strong>'.Input::get('subject').'<br>
<strong>Ticket Message: </strong>'.Input::get('content').'<br><br>

<strong>Conversation:</strong><br><br>

'.$fn.'<br>
'.$time.'<br>
'.$stc.'<br>
---------------------------------<br><br>


Thank You,<br>
Support Team');
}
?>

What I'm trying to do is email a support ticket conversation to one recipient. Like this:

enter image description here

I'm getting the "Original Message" from the Message $_POST. And the email address and email message from the New Data $_POST.

For the Conversation I'm trying to pull that info from the database table.

My current code emails the conversation, but emails each conversation in multiple emails.

Example, If there are 3 entries in the conversation it sends 3 emails:

  • Email 1 contains: Admin reply 1
  • Email 2 contains: User reply 1
  • Email 3 contains: Admin reply 2

I think if I get the echo working it will group the entire conversation into one email.

This is an example of the final email output I trying to obtain:

Hello -

Hey Please view this ticket and take care of the problem. Thanks

Ticket Info:

  • Status: OPEN
  • Ticket #: 201406016
  • Posted By: User Name
  • Posted On: June 27, 2014 - 03:46 pm
  • Building Unit: D47
  • Subject: 1
  • Ticket Message: 1

Conversation:

Support Team

July 01, 2014 - 09:19 am

Admin reply 1


User Name

July 01, 2014 - 09:19 am

User reply 1


Support Team

July 01, 2014 - 09:19 am

Admin reply 2


Thank You,

Support Team


END Email Example

Any suggestions would be helpful.

UPDATE

I'm starting to understand this a little better. The only error I'm receiving is in my foreach loop not sure how to correct it.

Here is the code:

$st_messages = DB::getInstance()->query("SELECT `id`,`st_id`,`from_name`,`st_content`,`st_time` FROM `st_messages` WHERE `st_id` = $st_id ORDER BY id ASC");

//START EMAIL
$recMessage = Input::get('rec_message');
User::sendNewticket($send_to, 'Maintenance Ticket '.Input::get('st_id'), <<<TEXT
Hello -<br><br>
$recMessage
TEXT

foreach ($st_messages->results() as $mt) {

$fn = "echo escape ($mt->from_name);";
$time = "echo escape (date(\"F d, Y - h:i a\", strtotime ($mt->st_time)));";
$stc = "echo nl2br (escape($mt->st_content));";
}


<<<TEXT2
<strong>Conversation:</strong><br><br>
 
$fn<br>
$time<br>
$stc<br><br>

Thanks,<br>
Support Team
);
TEXT2

?>
Community
  • 1
  • 1
daugaard47
  • 1,726
  • 5
  • 39
  • 74

1 Answers1

2

I believe this is what you were intending to do, it's a bit awkard looking:

$fn = escape($mt->from_name);
$time = escape(date("F d, Y - h:i a", strtotime($mt->st_time)));
$stc = nl2br(escape($mt->st_content));

//START EMAIL
User::sendNewticket($send_to, 'Maintenance Ticket ' . Input::get('st_id'), <<<TEXT
Hello -<br><br>
TEXT
        . Input::get('rec_message') . <<<TEXT2
<br><br>

<strong>Conversation:</strong><br><br>

$fn<br>
$time<br>
$stc<br>

TEXT2
);

The start of a heredoc must be the last thing on a line, so after that you drop down and immediately go into the text block. The end of the heredoc must be the only thing on the line, so to concatenate it you have to drop down a line before you can use the operator (.). You also don't need to use any quotation marks or append operators when inside a heredoc.

It's so unintuitive in fact, that even the StackOverflow Syntax highlighter gets it wrong, the first <<<TEXT works fine when I ran this:

$mt = new stdClass();
function escape($v){return $v;}
class User{static function sendNewTicket($a, $b, $c){echo $c;}}
class Input{static function get($a){return 'Got something';}}
$send_to = '';
$mt->from_name = 'Kitty';
$mt->st_time = '5th November';
$mt->st_content = <<<CONTENT
    Dear Judy,

        Lorem Dipsum and all that jazz.

        Toodles,
            Kitty.
CONTENT;

$fn = escape($mt->from_name);
$time = escape(date("F d, Y - h:i a", strtotime($mt->st_time)));
$stc = nl2br(escape($mt->st_content));

//START EMAIL
User::sendNewticket($send_to, 'Maintenance Ticket ' . Input::get('st_id'), <<<TEXT
Hello -<br><br>
TEXT
        . Input::get('rec_message') . <<<TEXT2
<br><br>

<strong>Conversation:</strong><br><br>

$fn<br>
$time<br>
$stc<br>

TEXT2
);

It produced:

Hello -<br><br>Got something<br><br>

<strong>Conversation:</strong><br><br>

Kitty<br>
November 05, 2014 - 12:00 am<br>
    Dear Judy,<br />
<br />
        Lorem Dipsum and all that jazz.<br />
<br />
        Toodles,<br />
            Kitty.<br>

Though I'm sure it'll look better with your data.

Edit: And as Blizz very correctly points out below, if you save the rec_message into a variable, you can avoid the concatenation altogether:

$recMessage = Input::get('rec_message');
User::sendNewticket($send_to, 'Maintenance Ticket ' . Input::get('st_id'), <<<TEXT
Hello -<br><br>
$recMessage
<br><br>

<strong>Conversation:</strong><br><br>

$fn<br>
$time<br>
$stc<br>

TEXT
);

Edit 2: With the extra messages, you should use something like this to simplify the email:

$messages = '';
foreach($st_messages->results() as $mt)
{

    $fn = escape($mt->from_name);
    $time = escape(date("F d, Y - h:i a", strtotime($mt->st_time)));
    $stc = nl2br(escape($mt->st_content));

    $messages .= <<<MESSAGE

$fn<br>
$time<br>
$stc<br><br>

MESSAGE;
}

$recMessage = Input::get('rec_message');
User::sendNewticket($send_to, 'Maintenance Ticket ' . Input::get('st_id'), <<<TEXT
Hello -<br><br>
$recMessage
<strong>Conversation:</strong><br><br>

$messages

Thanks,<br>
Support Team

TEXT
);
?>

Which with some basic dummy text produces:

Hello -<br><br>
Got something
<strong>Conversation:</strong><br><br>


Person<br>
November 11, 2014 - 12:00 am<br>
My message blah blah<br><br>

sdfasdf<br>
April 11, 2014 - 12:00 am<br>
My message asdfasdf blah blah<br><br>


Thanks,<br>
Support Team
Community
  • 1
  • 1
MrLore
  • 3,759
  • 2
  • 28
  • 36
  • 2
    If you would capture the output of `Input::get('rec_message')` in a variable as well before you start the `HEREDOC`, there would be no need to actually interrupt it. – Blizz Jul 02 '14 at 06:15
  • Looks a lot cleaner :) – Blizz Jul 02 '14 at 06:58
  • @MrLore I tried your method, and it worked as far as the code goes, but still received 3 multiple emails containing each reply from the conversation separately. I updated my question with full code and more detail. – daugaard47 Jul 02 '14 at 07:04
  • @MrLore everything is working as it should, but can't seem to figure out how to get the 3 replies from the conversation into the email. Am I missing something here? – daugaard47 Jul 02 '14 at 14:14
  • @echo Don't you just need to add the messages with their html together in this loop? `foreach ($st_messages->results() as $mt)` Then you'd have all the messages in a single variable. – MrLore Jul 02 '14 at 14:22
  • Yes, but when I do this it sends 3 separate emails instead of one email with all the replies from the conversation. – daugaard47 Jul 02 '14 at 14:24
  • @MrLore I added an update, if you could take a look if you get a chance, I would appreciate it. – daugaard47 Jul 02 '14 at 15:49
  • @MrLore That worked great thanks so much! Just curious though, so I can understand this better. How did the `$messages = '';` help solve the problem? – daugaard47 Jul 02 '14 at 16:32
  • 1
    @echo It just allows us to build up the conversation so we can put all of them into the email with the one variable, it would be a real mess to try and concatenate three heredocs together with the for loop in the middle. – MrLore Jul 02 '14 at 16:33