Login | Sign up | Toll-Free: 1-800-752-0455



9 posts by 2 authors in: Forums > CMS Builder
Last Post: November 14, 2013

I'm using the sendMessage function to sent mail.

I would like to have the sendMessage function return the ID of the log record created.

This will allow us to link the sent message to a view screen in a ticket system we are building.

Any help would be appreciated.

Hi Brownleather, 

By "of the log created" do you mean the record created in "_outgoing_mail"?

If so, there's no really easy way to get at that, but you could check for the highest record number before and after sending a message:

  list($latestRecordNum1) = mysql_get_query("SELECT num from {$TABLE_PREFIX}_outgoing_mail ORDER BY num DESC", true);
  // send message here...
  list($latestRecordNum2) = mysql_get_query("SELECT num from {$TABLE_PREFIX}_outgoing_mail ORDER BY num DESC", true);
  $newLogRecordNum = 0; 
  if ($latestRecordNum2 > $latestRecordNum1) { $newLogRecordNum = $latestRecordNum2; }

Unless you know that for sure logging will ALWAYS be enabled, and that you're checking for errors from sendMessage() then you could just do this:

// send message here...
list($newLogRecordNum) = mysql_get_query("SELECT num from {$TABLE_PREFIX}_outgoing_mail ORDER BY num DESC", true);

Hope that helps, let me know any questions!

Dave Edis - Senior Developer

Hmm, well mysql_insert calls this function to get the last insert id: http://php.net/mysql_insert_id

  $recordNum = mysql_insert_id();

So if you know logging is on and there's no other plugin hooks or mysql_inserts after that code (I don't see any) then you could just do this:

// send message code
$lastLogNum = mysql_insert_id();

It only returns the ID of the last insert of the current running script, so you wouldn't have to worry about race conditions (the situation where the newest record is not the one that was just sent).

Let me know if that would work for you.

Dave Edis - Senior Developer

And an altogether different approach would be to write a wrapper function sendTicketMessage() that logs ticket messages to another table and works exactly as needed returning the latest log if (from a new table).  Because you may find that outgoing mail has some extra fields or isn't exactly what you want (unless it's just for debugging/log purposes).  I know it gets full of messages on our site and I like to clear it out from time to time.

Anyways, hopefully one of those approaches will work! 

Dave Edis - Senior Developer

Wow! I'm thrilled with your response time!

I'll give that a try and let you know if it works.

Thank you very much.

[I don't feel good asking this.. but I'm desperate... can you sprinkle some of your  super power powder on this?



Glad it worked.  I'll figure out that Download Mail issue.  It's been escalated to me already so it must be tricky.  Stay tuned! 

Dave Edis - Senior Developer

Hi Dave,

Any updates with the Download Mail issue?

Yep, I'll respond on that other thread shortly.  Stay tuned!  Thanks.

Dave Edis - Senior Developer