tags:

views:

1633

answers:

3

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>"
        );
+1  A: 

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]");
?>
Brian Gianforcaro
**What should I put to the variable *$headers*?**
Masi
Try: $headers = 'MIME-Version: 1.0' . "\r\n";$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
Brian Gianforcaro
I do not receive emails to my email address with your code. - This suggests me that the problem is apparently in MTA.
Masi
+1  A: 

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.
Greg Hewgill
@Greg: I installed NullMailer to my Ubuntu. MTAs are new to me. I changed my details in the email such that my mail is not filtered as spam. I have never used MTAs before. - **How can you see if NullMailer has sent the mails?**
Masi
Does NullMailer write log files to detail its activity? If so, have a look there. If not, then I'm not sure what to suggest, as I don't use NullMailer.
Greg Hewgill
@Greg: What is your MTA?
Masi
I use Postfix: http://www.postfix.org
Greg Hewgill
+1  A: 

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

Omar Dolaimy
I am using a localhost on my computer. **How can you set up a mail server?**
Masi
This for Ubuntu: https://help.ubuntu.com/community/MailServerAnd this for windows: http://www.hmailserver.com/A full list of available e-mail servers is here: http://en.wikipedia.org/wiki/Comparison_of_mail_servers
Omar Dolaimy