简体   繁体   中英

PHP Mail function is not working ..!

Actually, I am using LOCALHOST . I've created a form to allow visitors to contact me via email. I am using PHP mail function but it doesn't work. The error is which is mentioned in the else statement.

HTML

<form  id="contact" method="post" action="contact us.php">
        <h1>Contact us</h1>
        <label for="subject">Subject:(required)</label>
        <input type="text" name="subject" placeholder="Enter your subject.." required/></br>
        <label for="email">E-mail:(required)</label>
        <input type="email" name="email" placeholder="Enter your email.." required/></br>
        <label for="website">Website:(optional)</label>
        <input type="url" name="website" placeholder="Enter your website.."/></br>
        <label for="message">Message:(required)</label>
        <textarea placeholder="Enter your message.." required="required" rows="12" name="message"></textarea></br>
        <input type="submit" name="send" value="Send message"/>
    </form>

PHP

 <?php
if(isset($_POST['send']))
{
$to = "some@email.com";
$from = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$header = "From:".$from;
    mail($to, $subject, $message, $header);
    if(mail($to, $subject, $message, $header))
    {
        echo "<script>alert('mail was sent successfully!');</script>";
    }
    else
    {
        echo "<script>alert('mail was not sent!');</script>";
    }
}
?>

The main error which appears in the browser is:

Warning: mail(): Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() in C:\xampp\htdocs\PCzone\contact us.php on line 268

you have to setup mail server on your local machine or use smtp lib to send email OR just install Sendmail on your local machine then change config with your SMTP detail

smtp_server=smtp.gmail.com
smtp_port=465
auth_username=user@gmail.com
auth_password=your_password

If your trying to work on local you have do some settings.Code seems to be perfect please check one on server. I hope it will work also check on spam and trash too.

I have change settings in

php.ini

Now code runs successfully .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM