在localhost中发送邮件需要很长时间

mym*_*and 17 php ubuntu sendmail

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

sudo apt-get install sendmail
Run Code Online (Sandbox Code Playgroud)

现在我想使用以下php代码检查邮件是否来自我的localhost.

<?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.";
?> 
Run Code Online (Sandbox Code Playgroud)

当我执行代码时,它需要很长时间,最后将邮件作为邮件发送回显.有没有可能解决这个问题?

小智 14

编辑文件/etc/hosts并确保第一行是以下内容:

127.0.0.1 localhost.localdomain localhost myhostname

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

O HostsFile =/etc/hosts

重新启动计算机,或运行sudo service sendmail restart.

计算机现在应该启动得更快,并且该mail()功能应该几乎立即返回.

但是,除非您按照步骤5执行,否则实际上不会发送电子邮件.

-f每当使用mail函数时,您必须新使用sendmail 选项.

例如:

mail('recipient@somewhere.com', 'the subject', 'the message', null, '-fsender@somewhere.com');
Run Code Online (Sandbox Code Playgroud)

  • 第5步?你的答案中没有"步骤". (20认同)
  • 第5步来自这个问题http://serverfault.com/questions/173762/php-mail-function-painfully-slow-on-local-development-machine/221894#221894 (2认同)

Ras*_*ika 0

延迟通常表示 DNS 超时。您的计算机是否配置了正确的 DNS 条目?我会尝试在命令行上使用邮件进行测试,因为这可以隔离问题。