Get the ID of the sent email log record

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

By Brownleather - November 6, 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.

By Dave - November 12, 2013

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

By Brownleather - November 12, 2013

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.

By Dave - November 12, 2013

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

By Dave - November 12, 2013

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

By Brownleather - November 12, 2013

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?

http://www.interactivetools.com/forum/forum-posts.php?downloadMail---Downloading-email-error-79099

;)

By Dave - November 12, 2013

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

By Dave - November 14, 2013

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

Dave Edis - Senior Developer
interactivetools.com