How can I reset mail_smtppass in Outbound Emails either via API or database?

Currently we've got Sugar setup on an EC2 instance in AWS (only web application, rest is separated out into separate services).

One issue I'm trying to work out is this:

  1. EC2 instance gets restarted or terminated (for example when we're modifying the underlying AMI of the instance)
  2. mail_smtppass for Outbound Emails is no longer valid, so all outbound emails fail. I'm guessing this is because the hash in the database is no longer valid.

Currently I fix this by:

  1. Going to admin panel in Sugar and manually setting the password for the system email again.
  2. Doing an update on the Outbound Emails table in the database to copy the hash for the system user to all other users that use the same SMTP server.

The issue I have is that this is a very manual step in an otherwise automatic process, and if it's forgotten it can cause big issues.

Does anyone have a solution for me, where I could either use the API or run a local script on the instance to automate this?

Basically I want to get the correct hash into the mail_smtppass field, but I haven't been able to work out the correct way to hash the password.

Help greatly appreciated!