tags:

views:

325

answers:

5

Hi

I have been trying to get a php contact form working on my portfolio site (currently on a free megabyet.net account), but on testing it(on the uploaded site) even though i get the thankyou/confirmation message, I still don't receive any message on my mail account (specified in the code), I can't seem to understand the problem here....help needed!

can it be something related to SMTP??


Here's the code :

<?php
if(isset($_POST['submit'])) {

$to = "[email protected]";
$subject = "Portfolio Contact";
$name_field = $_POST['name'];
$email_field = $_POST['email'];
$message = $_POST['message'];
$headers = "From: $email_field";
$body = "From: $name_field\n E-Mail: $email_field\n Message:\n $message";

echo "Mail has been sent, thankyou!";
mail($to, $subject, $body, $headers);

} else {

echo "blarg!";

}
?>

HTML Code:

<form id="contact_frm" action="mail.php" method="POST">
<h4>Name :</h4>
<input type="text" id="f_name" name="name"/><br/><br/>
<h4>E-Mail Address :</h4>
<input type="text" id="f_email" name="email"/><br/><br/>
<h4>Message :</h4>
<textarea id="f_msg" name="message" cols="22" rows="5"/></textarea><br/><br/>
<input id="send_btn" type="submit" value="Send >>" name="submit" /><br/>
</form>

+1  A: 

Firstly you should be checking if mail() returns true or not to determine if mail has been sent successfully:

<?php
if(isset($_POST['submit'])) {

    $to = "[email protected]";
    $subject = "Portfolio Contact";
    $name_field = $_POST['name'];
    $email_field = $_POST['email'];
    $message = $_POST['message'];
    $headers = "From: $email_field";
    $body = "From: $name_field\n E-Mail: $email_field\n Message:\n $message";

    $success = mail($to, $subject, $body, $headers);
    if ($success) {
        echo "Mail has been sent, thankyou!";
        // redirect to thank you page here
    }
    else {
        echo "message failed";
    }
} else {

echo "blarg!";

}
?>

Try that and let us know if that works.

Also, have you tried sending to a different email address? It may be that Yahoo is blocking that web host for spam. Being a free host it is a very likely scenario.

John Conde
i tried the code above and get a failed output =( ...details in my answer below ...
Vishu
A: 

Hello,

If you are looking for something related to sending email via SMTP. I would recommend you use Code Igniters mailer class.

http://codeigniter.com/user_guide/libraries/email.html

This also allows for debugging and handling SMTP errors gracefully.

Hugh
A: 

can it be something related to SMTP??

Probably. Why don't you check your mailq and the log files from your MTA?

symcbean
A: 

@John .. checked with that if condition with the code below and i get a failed output =/ ...so my mail() function is returning false =( ...and yea i've tried gmail but with the mail function not running fine on the first place.... it doesn't work...

<?php
if(isset($_POST['submit'])) {

$to = "[email protected]";
$subject = "Portfolio Contact";
$name_field = $_POST['name'];
$email_field = $_POST['email'];
$message = $_POST['message'];
$headers = "From: $email_field";
$body = "From: $name_field\n E-Mail: $email_field\n Message:\n $message";

$success = mail($to, $subject, $body, $headers);

if($success) { 
 echo "Mail has been sent, thankyou!";

} else {
 echo "message sending failed!";
 }
} else {
 echo "blarg!";
 }
?>

output- message sending failed!

so, do I need to define some extra params here?...also i saw that my host has given the path to sendmail as -- /usr/sbin/sendmail does it has anything to do with my mail function acting bad?...i mean do I need to define the sendmail param in it?

@unknown- hmm codeigniter may help, but i've never used it before...let's see...

@symcbean- sorry i don't know how to do that :P...probabaly cuz i'm not very well versed with SMTP yet?.... still a learner/beginner...

Vishu
A: 

If the E-Mail goes out correctly, but never arrives, it could be that it gets caught by a spam filter. A few bullet points I wrote in reply to an similar question a few months ago:

  • Does the sender address ("From") belong to a domain on your server? If not, make it so.
  • Is your server on a blacklist (e.g. check IP on spamhaus.org)? This is a remote possibility with shared hosting.
  • Are mails filtered by a spam filter? Open an account with a freemailer that has a spam folder and find out. Also, try sending mail to an address without a spam filter.
  • Do you possibly need the fifth parameter "-f" of mail() to add a sender address? (See mail() command in the PHP manual)
  • If you have access to log files, check those, of course, as suggested above.
  • Do you check the "from:" address for possible bounce mails ("Returned to sender")? You can also set up a separate "errors-to" address.
Pekka
i made the sender address belong to my domain on my server, it solved the issue of mail() function returning false, now i get a success message (from the revised code), means the mail() returns true, but still i dont receive any mail, also, checked my spam folder nothing there, and checked the host at spamhaus - it's not in the blacklist...log files don't show anything relevant, though i'll have to investigate the '-f' param...if you have any more suggestions/advice, please do let me know..thanks for all the help so far...
Vishu
Can you try sending mail to an address that has no spam blocking to make sure it gets sent out properly?
Pekka