无法使用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中正确引用它。
之前我使用过相同的代码,没有任何问题,所以我认为它是服务器端的东西?
任何帮助表示感谢。
答案 0 :(得分:0)
披露:我是AlphaMail背后的开发者之一
正如WebnetMobile所说,我还建议您停止使用PHP的内置mail()函数。如果您正在为PHP寻找一个好的SMTP客户端,我宁愿建议您使用SwiftMailer或PHPMailer,他们可以更好地完成工作! (更清晰的代码,更容易处理错误)。
虽然记住,这只是来自客户端,并不保证您的电子邮件一旦“发送”即可发送。因此,我建议您使用交易电子邮件服务,例如:
<强>为什么吗
如果您选择使用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 Dashboard中修改模板,并且可以使用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>
答案 1 :(得分:0)
原来SELinux必须关闭才能使邮件正常工作。
一旦关闭,机器重启邮件()工作没有任何问题。
答案 2 :(得分:0)
代码看起来很好,它应该是服务器的东西。如果您认为服务器设置正确,那么可能是由selinux引起的。请从外壳检查:
getsebool -a | grep mail
如果你得到像
这样的答案allow_postfix_local_write_mail_spool --> off
比你发现问题。
要么禁用它,要么使用以下命令允许从httpd:
发送邮件setsebool -P httpd_can_sendmail on
如果这不能解决您的问题,请在日志中发布错误消息。
答案 3 :(得分:-1)
检查Linux服务器上是否正确配置发送邮件,然后根据需要更改PHP.INI中的设置。