开发者

PHPs mail function doesn't send email to some servers

开发者 https://www.devze.com 2023-01-02 15:55 出处:网络
I have the following code which works on some servers and does not work on others: $Name = \"myname\"; //senders name

I have the following code which works on some servers and does not work on others:

$Name = "myname"; //senders name
            $email_sender = "myemail.dia@gmail.com"; //senders e-mail adress
            $recipient = $email; //recipient
            $mail_body = "The text for the mail..."; //mail body
            $subject = "Subject for reviever"; //subject
            $header = "From: ". $Name . " <" . $email_sender . ">\r\n"; 
           开发者_如何学Python             $status = mail($recipient, $subject, $mail_body, $header); 
            print('ENVOI '. $status);

The $status variable is true but I don't see any email.


$status being true doesn't mean the mail was RECEIVED by your recipient. It just means the mail function successfully delivered the mail to the LOCAL delivery agent. After that it's out of PHP's hands.

The process looks something like this:

  1. PHP script calls mail()
  2. mail() delivers message to the local mail server (sendmail, postfix, exim, etc..)
  3. mail(), having successfully completed 'delivery' of the email, returns TRUE
  4. local mail server connect's to recipient's mail server, delivers mail
  5. recipient's mail server does whatever it has to to get the email into the recipient's inbox.

Since mail() is returning true, that means that at least your sending code is correct enough to not cause things to blow up at that stage. That leaves delivery problems between your and the recipients' mail servers:

a) Perhaps the recipient is using greylisting (in which case the mail SHOULD eventually show up). Maybe your server gives up before the greylist timeout period expires, so the retry attempt(s) is never made.

b) your mail server is blacklisted. Your server, and/or some other potential spam source is in the same netblock have been added to one or more anti-spam RBL lists the recipient subscribes to.

c) Perhaps the remote server is very prickly about header correctness and your server's a bit too relaxed about one or more headers.

At least these problems SHOULD be visible in your own mail server's maillog (generally /var/log/maillog on most Unix-ish systems). Try sending a test mail while watching the log to see how the message procedes through the system. Also check the server's outgoing mail queue (mailq command, usually). Maybe the missing messages are stuck in there.

And then there's the bigger problems:

d) the remote mail server is accepting the message, but silently tossing it because it's flagged as spam or as infected. This you can't detect from your own mail logs, as this is done purely on the recipient end. All you'll see is the "250 OK" success message.

For this you'll need the recipient's help in diagnosing the problem.


This may or may not be related but you have a pretty simple header, I would replace your header variable with something like what's below and see if that changes anything for you.

$headers = 'From: ' .$email_sender. "\r\n" .
    'Reply-To: ' .$email_sender. "\r\n" .
    'X-Mailer: PHP/' . phpversion();


Make sure you smtp setting are correct on the servers in question.

0

精彩评论

暂无评论...
验证码 换一张
取 消