我有一个这样的脚本:
<html>
<body>
<?php
$addresses = ['<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="99fff6f6d9f4e0fdf6f4f8f0f7b7faf6f4" rel="noreferrer noopener nofollow">[email protected]</a>'];
foreach ($addresses as $address) {
sendMail($address);?><br /><?php
}
?>
<?php
function sendMail($address) {
mail($address, "object", "message");
print $address;
}
?>
</body>
</html>
我安装并配置了邮件服务器haraka。我认为我的配置没问题:当我使用命令 swaks -tls -f <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e195849295a18c98858e8c80888fcf828e8c" rel="noreferrer noopener nofollow">[email protected]</a> -t <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ccaaa3a38ca1b5a8a3a1ada5a2e2afa3a1" rel="noreferrer noopener nofollow">[email protected]</a> -s localhost -p 587 -au testuser -ap testpassword
时,我正确收到了邮件。
但是当我通过 PHP 中的邮件功能发送邮件时,我没有收到任何内容。
在我的php.ini
,我配置:
;[mail function]
SMTP = localhost
smtp_port = 587
username = testuser
password = testpassword
sendmail_from = <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0b7f6e787f4b66726f64666a626525686466" rel="noreferrer noopener nofollow">[email protected]</a>
脚本执行后,当我检查haraka的日志时,我没有看到任何东西。但在文件 /var/log/maillog
中,我可以看到sendmail的日志被添加了。
你能告诉我如何配置 PHP 来正确使用我的本地邮件服务器 Haraka 吗?
最佳答案
它不适用于 php 的邮件功能,请尝试使用它来通过 SMTP 发送消息 PHPMailer或swiftMailer , check this too
//PHPMailer
<?php
use PHPMailer\PHPMailer\PHPMailer;
require 'vendor/autoload.php';
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPDebug = 2;
$mail->Host = 'localhost';
$mail->Port = 587;
$mail->SMTPAuth = true;
$mail->Username = '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="384c5d4b4c785c5755595156165b5755" rel="noreferrer noopener nofollow">[email protected]</a>';
$mail->Password = 'EMAIL_ACCOUNT_PASSWORD';
$mail->setFrom('<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c2b6a7b1b682a6adafa3abaceca1adaf" rel="noreferrer noopener nofollow">[email protected]</a>', 'Your Name');
$mail->addReplyTo('<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="8efcebfee2f7a3ece1f6ceeae1e3efe7e0a0ede1e3" rel="noreferrer noopener nofollow">[email protected]</a>', 'Your Name');
$mail->addAddress('<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f19489909c819d94b1969c90989ddf929e9c" rel="noreferrer noopener nofollow">[email protected]</a>', 'Receiver Name');
$mail->Subject = 'PHPMailer SMTP message';
$mail->msgHTML(file_get_contents('message.html'), __DIR__);
$mail->AltBody = 'This is a plain text message body';
$mail->addAttachment('test.txt');
if (!$mail->send()) {
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message sent!';
}
?>
阅读文档以获取更多信息
关于php - 使用 Haraka 邮件服务器配置 PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55046252/