The easiest way is to tie into the WordPress action publish_post
. Here is a link to the codex with a basic example:
https://codex.wordpress.org/Plugin_API/Action_Reference/publish_post
Note: If you are looking to do this for a Custom Post Type, you’ll need to add an action for publish_custom_post_type
.