简体   繁体   中英

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. 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. AND, it did not work !! But, when I modified a string a bit, like "user.server.com.us", it works !! 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. Give SwiftMailer or another PHP Mailing library for better support.

When using mail() function you are required to specify 'From' header. Please refer to this documentation page, look for 'additional_headers' parameter's Notes section.

So your mail() call will look like this:

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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