Sending email by jQuery / PHP

How can you send an email by jQuery and PHP if the user flags a question?

My JS code in HEAD

jQuery('a.flag_question').live('click', function(){
    jQuery.post('/codes/handlers/flag_question.php', 
        { question_id: jQuery(this).attr('rel') });                                            
            alert ("Question was reported.");
});

Its handler flag_question.php which does not send me an email

$question_id = $_POST['question_id'];   // data from jQuery.post
$subject = "Flagged question"; 
$to = '"Michael Boltman <[email protected]>';
$headers = 'MIME-Version: 1.0' . "\r\n"; 
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; 

$message = "<a href='index.php?question_id=" . $question_id . "'>"
    . "The question " . $question_id . "</a> is flagged by an user. Please, review it.";
if ( mail( $to, $subject, $message ) ) {
    echo ("Thank you for your report!");

}

I did use my real email address, while the one in the code is pseudo one.

The link is generated by PHP

    echo ("<a href='#'"
            . " class='flag_question'"
            . " rel='" . $question_id . "'>flag</a>"
        );

Upvotes: 2

Views: 5160

Answers (3)

Omar Al-Ithawi
Omar Al-Ithawi

Reputation: 5170

You may not have a mail server! This happens usually when you're using a LocalHost on your computer.

Upvotes: 1

Greg Hewgill
Greg Hewgill

Reputation: 994897

Your call to PHP's mail function seems to send mail to [email protected]. I would be impressed if that is your real email address, otherwise you will need to use your actual email address. If you did, please mention that.

Things to check:

  • Check your "spam" folder on gmail to see whether gmail thinks your mail is spam. If so, there are lots of questions here on SO that answer "How do I make sure my email isn't considered spam?"
  • Check your local MTA (whatever you're running on your Ubuntu box) to see whether it reports the mail has been accepted from your PHP script and delivered to Gmail properly. If this is the problem, then serverfault.com is the correct place for diagnosing that problem.

Upvotes: 1

Brian Gianforcaro
Brian Gianforcaro

Reputation: 27220

You can checkout the documentation for php's mail function here.

Item of note: There are many people posting workarounds in the documentation comments because this function often seems to not work if your server/system isn't configured correctly.

This users comment might be useful (link)

Edward 01-Aug-2009 09:08

Currently my hosting service is on Godaddy. When attempting to use the mail function without the fifth parameter containing "-f", my message headers would not work.

Whenever your message headers do not work, simply try using the fifth parameter:

<?php
mail($to, $subject, $message, $headers, "[email protected]");
?>

Upvotes: 1

Related Questions