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 (将#修改为@)