微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

PHP mail() 函数在 localhost:4000 上不起作用

如何解决PHP mail() 函数在 localhost:4000 上不起作用

我为 PHP 使用 localhost:4000,但在发送电子邮件时出错: 警告:mail():无法在“localhost”端口 25 连接到邮件服务器,请验证 PHP.ini 中的“SMTP”和“smtp_port”设置或在 C:\Users\schpe\www\contact.PHP 中使用 ini_set()第 33 行

我没有PHP.ini文件(只是开发和生产)

PHP

<?PHP
$errors = [];
$errorMessage = '';

if (!empty($_POST)) {
    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message'];

    if (empty($name)) {
        $errors[] = 'Name is empty';
    }

    if (empty($email)) {
        $errors[] = 'Email is empty';
    } else if (!filter_var($email,FILTER_VALIDATE_EMAIL)) {
        $errors[] = 'Email is invalid';
    }

    if (empty($message)) {
        $errors[] = 'Message is empty';
    }


    if (empty($errors)) {
        $toEmail = 'xxxxx@gmail.com';
        $emailSubject = 'New email from your contant form';
        $headers = ['From' => $email,'Reply-To' => $email,'Content-type' => 'text/html; charset=UTF-8'];

        $bodyParagraphs = ["Name: {$name}","Email: {$email}","Message:",$message];
        $body = join(PHP_EOL,$bodyParagraphs);

        if (mail($toEmail,$emailSubject,$body,$headers)) {
            header('Location: index.html');
        } else {
            $errorMessage = 'Oops,something went wrong. Please try again later';
        }
    } else {
        $allErrors = join('<br/>',$errors);
        $errorMessage = "<p style='color: red;'>{$allErrors}</p>";
    }
}


?>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。