How to prevent anyone from using localhost to send email

I have a PHP script that is executed via a cron job and sends emails. I want to ensure that emails are sent only after successful SMTP authentication using a valid username and password. Without SMTP authentication, anyone can send emails from localhost and potentially spoof other users’ email addresses. The script should send emails only when SMTP authentication succeeds; otherwise, it should reject the request.