0

I'm trying to implement a PHP mail function in my aws Windows Server 2016 but after using PHP mail function mail is not received:

<?php
    ini_set();
    $to = "km.kuldeepmourya@gmail.com";
    $subject = "My subject";
    $txt = "Hello world!";
    $headers = "From: webmaster@example.com" . "\r\n" .
        "CC: somebodyelse@example.com";

    $check=mail($to,$subject,$txt,$headers);

    if($check)
        echo"mail sent";
    else
        echo"mail not sent";
?>  

Output:

Warning: ini_set() expects exactly 2 parameters, 0 given in 
C:\xampp\htdocs\mymail.php on line 2
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:\xampp\htdocs\mymail.php on line 9 mail not sent
krlzlx
  • 5,752
  • 14
  • 47
  • 55
iamkdblue
  • 3,448
  • 2
  • 25
  • 43

2 Answers2

0

The mail() function will always return false on PHP unless you define sendmail_from and sendmail_path in your php.ini file on your server. Please make sure these values are defined.

For more information on these values: http://php.net/manual/en/function.mail.php

If you do not have access to the ini file you could also use ini_set() in the php script to set these values or in the additional header parameter of the function.

quick note: you would see a warning associated with these being undefined if you had warnings on. I'd recommend turning the warnings on in a development environment so you don't get stuck on stuff like this in the future.

Mark Berube
  • 206
  • 3
  • 11
  • The warning message clearly shows that it is trying to connect to a local MTA, but there isn't one running. – gview Feb 15 '17 at 15:43
  • I do agree that an explicit config is better, or even required if the user doesn't plan on using an MTA on the same machine. – gview Feb 15 '17 at 15:50
0

The warning message tells you what you need to know. PHP is trying to connect to an MTA (mail server) on localhost port 25. It is not finding an MTA there. This is not really a PHP question, but rather a mail server on windows question. Windows calls this an SMTP server, since that is the mail transfer protocol. You would either need to install a mail server for windows, or install the packaged SMTP server that comes with Windows Server, as described here.

gview
  • 14,876
  • 3
  • 46
  • 51