How to use add_action(“wpcf7_before_send_mail” outside wp loop?

Just simply put your add_action hook in a simple plugin. Activate the plugin and watch it run with the rest of the plugins.

If you insist on putting the add_action in the theme’s functions.php then simply define a constant in your point of entry script, like this: define('SCRIPTONOMY', true);

Finally run a conditional in the functions.php testing for the constant.