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

发布于 2024-12-06 15:02:56 字数 696 浏览 0 评论 0原文

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

sudo apt-get install sendmail

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

<?php
$to = "[email protected]";
$subject = "Test mail";
$message = "Hello! This is a simple email message.";
$from = "[email protected]";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
echo "Mail Sent.";
?> 

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

I am using Ubuntu.I installed sendmail in my local host using the following command

sudo apt-get install sendmail

Now I would like to check whether mail goes from my localhost using following php code.

<?php
$to = "[email protected]";
$subject = "Test mail";
$message = "Hello! This is a simple email message.";
$from = "[email protected]";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
echo "Mail Sent.";
?> 

When i execute the code, It takes very long time and finally echo the message as Mail Sent.
Is there any possibilities to fix this ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

羁〃客ぐ 2024-12-13 15:02:56

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

127.0.0.1 localhost.localdomain localhost myhostname

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

O HostsFile=/etc/hosts

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

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

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

每当使用邮件功能时,您都必须重新使用 sendmail -f 选项。

例如:

mail('[email protected]', 'the subject', 'the message', null, '[email protected]');

Edit the file /etc/hosts and make sure the first line is the following:

127.0.0.1 localhost.localdomain localhost myhostname

Edit the sendmail configuration file ( /etc/mail/sendmail.cf in Ubuntu) and Uncomment the line #O:

O HostsFile=/etc/hosts

Restart the computer, or run sudo service sendmail restart.

The computer should boot up much faster now and the mail() function should return almost immediately.

HOWEVER, the emails won't actually be sent unless you follow step 5.

You must new use the sendmail -f option whenever using the mail function.

For example:

mail('[email protected]', 'the subject', 'the message', null, '[email protected]');
如果没有 2024-12-13 15:02:56

我知道这个问题已经得到解答,但我发布此问题是希望它可以帮助其他人寻找此问题的不同解决方案。

对我来说,我只需要将服务器的完全限定域名 (FQDN) 放入 /etc/mailname 中。例如:server.example.com

重新启动 Sendmail 以应用更改。

$ sudo 服务 sendmail 重新启动

I know this question has already been answered, but I'm posting this in the hope it may help someone else looking for a different solution to this issue.

For me, I just needed to put my server's Fully Qualified Domain Name (FQDN) into /etc/mailname. For example: server.example.com.

Restart Sendmail to apply the changes.

$ sudo service sendmail restart

蓝眼睛不忧郁 2024-12-13 15:02:56

这个答案帮助我https://serverfault.com/a/221894/186680

安装了Postfix和sendmail。删除了 sendmail Yum 删除了 sendmail

This answer helped me https://serverfault.com/a/221894/186680

Postfix and sendmail where installed. Removed sendmail Yum remove sendmail

两相知 2024-12-13 15:02:56

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

The delay is usually indicating a DNS time-out. Is your machine configured with proper DNS entries? I would try doing a test using mail on the command line as this would isolate the issue.

金橙橙 2024-12-13 15:02:56

除非您非常了解 Sendmail,否则最好安装其他 MTA。另一个答案中已经建议了 ssmtpd ;一个常见的选择是 Postfix。

Unless you know Sendmail well, you are probably bettter off installing some other MTA. ssmtpd was already suggested in another answer; a common choice is Posfix.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文