treehouse : what would you like to learn today?
Web Design Web Development iOS Development

sending e-mail via php

  • Hi,

    I am sending email using php when any user added comments to my comments but i did not received any mail .
    i used mail() function for sending mail.

    could any one plz tell me how to solve this issue and is any alternate methods are there for sending using PHP.

    regards,
    Raghavendrachari
  • Have you tried using the mail function separately? What I mean is, have you tried letting it send you the email after filling in a form and clicking the submit button? Maybe there is something wrong and your code won't recognize it correctly when someone added a comment. Even better maybe would be to see the code.
  • This is the code which i am using

    <?php
    $to = 'sender_to@gmail.com';
    $subject = 'the subject';
    $message = 'hello';
    $headers = 'From: sender_from@gmail.com' . "\r\n" .
    'Reply-To: sender_from@gmail.com' . "\r\n" .
    'X-Mailer: PHP/';
    echo "<p></p>";
    if (mail($to, $subject, $message, $headers)) {
    echo("<p>Message successfully sent!</p>");
    } else {

    echo("<p>Message delivery failed...</p>");
    }

    ?>
  • Is it saying 'Message sent successfully'?
  • No .. it displays 'Message delivery failed...!!!'

  • Then it's not that you aren't 'receiving' the mail, the mail is not even being sent because there is an error somewhere in your script.

    To be honest with you though, if you were going to copy code from http://php.net/manual/en/function.mail.php you should at least copy properly! You've quite clearly deleted phpversion() from the code. However, if this doesn't fix the problem, remove the headers parameter from the function and just try the basic $to, $subject, $message mail function. If this also fails then it isn't your code's fault

    Also, why have you output an empty paragraph on the page?!
  • Hi,

    Thank U

    I tried what u said but same problem facing ; after header parameter removed same 'Failure error message'.

    Is there any alternate method or how can i checkl php error logs,where it was stored in logs.

    regards,
    Raghavendrachari
  • Are you testing this on a local or live server?
  • on local server having internet access.. !!

    i tried from other system via internet .. also same issue is coming..!!

    is there any possibility of source code which will execute properly ?? plz send

    thank U
  • Testing mail functions on a local server requires further set up. Try testing it on a live server.