简体   繁体   English

无法在php mail()中使用变量$ _SERVER ['HTTP_HOST']

[英]Cannot use variable $_SERVER['HTTP_HOST'] in php mail()

I tested this: 我测试了这个:

<?php
 $to = "recipient@example.com";
 $subject = "Hi!";
 $server = $_SERVER['HTTP_HOST'];
 $body = "From: ". $server. "<br>";
 $body .= "Hi,\n\nHow are you?";
 if (mail($to, $subject, $body)) {
   echo("<p>Message successfully sent!</p>");
  } else {
   echo("<p>Message delivery failed...</p>");
  }
 ?>

This will never send emails to me although the feedback "successfully" is displayed. 尽管显示“成功”的反馈,但它永远不会向我发送电子邮件。 The code will work (email actually sent) however if I removed the inclusion of 该代码将起作用(实际发送电子邮件),但是如果我删除了

$server = $_SERVER['HTTP_HOST'];

in the email body. 在电子邮件正文中。

Very weird, it doesn't make sense ? 很奇怪,这没有道理吗?

This is just a PHP page. 这只是一个PHP页面。 I call this page from a browser !! 我通过浏览器调用此页面! Please try ... 请试试 ...

UPDATE!! 更新! okay, Instead of using $_SERVER['HTTP_HOST'], I use a string "user.server.com" directly. 好的,我没有使用$ _SERVER ['HTTP_HOST'],而是直接使用字符串“ user.server.com”。 AND, it did not work !! 而且,它没有用! But, when I modified a string a bit, like "user.server.com.us", it works !! 但是,当我稍微修改一个字符串(例如“ user.server.com.us”)时,它可以工作! So basically, the mail server filler its own reference to its domain, not sure why its doing this... 因此,基本上,邮件服务器会填充自己对域的引用,不确定为什么要这样做...

Some spam servers grade your message as 'spammy' for urls in the body. 某些垃圾邮件服务器会将您的邮件归为体内网址的“垃圾邮件”。 Unfortunately you'll be hard-pressed to find a more specific error message with mail() unless you have access to the sender and recipient's mail server logs. 不幸的是,除非您有权访问发件人和收件人的邮件服务器日志,否则您将很难用mail()查找更具体的错误消息。 Give SwiftMailer or another PHP Mailing library for better support. 提供SwiftMailer或其他PHP Mailing库以获得更好的支持。

When using mail() function you are required to specify 'From' header. 使用mail()函数时,需要指定“发件人”标头。 Please refer to this documentation page, look for 'additional_headers' parameter's Notes section. 请参考此文档页面,查找“ additional_headers”参数的“注释”部分。

So your mail() call will look like this: 因此,您的mail()调用将如下所示:

mail($to, $subject, $body, 'From: some.guy@example.com');

Of course you don't receive it because there these days all major webmails use anti spam filters and probably your message is detected as a spam. 当然,您不会收到它,因为这些天所有主要的Web邮件都使用反垃圾邮件过滤器,并且您的邮件可能被检测为垃圾邮件。 The solution is to specify exactly your email for $from variable, if you don't specify your real email your message is detected as spam. 解决方案是为$ from变量精确指定您的电子邮件,如果您未指定真实电子邮件,则将您的邮件检测为垃圾邮件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM