php send mail from localhost

I am new at php. I was trying to send mail from php using this code.

<?php

    $to      = '[email protected]';
    $subject = 'The subject';
    $message = 'hello';
    $headers = 'From: [email protected]' . "\r\n" .
        'Reply-To: [email protected]' . "\r\n" .
        'X-Mailer: PHP/' . phpversion();

    mail($to, $subject, $message, $headers);

?>

I have change settings in php.ini

[mail function]
; For Win32 only.
; http://php.net/smtp
SMTP = localhost
; http://php.net/smtp-port
smtp_port = 25

; For Win32 only.
; http://php.net/sendmail-from
sendmail_from = [email protected]

& in sendmail.ini

# A freemail service example
account Gmail
tls on
tls_certcheck off
host smtp.gmail.com
from [email protected]
auth on
user [email protected]
password xxxxxxxxx

# Set a default account
account default : Gmail

Now code runs successfully but I am not getting any mail.


Solution 1:

You must change the php.ini file:

[mail function]
; For Win32 only.
; http://php.net/smtp
SMTP = localhost
; http://php.net/smtp-port
smtp_port = 25

; For Win32 only.
; http://php.net/sendmail-from
sendmail_from = you@yourdomain

It won't work if localhost is set, for that reason change to your mail server.

Solution 2:

You won't have SMTP server installed by default so you can't send emails from localhost directly. Either you can set up SMTP server on local or use third party SMTP servers. Have a look at http://www.mittalpatel.co.in/php_send_mail_from_localhost_using_gmail_smtp which gives you insight about how to send mail from localhost using third party SMTP server.

Solution 3:

The function will not work on your localhost, as the locahost doesn't works as a SMTP server, upload your content to a valid server with SMTP installed, and then go for the mail call.