send mails using mail() ?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • swetha123
    New Member
    • Dec 2008
    • 5

    send mails using mail() ?

    hello,


    can any tell me how to send mails from site using mail() in php

    do we need to change in the php.ini
    these are in php.ini


    [mail function]
    ; For Win32 only.
    SMTP = localhost
    smtp_port = 25

    ; For Win32 only.
    sendmail_from = me@example.com


    and i tried with this example it is giving me the message saying "message successfully sent" but there is no mail in my inbox

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    </head>
    
    <body>
    <?php
    $to = "swetha123@example.com";
    $subject = "Hi!";
    $body = "Hi,\n\nHow are you?";
    
    if (!is_null($to))
    {
      mail($to, $subject, $body); 
      echo("<p>Message successfully sent!</p>");
    } else {
      echo("<p>Message delivery failed...</p>");
    }  
    ?>
    </body>
    </html>
    Last edited by Atli; Dec 24 '08, 08:10 AM. Reason: Replaced real email address with an example.
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    Hi.

    You aren't really checking whether the mail was actually sent. You are completely ignoring the return value of the mail() function, printing a success message regardless of it's success or failure.

    You need to do something more like:
    [code=php]
    if(!empty($to)
    && !empty($subject )
    && !empty($message ))
    {
    if(mail($to, $subject, $message)) {
    echo "Success";
    } else {
    echo "Failed!";
    }
    }
    else {
    echo "Required data missing.";
    }[/code]

    Also, the PHP config directives you posted must contain real values for an active SMTP server. PHP must have access to a real SMTP server to be able to send mail.

    If there is an active SMTP server available on the localhost via port 25, what you posted will work fine.

    Comment

    Working...