Sending smtp email using mail()
mail() is a PHP function, that will never use SMTP.

You need to use the drupal mail manager and its mail function. See https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Mail%21MailManager.php/function/MailManager%3A%3Amail/8.4.x and the example code ther.