Error detection inside scripts?

Is there any way to detect inside a script that a certain service call failed?

In particular I have many places where I send an email using smtp platform, but if there is no Internet connection it fails with an error message in the log. Is there any way to check inside the script itself whether that service call failed or not?

Thanks!