简体   繁体   中英

PHP5 mail() not sending

Having trouble using the PHP mail() function. It simply won't send.

My is as follows:

//email them new password
$recipient = $actual_email; //recipient
$subject = "Reset Password"; //subject = reason for contacting
// To send HTML mail, the Content-type header must be set
$headers  = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1" . "\r\n";
$headers .= "From: Website <do-not-reply-reset@domain.co.nz>\r\n"; //headerfields
$mail_body = "
  <html>
   <head>
     <title>Reset Password</title>
   </head>
    <body>
        <h2>Reset Password</h2>
        <p>Hello $actual_email<br />
        You recently asked to reset your password.</p>
        <p>Your reset password is below:<br />
        <br />
        <b>$string</b></p>
        <br />
        <p>It is recommended you copy and paste this password into the login page, and change your password under settings, then delete this email.</p>
        <p>If you did not request this change of password, it is recommended you login and change your password immediately, and get in contact with us.</p>
        <p>Thank you,<br />
        The Team
        <br />
        NOTE:<br />
        Please do not reply to this message, which was sent from an unmonitored e-mail address. Mail sent to this address cannot be answered.</p>
     </body>
    </html>";       
//send mail
if(mail($recipient, $subject, $mail_body, $headers)){ //mail command
   header('Location: index.php?success=yes');
   exit;
} else {
   $error_message = "<div class='error'>
      <img src='http://resources.domain.co.nz/backgrounds/icon_error.png' class='messageimg' />
    <h4>Error - Recovery Error</h4>
    <p>There was an error sending a recovery password. If the problem persists, please contact us directly.</p>
   </div>"; 
}

I have ensured sendmail is installed in the default place for UNIX, and it references it correctly in the PHP.ini.

Ive used this same code before, and not had any issues, so I assume it is something on the server side?

Any help appreciated.

Disclosure: I'm one of the developers behind AlphaMail

As WebnetMobile is saying, I also recommend that you stop using PHP's built in mail() function. If you're looking for a good SMTP-client for PHP, I would rather recommend that you use SwiftMailer or PHPMailer , they get the job done a lot better! (cleaner code and easier to handle errors).

Though remember, this is just from the client side, and it does not guarantee that your e-mails are delivered once "sent". Therefore 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. Ie 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>

Turns out SELinux must be off for mail to work.

Once its off, and machine restarted mail() worked without any problems.

The code looks fine to me, it should be something with the server. If you believe the server is properly setup than it may be something caused by selinux. Please check from the shell:

getsebool -a | grep mail

If you get an answer like

allow_postfix_local_write_mail_spool --> off

Than you spot the problem.

Either disable it or rather use the following command to allow mail to be sent from httpd:

setsebool -P httpd_can_sendmail on

If this does not resolve your issue, please post the error message from logs.

检查是否在Linux服务器上正确配置了发送邮件,然后根据需要更改PHP.INI中的设置。

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