It is possible that SELinux is turned on and, by default, SELinux does not allow sending mail using apache / web server.
To verify that SELinux is enabled and configured to stop sending emails, run the following command:
getsebool -a | grep mail
output sampling
allow_postfix_local_write_mail_spool --> on httpd_can_sendmail --> off logging_syslogd_can_sendmail --> off
Now, to configure SELinux to allow apache to send email, run the following command:
setsebool -P httpd_can_sendmail on
Check this answer, it will solve my problem: Unable to send mail while working through the web server, but is able to send from the command line with the same php script
source share