-2
<?php
    $to = "[email protected]";
    $subject = "My subject";
    $txt = "Hello world!";
    $headers = "From: [email protected]";

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

I was trying to send mail. It's not working I don't know why. I am not getting any error either. I am not getting any mail. I tried by different email addresses also.. Should I edit any file in lampp/xampp?

var_dump is always returning bool(false)

I have changed some code 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]

; For Unix only.  You may supply arguments as well (default: "sendmail -t -i").
; http://php.net/sendmail-path
;sendmail_path =

; Force the addition of the specified parameters to be passed as extra parameters
; to the sendmail binary. These parameters will always replace the value of
; the 5th parameter to mail(), even in safe mode.
;mail.force_extra_parameters =

; Add X-PHP-Originating-Script: that will include uid of the script followed by the filename
mail.add_x_header=On

; Log all mail() calls including the full path of the script, line #, to address and headers
;mail.log =

Although I am having the same issue

3
  • Do you have a SMTP server installed locally?
    – Panki
    Commented Mar 30, 2021 at 10:51
  • If you are using Kali you should not need to ask this.. how do you expect a mail to be sent without an SMTP server?
    – Panki
    Commented Mar 30, 2021 at 10:57
  • Why are you changing configuration that's apparently "For Win32 only" and then asking about it on a Unix & Linux site?
    – muru
    Commented Mar 30, 2021 at 11:09

1 Answer 1

1

In order for the php mail function, you will need some backend work, namely a properly configured SMTP service. You will need to check your php.ini for the relevant configuration in the [mail function] section, and set this up to be able to access the aforementioned, properly configured SMTP service.

As the default value is localhost, if you have not setup SMTP on your localhost or if it is not configured for forwarding mail to the Internet, your PHP mail function will not work.

1
  • 1
    [comment after your edit] As you can see, there are some things that you need to take care of in order for PHP to be able to send emails. If you are running PHP on linux, you most probably need to setup the postfix package and then try to set it up to be able to send mails to the internet. The best way to do this is by having postfix use another publicly available SMTP server (for example linode.com/docs/guides/…)
    – Panos
    Commented Mar 30, 2021 at 19:17

You must log in to answer this question.