0

I have a website with a form where you can send a message to my email adress. But it doesn't work yet. I am not really sure what is required to do this. Here is what i have done so far:

The PHP:

<?php   
if (isset($_POST['from']) && isset($_POST['header']) && isset($_POST['message'])) { 
 $to = "example@awesomemail.com";
 $subject = $_POST['from'];
 $body = $_POST['message'];
 $headers = $_POST['header'];

 if (mail($to, $subject, $body, $headers)) {
   echo("<p>Email successfully sent!</p>");
   echo json_encode('success');
  } else {
   echo("<p>Email delivery failed…</p>");
   echo json_encode('failed');
  }


} 
?> 

The JS:

$.ajax({
       type : "POST",
       url : "sendmail.php",
       data:  {"from": from, "header": header, "message": message},
       dataType: "json",
       success: function(msg){  
           alert(msg);     

       }
});

Is there something wrong with my code or am i missing something? The email is supposed to be sent to a gmail account.

Koiski
  • 568
  • 2
  • 8
  • 23
  • Why do you use json as your dataType but also echo out json and plain text? – Liam Sorsby Jan 30 '14 at 17:48
  • seems odd that you would be passing the header in the post. That is an optional setting for php mail, try it without the header declaration and see if you get a plain text email. – pathfinder Jan 30 '14 at 17:53
  • The php doesn't echo anything with the if isset statement. if i remove it i get this: Notice: Undefined index: from in C:...sendmail.php on line 4 Notice: Undefined index: message in C:.....php on line 5 Notice: Undefined index: header in C:...sendmail.php on line 6 Email successfully sent! "valid" – Koiski Jan 30 '14 at 17:55
  • also the alert message with the "msg" never comes up and removing the header part didn't work either – Koiski Jan 30 '14 at 17:56

3 Answers3

1

Try it like this , try sending a complete json response from the server side to client side for it to parse..

PHP :

<?php

  if (isset($_POST['from']) && isset($_POST['message'])) 
  { 
     $to = "example@awesomemail.com";
     $subject = $_POST['from'];
     $body = $_POST['message'];

     $headers =  urldecode($_POST['header']);

     if (mail($to, $subject, $body, $headers))
     echo json_encode('success'); 
     else 
     echo json_encode('failed');

    } 
    else
    echo json_encode('failed');

     ?> 

JS :

    $.post('sendmail.php',{"from": from ,"header" :header, "message":message},function(response) 
   {     
        var response = $.parseJSON(response);           
        console.log(response);

   });
Aditya
  • 4,453
  • 3
  • 28
  • 39
1

Not enough points to comment on question... but just wanted to confirm you have an Mail Transfer Agent set up on the system. If not, that may be the culprit.

If this hasn't yet been done yet, you can refer to sendmail: how to configure sendmail on ubuntu? or https://help.ubuntu.com/12.04/installation-guide/i386/mail-setup.html to get that going.

Community
  • 1
  • 1
Blake Petersen
  • 993
  • 7
  • 11
  • haha yes i thought i might be needing something like this :) i will set it up and come back here with the result – Koiski Jan 30 '14 at 18:42
  • Awesome! If you need a service through which you can send the emails, [SendGrid](http://www.sendgrid.com) offers 200 emails/day for free and has documentation as to how you can quickly set it up using [sendmail](http://sendgrid.com/docs/Integrate/Mail_Servers/sendmail.html) as well as [PHP code examples](http://sendgrid.com/docs/Code_Examples/php.html). – Blake Petersen Jan 31 '14 at 05:03
  • Thanks for your help everything works now :) Ended using this guide: https://www.digitalocean.com/community/articles/how-to-install-the-send-only-mail-server-exim-on-ubuntu-12-04 – Koiski Feb 02 '14 at 15:43
0

if sending header is a problem you can do like this: first concat all values into one variable

 var string = 'mail='+from+'/'+header+'/'+message;

$.ajax({
   type : "POST",
   url : "sendmail.php",
   data:  string,
   dataType: "json",
   success: function(msg){  
       alert(msg);     

   }
});

sendmail.php

<?php   
if (isset($_POST['mail']) && !empty($_POST['mail'])) { 
 $msg = explode('/','$_POST['mail']');

 $to = "example@awesomemail.com";
 $subject = $msg[0];
 $headers = $msg[1];
 $body = $msg[2];

 if (mail($to, $subject, $body, $headers)) {
   echo("<p>Email successfully sent!</p>");
   echo json_encode('success');
  } else {
   echo("<p>Email delivery failed…</p>");
   echo json_encode('failed');
  }


} 
?> 

This answer is just another alternative way, more based on your comment. (you can perform javascript validation before submitting form or with condition before even entering jquery/ajax)

Suman KC
  • 3,478
  • 4
  • 30
  • 42