-1

This is my first time trying to send email using php. I used this script which I found on W3school however it gives me the error shown below. What did I miss here?

<?php

$to = "somebody@example.com";
$subject = "My subject";
$txt = "Hello world!";

mail($to,$subject,$txt);
?>

Error:

Warning: mail() [function.mail]: Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set()

Rasclatt
  • 12,498
  • 3
  • 25
  • 33
  • 1
    Try and avoid w3school, see here http://www.w3fools.com/ it sounds like you're running this php on your local machine so you probably don't have mail set up. See this answer http://stackoverflow.com/a/5342867/1812355 – Coz Oct 20 '15 at 02:16
  • @Coz Other options include using a wrapper class such as SwiftMailer or PHPMailer does he mean -->
    ?
    – Yan Shuang Low Oct 20 '15 at 02:23
  • That's a css or html wrapper, in this context a 'wrapper' is a helper around a bunch of files letting you use the code easily. Just click on those links and have a read of the documentations :) – Coz Oct 20 '15 at 02:27
  • @Coz i read it but i was damn confused. All i saw was a sample code of the form, but i dont want the form, i just want to send my msg to the email. No other easy way out? – Yan Shuang Low Oct 20 '15 at 03:13
  • http://wiki.uniformserver.com/index.php/PHP_mail_function there's a tutorial there about setting up your php.ini file so it'll send mail – Coz Oct 20 '15 at 13:06

1 Answers1

0

mail() in PHP depends on an e-mail server. Without one, PHP can't send any emails.

Now, this is the important part: you can connect to a e-mail service like Gmail using SMTP to send e-mail, or have your own little local server which would usually run on localhost:25, as it tried to connect to. Some server stacks have email built in, but others not.

As the error states, you can change the addresses in php.ini or using ini_set.

Raymonf
  • 114
  • 1
  • 16