php 无法向我自己域中的地址发送电子邮件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1107730/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
can't send email to addresses at my own domain
提问by sol
I have a simple php script on my domain that sends me an email:
我的域中有一个简单的 php 脚本,它向我发送电子邮件:
...
$toMail = "[email protected]"; //this works - I get the email at my gmail
$toMail = "[email protected]"; //this doesn't - I get nothing
mail($toMail, $subject, $message, $header);
What setting to I change to fix this?
我更改什么设置来解决这个问题?
采纳答案by schubySteve
I've had this problem myself, when I was redesigning a site recently.
当我最近重新设计一个网站时,我自己也遇到了这个问题。
There was an issue with the way our system was set up, so that the system thought that because the email was coming from the same domain it was a spam email and as such blocked it.
我们系统的设置方式存在问题,因此系统认为由于电子邮件来自同一域,因此它是垃圾邮件,因此将其阻止。
Check with your system administrator that you are allowed to be sending the emails etc.
请与您的系统管理员确认您是否可以发送电子邮件等。
Either that, or you'll have to modify the headers to have look like it's being sent from an external address. Hope you get it sorted.
要么如此,要么您必须修改标头,使其看起来像是从外部地址发送的。希望你能把它整理好。
回答by VicM
For the people using Google Apps for email, but having your host in other provider this are more detailed instructions for the people that is not very familiar with cPanel.
对于使用 Google Apps 发送电子邮件但在其他提供商处拥有您的主机的人,对于不太熟悉 cPanel 的人来说,这是更详细的说明。
I could fix the problem of sending email from my domain using a PHP form, when sending the email to an account inside my domain.
当将电子邮件发送到我的域内的帐户时,我可以解决使用 PHP 表单从我的域发送电子邮件的问题。
i.e. mydomain.com Contact form sending email to [email protected]. The above was not working even if my domain has the correct MX records for the domain using Google Apps.
即 mydomain.com 联系表格发送电子邮件至 [email protected]。即使我的域具有使用 Google Apps 的域的正确 MX 记录,上述内容也不起作用。
As Mike noted (and others) above the problem was solved: Adding the MX records into the cPanel.
正如 Mike 上面提到的(和其他人),问题已经解决:将 MX 记录添加到 cPanel 中。
- Enter into the cPanel
- Go the the cPanel Mail section
- Search for MX Entry Maintenance, sometimes there is no text above the icon.
- Select the related domain
- Change EmailRouting to Remote Mail Exchanger.
- Add all the google MX records as they are in your domain configuration using the appropriate priority values. You can check the records here and priorities https://support.google.com/a/answer/174125
- Double check that Remote Mail Exchanger.is selected.
- 进入cPanel
- 转到 cPanel 邮件部分
- 搜索MX Entry Maintenance,有时图标上方没有文字。
- 选择相关域
- 将电子邮件路由更改为远程邮件交换器。
- 使用适当的优先级值添加域配置中的所有 google MX 记录。您可以在此处查看记录和优先级 https://support.google.com/a/answer/174125
- 仔细检查远程邮件交换器。被选中。
With this setting I was able to send email using mail PHP function to an email account inside the same domain as my website.
通过此设置,我能够使用邮件 PHP 功能将电子邮件发送到与我的网站位于同一域内的电子邮件帐户。
Google App instructions talking about MX records https://support.google.com/a/answer/54717?hl=en
关于 MX 记录的 Google App 说明 https://support.google.com/a/answer/54717?hl=en
回答by Matthieu Grimber
If you use postfix, do this :
如果您使用postfix,请执行以下操作:
- connect to your server via ssh.
- edit your main.cffile :
- 通过 ssh 连接到您的服务器。
- 编辑您的main.cf文件:
nano /etc/postfix/main.cf
nano /etc/postfix/main.cf
- comment the following line with #:
- 用#注释以下行:
# mydestination = ...
# mydestination = ...
- add at the end of the main.cfdocument :
- 在main.cf文件的末尾添加:
mydestination =
mydestination =
- reload your postfix configuration by running :
- 通过运行重新加载您的后缀配置:
/etc/init.d/postfix reload
/etc/init.d/postfix reload
回答by Michael Hellein
The top answer at https://serverfault.com/questions/65365/disable-local-delivery-in-sendmailseems correct to me. The gist of it is that you want the following in your sendmail.mc:
https://serverfault.com/questions/65365/disable-local-delivery-in-sendmail 上的最佳答案对我来说似乎是正确的。其要点是您希望在 sendmail.mc 中包含以下内容:
define(`MAIL_HUB`, 'example.com.')dnl
define(`LOCAL_RELAY`, 'example.com.')dnl
Where example.com is the domain in question.
其中 example.com 是有问题的域。
回答by David Pratt
You need to set an additional parameter on your mail function. On your working example you would need to prepend your email address with '-f' e.g.
您需要在邮件功能上设置一个附加参数。在您的工作示例中,您需要在您的电子邮件地址前加上“-f”,例如
mail($toMail, $subject, $message, $header, "[email protected]");
mail($toMail, $subject, $message, $header, "[email protected]");
回答by Mike
I had the same problem and was able to solve it in the following way. I do not store mail locally on the server but use MX records on the registrar to direct mail into Google Apps. It turned out the MX records needed to be updated in Cpanel as well, as the server was not taking the MX records from the registrar but instead discarding since there was no local MX record or mailbox. I updated the MX entries on Cpanel to match the registrar's MX entries, and the problem was fixed instantly
我遇到了同样的问题,并且能够通过以下方式解决它。我不在服务器本地存储邮件,而是使用注册商上的 MX 记录将邮件定向到 Google Apps。结果证明 MX 记录也需要在 Cpanel 中更新,因为服务器没有从注册商处获取 MX 记录,而是丢弃了,因为没有本地 MX 记录或邮箱。我更新了 Cpanel 上的 MX 条目以匹配注册商的 MX 条目,问题立即得到解决
回答by estepix
I agree with Michael Hellein, the root problem could be your sendmail considering your domain example.com email accounts as local accounts. If so, here are few guiding links:
我同意 Michael Hellein 的观点,根本问题可能是您的 sendmail 将域 example.com 电子邮件帐户视为本地帐户。如果是这样,这里有几个指导链接:
- https://serverfault.com/questions/65365/disable-local-delivery-in-sendmail
- https://serverfault.com/questions/102647/sendmail-to-local-domain-ignoring-mx-records-part-2
- http://lists.freebsd.org/pipermail/freebsd-questions/2004-September/057382.html
- http://objectmix.com/sendmail/367920-sendmail-ignores-mailertable-some-semilocal-domains.html
- https://serverfault.com/questions/65365/disable-local-delivery-in-sendmail
- https://serverfault.com/questions/102647/sendmail-to-local-domain-ignoring-mx-records-part-2
- http://lists.freebsd.org/pipermail/freebsd-questions/2004-September/057382.html
- http://objectmix.com/sendmail/367920-sendmail-ignores-mailertable-some-semilocal-domains.html
But in my case, using FreeBSD 8.2, what really did the trick was:
但就我而言,使用 FreeBSD 8.2,真正的诀窍是:
# cd /etc/mail
# vim freebsd.mc
Add these two lines:
添加这两行:
define(`MAIL_HUB', `example.com.')dnl
define(`LOCAL_RELAY', `example.com.')dnl
Right before:
就在之前:
MAILER(local)
MAILER(smtp)
Then:
然后:
# make
This is output:
这是输出:
cp freebsd.mc host.example.com.mc
/usr/bin/m4 -D_CF_DIR_=/usr/share/sendmail/cf/ /usr/share/sendmail/cf/m4/cf.m4 host.example.com.mc > host.example.com.cf
cp freebsd.submit.mc host.example.com.submit.mc
/usr/bin/m4 -D_CF_DIR_=/usr/share/sendmail/cf/ /usr/share/sendmail/cf/m4/cf.m4 host.example.com.submit.mc > host.example.com.submit.cf
Then:
然后:
# cp sendmail.cf sendmail.cf.bak
# cp host.example.com.cf sendmail.cf
# /etc/rc.d/sendmail restart
Hope this saves some headaches to someone.
希望这可以为某人省去一些麻烦。
回答by Meep3D
Do you have your email hosted on a different server than the website? If that is the case the PHP script may be trying to send it internally in which case it'll just disappear, while the other target emails will get put on to the internet and routed properly.
您是否将电子邮件托管在与网站不同的服务器上?如果是这种情况,PHP 脚本可能会尝试在内部发送它,在这种情况下它会消失,而其他目标电子邮件将被放到互联网上并正确路由。
The solution I found was to disable the mail server on your web host, and then PHP will put the message on to the internet to be sent properly.
我找到的解决方案是禁用 Web 主机上的邮件服务器,然后 PHP 会将邮件放到 Internet 上以便正确发送。
回答by Nolwennig
With PostFix, Debian7, smtp Mailjet
使用 PostFix、Debian7、smtp Mailjet
If domain is my-domain.com, in /etc/postfix/main.cf, change
如果域是my-domain.com,在/etc/postfix/main.cf,改变
mydestination = my-domain.com, localhost, localhost.localdomain, localhost
to
到
mydestination = localhost, localhost.localdomain, localhost
回答by Michelle
As explained by others, some servers are configured to reject emails missing a valid email address on the sending server. Check that the $headers string includes a defined valid email address "From:[email protected]".
正如其他人所解释的那样,某些服务器被配置为拒绝发送服务器上缺少有效电子邮件地址的电子邮件。检查 $headers 字符串是否包含定义的有效电子邮件地址“From:[email protected]”。

