1

I am working on a project in which I need to send email. My send mail function is:

    ini_set('sendmail_path', "\"C:\xampp\sendmail\sendmail.exe\" -t");
    ini_set('smtp_server', 'smtp.gmail.com');
    ini_set('smtp_port', 25);
    ini_set('smtp_ssl', 'auto');
    ini_set('error_logfile', 'error.log');
    ini_set('auth_username', 'myemailAddreds@gmail.com');
    ini_set('auth_password', 'mygmail_password');

    //sendmail_from('myemailAddreds@gmail.com');

    $to = 'myemailAddreds@gmail.com';
    $subject = 'Hello from XAMPP!';
    $message = 'This is a test';
    $headers = "From: your@email-address.com\r\n";
    if (mail($to, $subject, $message, $headers)) {
       echo "SUCCESS";
    } else {
       echo "ERROR";
    }

But I am getting following error

Warning: mail(): Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() in C:\umtab\xampp\htdocs\umtab\email.php on line 23

Cristik
  • 30,989
  • 25
  • 91
  • 127
  • 1
    Far better to use PHPMailer then you can connect to the SMTP server direct from PHP rather than adding a layer of complications with the sendmail settings – ADyson Sep 28 '21 at 11:32
  • Please tell us if our solutions worked for you! – Andrea Sep 29 '21 at 10:07

1 Answers1

1

As you can see from the error: you get Failed to connect to mailserver at "localhost" port 25. It's written localhost, like the server is not properly set.

I think that ini_set('smtp_server', 'smtp.gmail.com'); is not working.

Try to use ini_set('SMTP','smtp.gmail.com'); instead ... that should be the correct way to set the SMTP server!

But anyway, as suggested by @ADyson, it would be better using PHPMailer. You can read the documentation and the usage right here.

Andrea

Andrea
  • 181
  • 9
  • Ok thank, I've tried that but still I got this *error* ```//Warning: mail(): SMTP server response: 530 5.7.0 Must issue a STARTTLS command first. l7sm1379142qtr.87 - gsmtp in C:\umtab\xampp\htdocs\umtab\email.php on line 24 ``` – Umar Tahir Bako Sep 30 '21 at 09:49
  • I will try using PHPMailer – Umar Tahir Bako Sep 30 '21 at 09:50
  • I suggest you to have a look to [**this post here**](https://stackoverflow.com/questions/10509699/must-issue-a-starttls-command-first) ... it explains how to figure it out. If my answer helped you, please mark it as the solution :) – Andrea Sep 30 '21 at 15:37
  • I will try it soon, thanks for your assistance – Umar Tahir Bako Oct 04 '21 at 10:41
  • Hi there, did you solve the problem? If yes, please mark my answer as solution! – Andrea Nov 26 '21 at 11:22