Emails not being sent in php

When I try to send an email with PHP and this code:

$to = "myicloud@icloud.com";
$subject = "Test";
$txt = "Test";
$headers = "From: mygmail@gmail.com";
mail($to,$subject,$txt,$headers);

my php.ini [mail function] :


[mail function]

SMTP=smtp.gmail.com
smtp_port=587
sendmail_from = mygmail@gmail.com
sendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" -t"

and sendmail.ini :


[sendmail]

smtp_server=smtp.gmail.com
smtp_port=587
error_logfile=error.log
debug_logfile=debug.log
auth_username=mygmail@gmail.com
auth_password=XXXXXXXXX

Can you please tell me what is wrong? There is no error and mygmail@gmail.com is my not what I typed in and same with myicloud@icloud.com and XXXXXXXXX

Don’t you need to enable SSL when sending via gmail? I recently did a VB project which required this. I also needed to sign into gmail and tell it to allow less secure apps to send email.

Generally people advise to steer clear of the built-in mail() function and use something like PHPMailer instead.

Not sure what this bit means sorry, perhaps you could clarify:

’ and mygmail@gmail.com is my not what I typed in and same with myicloud@icloud.com and XXXXXXXXX’ means that in my code stuff like XXXXXXXXX is not XXXXXXXXX, but say my password and mygmail@gmail.com is my gmail in my code and myicloud@icloud.com is my icloud email. I will try phpmailer and will try using ssl.