繁体   English   中英

PHP5 mail()未发送

[英]PHP5 mail() not sending

使用PHP mail()函数时遇到问题。 它根本不会发送。

我的如下:

//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>"; 
}

我确保sendmail已安装在UNIX的默认位置,并且它在PHP.ini中正确引用了它。

我以前使用过相同的代码,并且没有任何问题,所以我认为这是服务器端的问题?

任何帮助表示赞赏。

披露:我是AlphaMail背后的开发人员之一

正如WebnetMobile所说,我还建议您停止使用PHP的内置mail()函数。 如果您正在为PHP寻找一个好的SMTP客户端,我宁愿建议您使用SwiftMailerPHPMailer ,它们可以使工作做得更好! (更清晰的代码,更易于处理错误)。

请记住,这只是从客户端发出的,它不能保证您的电子邮件一旦“发送”就可以发送。 因此,我建议您使用事务电子邮件服务,例如:

为什么?

  • 您不必对电子邮件传递考虑太多。
  • 统计。 让我们跟踪“发送总数/点击次数/打开次数/跳动次数”。
  • 通常基于Web服务而不是SMTP。 即更容易处理。
  • 简洁的代码(至少在使用AlphaMail将数据与表示分离的情况下)。
  • 可扩展且面向未来。

如果您选择使用AlphaMail,则可以使用AlphaMail PHP-client

例:

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
);

AlphaMail的另一个优点是,您可以直接在AlphaMail仪表板中编辑模板,并且可以使用Comlang模板语言来格式化电子邮件。

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

事实证明SELinux必须关闭才能正常工作。

一旦关闭,机器重新启动后mail()就可以正常工作。

代码对我来说看起来不错,应该与服务器有关。 如果您认为服务器安装正确,则可能是selinux引起的。 请从外壳检查:

getsebool -a | grep mail

如果您得到类似的答案

allow_postfix_local_write_mail_spool --> off

比您发现问题。

禁用它,或者使用以下命令允许从httpd发送邮件:

setsebool -P httpd_can_sendmail on

如果这不能解决您的问题,请从日志中发布错误消息。

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

暂无
暂无

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

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