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

Forum

 

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
interactivetools.com

Hi Dave,

Thanks for your reply,

I am currently using a similar method to the one you describe.

The issue is that I have a few apps that use the send mail function, conceivably, there can be a situation where the newest record is not the one that was just sent.

I was hoping that there would be a way to have the mysql_insert function (which writes the outgoing log record) pass the id back to the sendMessage function.

Thanks for your help.

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
interactivetools.com

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
interactivetools.com

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
interactivetools.com

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
interactivetools.com