Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm trying to email the contents of a JS array by encoding using JSON.stringify and then decoding in the PHP which should then be sent via email. I get a success alert that the data is sent to the PHP okay, but the email doesn't come through. Can anybody spot anything glaringly obvious I'm missing/got wrong, please?

Array has been populated via .push function and I can output that fine in the HTML, so I know it's populated.

Using ajax to encode my data string:

dataString = myArray; 

var jsonString = JSON.stringify(dataString);

 $.ajax({
    type: "POST",
    url: "script.php",
    data: {data : jsonString}, 
    cache: false,

    success: function(){
       alert("Success");
    }
});

Then in the PHP:

<?php
$data = json_decode(stripslashes($_POST['data']));

$to = "[email protected]";
$header = "Content-Type: text/html\r\nReply-To";
$subject = "This is my Subject Line";

$body = 
    @"
    <strong>The data is:</strong> $data
    ";


    if(mail($to, $subject, $body, $header)) {
        die("true");    
        } else {
            die("There was an error sending the email.");   
        }
?>

The email doesn't come through at all, and I don't get any error messages at all. Can anybody help, please? Thanks!

share|improve this question

3 Answers

Probably you don't have a mail server.

What you can do quickly is, go to this link,

http://www.toolheap.com/test-mail-server-tool/

Download the tool. follow the steps to run it.

Then change your code to this to see things that you can do,

<?php
$data = json_decode(stripslashes($_POST['data']));

$to = "[email protected]";
$header = "Content-Type: text/html\r\nReply-To";
$subject = "This is my Subject Line";

$body = 
@"
<strong>The data is:</strong> 
".print_r($data, true) ;


if(mail($to, $subject, $body, $header)) {
    die("true");    
    } else {
        die("There was an error sending the email.");   
    }
?>

I think you will get the value but how you will use it that's your choice.

Cheers.

share|improve this answer
Thanks for that - I've ran the tool and no email is getting sent... – Martin Smith Jun 5 at 11:01
I think you haven't configured it properly. You should get the mail locally. You can check it in your local store(configured folder). – Indrajit Das Jun 6 at 5:10

Has you got a mailserver in the PHP machine?

mail() returns true if the mail is accepted to delivery, but this doesn't mean it's sent at all.

share|improve this answer
Yes, the server should be set up correctly as I've sent emails via PHP before! – Martin Smith Jun 5 at 10:29
And where does your program end? At die("true") or at the second die()? – Antonio Jesús Sánchez Padial Jun 5 at 10:40

First, you can't convert $data array to string directly.

You have to use print_r($data, true) to insert contents of the array into a string

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.