@mail not sending mail in php

2020-02-10 10:50发布

This is the first time I am using the mail function. I tried to send a mail using this and the code tells me it's "successfully sent", but I didn't receive any mail. I'm confused reading lots of articles on this. Please help me.

<?php
  if(isset($_POST['author'])&& isset($_POST['subject'])&& isset($_POST['text'])) {
    //Email
    $email_to = "lucy@yahoo.com";
    $email_subject = "Query From HYPERMARKETS";
    $name = $_POST['author']; // required
    $sub = $_POST['subject']; // required
    $comments = $_POST['text']; // required

    echo "POST:".$name.$sub.$comments;
    $error_message = "";

    $string_exp = "^[a-z .'-]+$";
    if(!strcmp($string_exp,$name)) {
      $error_message .= 'The Name you entered does not appear to be valid.<br />';
    }

    if(strlen($comments) < 5) {
      $error_message .= 'The Comments you entered do not appear to be valid.<br />';
    }

    if(strlen($error_message) > 0) {
      echo ("<center><table border=1 cellspacing=5 cellpadding=5><tr><th>$error_message</th></tr></table></center>");
    }
    $email_message = "Form details below.\n\n";
    $email_message .= "Name: ".$name."\n";
    $email_message .= "Subject: ".$sub."\n";
    $email_message .= "Comments: ".$comments."\n";


    // create email headers
    $headers = "From: mary@yahoo.com \r\n Reply-To: mary@yahoo.com \r\n" ;
    if(@mail($email_to, $email_subject, $email_message, $headers))
      echo "<center><table border=1 cellspacing=5 cellpadding=5><tr><th>MESSAGE SENT SUCCESSFULLY</th></tr></table></center>";
    else
      echo "<center><table border=1 cellspacing=5 cellpadding=5><tr><th>MESSAGE NOT SENT</th></tr></table></center>";
  }
  else
    echo ("<center><table border=1 cellspacing=5 cellpadding=5><tr><th>FILL ALL THE FIELDS..!!</th></tr></table></center>");
?>

Hey I have configured my php.ini file as

[mail function]

   sendmail_from = postmaster@localhost
   SMTP = localhost
   smtp_port = 25
   sendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" -t"
   sendmail_path = "C:\xampp\mailtodisk\mailtodisk.exe"

It still doesnt work.

标签: php email
5条回答
Bombasti
2楼-- · 2020-02-10 11:03

First, I would make sure that port 25 is open. Determine the public IP address of the server by connecting to WhatsMyIP.org (from the same internet connection that the server uses) then try the following from the command line:

telnet xxx.xxx.xxx.xxx 25

(replace xxx.xxx.xxx.xxx with the IP from WhatsMyIP.org)

If you can connect, then this answer isn't for you. Sorry.

If not, check all of your local firewall settings to make sure that port 25 is open on your end. Port 25 may also be blocked by the ISP. It turns out that some (maybe most) ISPs block port 25 by default to prevent spam.

Misconfigured email servers are easily compromised and used in spam botnets. I had to call Comcast and jump through hoops to get them to unblock port 25 before I could get my home email server working. (Comcast forum post about port 25).

If that doesn't get you going, I'd take another look at the email server configuration.

查看更多
手持菜刀,她持情操
3楼-- · 2020-02-10 11:06

did you ever send mails on this server? if not:

you have to configure mail function section in php.ini.

First of all you have to have SMTP support for send&receive e-mails. Second, you have to configure SMTP and smtp_port values in php.ini file.

查看更多
家丑人穷心不美
4楼-- · 2020-02-10 11:13

Disclosure: I'm one of the developers behind AlphaMail

Since you have no error to go on, it's not that easy to say what the issue is here. I would therefore recommend that to manually try and connect to the SMTP-server from your server and send the email manually. This way you'll have the error in hand instead of second-guessing.

If you don't want to do this, and just want it to "work" I would recommend that you use a Transactional Email Service such as:

Why?

  • You don't have to think that much about email delivery.
  • Statistics. Let's you track Total Sent/Clicks/Opens/Bounces.
  • Often web service-based instead of SMTP. I.e. easier to handle.
  • Cleaner code (at least if you use AlphaMail that separates data from presentation).
  • Scalable and future proof.

If you choose to go with AlphaMail you could use the AlphaMail PHP-client.

Example:

include_once("comfirm.alphamail.client/emailservice.class.php");

$email_service = AlphaMailEmailService::create()
    ->setServiceUrl("http://api.amail.io/v1")
    ->setApiToken("YOUR-ACCOUNT-API-TOKEN-HERE");

$person = new stdClass();
$person->userId = "1234";
$person->firstName = "John";
$person->lastName = "Doe";
$person->dateOfBirth = 1975;

$response = $email_service->queue(EmailMessagePayload::create()
    ->setProjectId(12345) // Your AlphaMail project (determines template, options, etc)
    ->setSender(new EmailContact("Sender Company Name", "from@example.com"))
    ->setReceiver(new EmailContact("Joe Doe", "to@example.org"))
    ->setBodyObject($person) // Any serializable object
);

Another advantage with AlphaMail is that you can edit your templates directly in the AlphaMail Dashboard, and you can format your emails using the Comlang template language.

<html>
    <body>
        <b>Name:</b> <# payload.firstName " " payload.lastName #><br>
        <b>Date of Birth:</b> <# payload.dateOfBirth #><br>

        <# if (payload.userId != null) { #>
            <a href="/sign-up">Sign Up Free!</a>
        <# } else { #>
            <a href="/login?id=<# payload.userId #>">Sign In</a>
        <# } #>
    </body>
</html>
查看更多
放我归山
5楼-- · 2020-02-10 11:14

Some hosting requires the extra parameter for mail. It could possibly be that.

    $ffrom = '-fmary@yahoo.com';
    if(@mail($email_to, $email_subject, $email_message, $headers, $ffrom));

It's not very common, but I have had to deal with it, and it took me ages to diagnose, so thought I would share it just in case.

查看更多
Root(大扎)
6楼-- · 2020-02-10 11:26

mail() doesn't actually send mail. It merely submits the message you've generated to the host system's mail subsystem to handle the sending. A result of true indicates that the mail sending subsystem accepted the message as valid. It doesn't indicate that the message was sent.

You'll need to look at your mail spool to see if the messages are in there awaiting delivery, and at your system's mail log to see if it is generating errors when it tries to send your PHP-generated messages.

Even if the message has been successfully sent, it doesn't mean it will be received. It may be caught by a spam filter, or rejected by a server, or just plain sent to the wrong address. It may have been delivered and ended up being marked as junk mail by the receiver's e-mail client. Any number of things can prevent a message being sent, a true response from mail is no indication that it was sent successfully.

查看更多
登录 后发表回答