我试图从本地主机发送邮件。 但是我无法从本地主机发送邮件 所以有人能告诉我如何重新配置我的xampp从本地主机发送邮件


当前回答

根据我的个人经验,我发现与Vikas Dwivedi非常相似的答案会很好地工作。

步骤1 (php.ini文件)

在php.ini文件xampp\php\php.ini。更改设置如下:

 extension=php_openssl.dll
 [mail function]
 sendmail_path =":\xampp7\sendmail\sendmail.exe -t"
 mail.add_x_header=On

通过put关闭邮件函数下的其他变量;在他们面前。例如;smtp_port = 25

步骤2 (sendmail.ini文件)

在sendmail.ini位于xampp\sendmail\semdmail.ini更改为 后:

 smtp_server=smtp.gmail.com
 smtp_port=465
 smtp_ssl=auto
 auth_username=address@gmail.com
 auth_password=YourPassword

步骤3(代码)

创建一个php文件,并使用以下命令:

 <?php
    mail($to, "subject", "body", "From: ".$from);
 ?>

请注意

为了重新加载php.ini,您需要重新启动apache。 你需要在https://myaccount.google.com/u/1/security激活谷歌不太安全的应用程序访问 使用管理员权限运行Xampp可能会有所帮助。

其他回答

你必须在你的服务器上配置SMTP。你可以通过谷歌免费使用G Suite SMTP:

<?php

$mail = new PHPMailer(true);

// Send mail using Gmail
if($send_using_gmail){
    $mail->IsSMTP(); // telling the class to use SMTP
    $mail->SMTPAuth = true; // enable SMTP authentication
    $mail->SMTPSecure = "ssl"; // sets the prefix to the servier
    $mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
    $mail->Port = 465; // set the SMTP port for the GMAIL server
    $mail->Username = "your-gmail-account@gmail.com"; // GMAIL username
    $mail->Password = "your-gmail-password"; // GMAIL password
}

// Typical mail data
$mail->AddAddress($email, $name);
$mail->SetFrom($email_from, $name_from);
$mail->Subject = "My Subject";
$mail->Body = "Mail contents";

try{
    $mail->Send();
    echo "Success!";
} catch(Exception $e){
    // Something went bad
    echo "Fail :(";
}

?>

点击这里阅读更多关于PHPMailer的信息。

您可以测试发送邮件在您的PC没有互联网

你应该使用剪纸这个简单的应用程序来测试发送邮件。你不需要配置任何东西。

只需运行它,并尝试测试发送邮件:

test_sendmail.php

<?php
$to = "somebody@example.com";
$subject = "My subject";
$txt = "Hello world!";
$headers = "From: webmaster@example.com" . "\r\n" .
"CC: somebodyelse@example.com";

mail($to,$subject,$txt,$headers);
?>

你会看到这个:

我希望你今天过得愉快。 你可以在Youtube上找到我的更多教程

欢呼!

除了所有的答案,请注意sendmail.ini文件中:

auth_password = this-is-Not-your-Gmail-password

由于新的谷歌安全问题,您应该按照以下步骤为此目的设置应用程序密码:

在安全选项卡中转到https://accounts.google.com/ 使用任何可用选项打开两步验证 回到安全选项卡(在同一部分下,两步验证最初被发现),并创建App-password(在选择app下拉菜单中,您可以选择“其他”)

最后,请注意,如果您只是使用一个应用程序,而不是自己编写php代码,则可能需要将Sendmail设置为首选选项。

作为管理员运行XAMPP还应该解决任何文件访问问题。

我花了一个多小时才搞定。对于每个人都有同样的问题,所有的建议张贴不工作:你必须重新启动Apache在你的XAMPP inrerface!只是重新启动XAMPP行不通!!

在本地主机或本地服务器上发送电子邮件非常简单

注意:我使用的测试邮件服务器软件在Windows 7 64bit安装Xampp

只需下载测试邮件服务器工具,并根据其网站上测试邮件服务器工具的说明进行安装

现在您只需要更改php.ini文件下的两行

找到[mail function]并删除前面的分号;smtp = localhost sendmail_path = "C:\xampp\mailtodisk\mailtodisk.exe"

您不需要更改任何其他内容,但如果您仍然没有收到电子邮件,则检查SMTP端口,端口号必须相同。

以上方法是针对Xampp软件提供的默认设置。