Sending email with jQuery / PHP
How can you send jQuery and PHP email if user asks 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 flag_question.php handler which doesn't send me an email
$question_id = $_POST['question_id']; // data from jQuery.post
$subject = "Flagged question";
$to = '"Michael Boltman <michael.boltman777@gmail.com>';
$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 actually used 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>"
);
source to share
You can check the documentation for the php mail function here .
Note: There are many people in the comments on the documentation that often don't work because this feature often doesn't work if your server / system is not configured correctly.
This user comment might be helpful ( link )
Edward 01-Aug-2009 09:08
Currently my hosting is powered by Godaddy. If I try to use the mail function without a fifth parameter containing "-f", my message headers won't work.
Whenever your post headers don't work, just try using the fifth parameter:
<?php
mail($to, $subject, $message, $headers, "-femail.address@example.com");
?>
source to share
Your PHP function call mail
seems to send mail to test@gmail.com
. I would be impressed if this is your real email address, otherwise you will need to use your actual email address. If you did, please indicate it.
Things to check:
- Check your gmail spam folder to see if the mail counts as spam. If so, there are many questions here that answer "How can I make sure my email address is not considered spam?"
- Check your local MTA (whatever you are using in your Ubuntu box) to see if the message was that the mail was received from your PHP script and delivered correctly to Gmail. If this is a problem, then serverfault.com is the right place to diagnose this problem.
source to share