1

I'm a beginner in php and found a simple mail function. Following is the code:

private function sendEmail() {

  $to = $this - > email;
  $subject = $this - > subject;
  $message = $this - > name. " : ". $this - > message;
  $header = "From: ".$this - > email;

  $mail = mail($to, $subject, $message, $header);

  echo $to, $subject, $message, $header, $mail, "End";

  if ($mail) {
    $this - > response_status = 1;
    $this - > response_html = 'Thank You!';
  } else {
    echo "Mail not found";
  }
}

When I call it from Javascript, it always lands in error block of $.ajax but no error message it thrown. When I check response of ajax call in network tab, I get proper values of all variables ($to, $subject, $name, $message, $header), but $mail is always blank.

Also I get Mail not found from else block.

Can anyone point me in the right direction?

I'm running it on localhost on XAMPP (on Ubuntu 15.10).

Following is the snapshot of response:

enter image description here

Rajesh
  • 24,354
  • 5
  • 48
  • 79
  • 2
    It's probably [`false` instead of blank](https://3v4l.org/XOofE). Debug it using [`var_dump()`](http://php.net/manual/en/function.var-dump.php) to see what it contains. – PeeHaa May 17 '16 at 16:07
  • you haven't shown us how you're calling it from Ajax. That's a separate issue. in PHP, the value of $mail will be either true or false. The fact you're going into the block in your function that returns "mail not found" suggests it's returning false. – ADyson May 17 '16 at 16:08
  • So is the actual issue here the fact that this code is being handled by your error block in your javascript? Show your Javascript, then... Also, this function doesn't `return` anything. Maybe that's your error, but we can't tell because we don't have enough code – WillardSolutions May 17 '16 at 16:15
  • @PeeHaa, you are right. Its returning false. Do I need a smtp server? I read on w3schools, `mail` does not require any installation. – Rajesh May 17 '16 at 16:16
  • You need at least some agent / transport capable of sending mails on your machine. `mail` just passes the mail to whatever is going to actually send it. Or use an external smtp server. – PeeHaa May 17 '16 at 16:18
  • @EatPeanutButter the issue is its throwing error. As **PeeHaa** has already clarified, I'm getting false. So issue is either in my PHP code or some missing configuration. – Rajesh May 17 '16 at 16:18

1 Answers1

2

How to configure XAMPP to send mail from localhost?

Check that out. Should work for ya, it's more than likely that you don't have any application to actually send the mail - e.g. sendmail for *nix.

Community
  • 1
  • 1
GunniH
  • 186
  • 1
  • 8
  • I did try to follow the steps but now getting another. I have installed *ssmtp* and now getting this error: *ssmtp: Cannot open smtp.gmail.com:587*. But still thanks for the help. – Rajesh May 18 '16 at 06:14
  • http://askubuntu.com/questions/677146/ssmtp-with-gmail-cannot-open-587587 - try this? – GunniH May 19 '16 at 11:24