gpt4 book ai didi

php - 在本地主机发送邮件需要很长时间

转载 作者:IT王子 更新时间:2023-10-29 00:12:41 26 4
gpt4 key购买 nike

我正在使用 Ubuntu。我使用以下命令在我的本地主机上安装了 sendmail

sudo apt-get install sendmail

现在我想使用以下 php 代码检查邮件是否来 self 的本地主机。

<?php
$to = "test@test.com";
$subject = "Test mail";
$message = "Hello! This is a simple email message.";
$from = "someonelse@example.com";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
echo "Mail Sent.";
?>

当我执行代码时,需要很长时间才能最终将消息回显为邮件已发送。有没有可能解决这个问题?

最佳答案

编辑文件 /etc/hosts 并确保第一行如下:

127.0.0.1 localhost.localdomain localhost myhostname

编辑sendmail配置文件(在Ubuntu中为/etc/mail/sendmail.cf)并取消注释line #O:

O HostsFile=/etc/hosts

重启电脑,或者运行sudo service sendmail restart

计算机现在应该启动得更快,mail() 函数应该几乎立即返回。

但是,除非您执行第 5 步,否则实际上不会发送电子邮件。

无论何时使用邮件功能,您都必须新使用 sendmail -f 选项。

例如:

mail('recipient@somewhere.com', 'the subject', 'the message', null, '-fsender@somewhere.com');

关于php - 在本地主机发送邮件需要很长时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7578952/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com