简体   繁体   English

PEAR 邮件在 php:apache Docker 容器中

[英]PEAR Mail in php:apache Docker container

I have two servers.我有两台服务器。 A Postfix mail server with Dovecot that, otherwise, works fine.带有 Dovecot 的 Postfix 邮件服务器,否则,工作正常。 I can send mail through it using Gmail client (So, yes there is a valid certificate installed there).我可以使用 Gmail 客户端通过它发送邮件(所以,是的,那里安装了有效的证书)。 The other server is my app server, which has the php:7.4-apache image running.另一台服务器是我的应用服务器,它正在运行 php:7.4-apache 映像。 I've installed the PEAR Mail library into that container/image, and I'm trying to send mail from the app through the mail server, but the PEAR Mail client keeps hanging up after it sends STARTTLS.我已将 PEAR Mail 库安装到该容器/映像中,并且我正在尝试通过邮件服务器从应用程序发送邮件,但 PEAR Mail 客户端在发送 STARTTLS 后一直挂断。 Questions:问题:

What am I doing wrong?我究竟做错了什么?

Maillog on the mail server says only this:邮件服务器上的 Maillog 只说明了这一点:

Jun  1 17:36:46 Mimosa postfix/submission/smtpd[19141]: connect from unknown[10.0.0.14]
Jun  1 17:36:46 Mimosa postfix/submission/smtpd[19141]: lost connection after STARTTLS from unknown[10.0.0.14]
Jun  1 17:36:46 Mimosa postfix/submission/smtpd[19141]: disconnect from unknown[10.0.0.14]

Debug output from the client says this:从客户端调试 output 说:

[ehlo] Recv: 250-PIPELINING DEBUG: Recv: 250-SIZE 10240000 DEBUG: Recv: 250-VRFY DEBUG: 
Recv: 250-ETRN DEBUG: Recv: 250-STARTTLS DEBUG: Recv: 250-ENHANCEDSTATUSCODES DEBUG: 
Recv: 250-8BITMIME DEBUG: Recv: 250 DSN DEBUG: Send: STARTTLS DEBUG: 
Recv: 220 2.0.0 Ready to start TLS DEBUG: Send: RSET DEBUG: Send: QUIT

This is the code being used on the client:这是客户端上使用的代码:

<html><body>
<?php

var_dump(extension_loaded('openssl'));
//echo phpinfo();

include('/usr/local/lib/php/Mail.php');
$recipients = 'myemail@gmail.com'; //CHANGE
$headers['From']= 'noreply@mydomain.com'; //CHANGE
$headers['To']= 'myemail@gmail.com'; //CHANGE
$headers['Subject'] = 'Test message';
$body = 'Test message'; // Define SMTP Parameters
$params['host'] = '10.0.0.6';
$params['port'] = '587';
$params['auth'] = 'PLAIN';
$params['username'] = 'noreply'; //CHANGE
$params['password'] = 'password'; //CHANGE
$params['debug'] = 'true'; 

$mail_object =& Mail::factory('smtp', $params);

foreach ($params as $p){
 echo "$p<br />";
}

// Send the message
$mail_object->send($recipients, $headers, $body);

?>
</body></html>

I've also tried the following code, which is essentially the same thing:我也试过下面的代码,基本上是一样的:

<?php

error_reporting(E_ALL ^ E_NOTICE ^ E_DEPRECATED ^ E_STRICT);

require_once "/usr/local/lib/php/Mail.php";

$host = "10.0.0.6";
$username = "noreply";
$password = "password";
$port = "587";
$to = "myemail@gmail.com";
$email_from = "noreply@mydomain.com";
$email_subject = "Testing Pear" ;
$email_body = "Sent using Pear install Mail" ;
$email_address = "noreply@domain.com";

$headers = array ('From' => $email_from, 'To' => $to, 'Subject' => $email_subject, 'Reply-To' => $email_address);
$smtp = Mail::factory('smtp', array ('host' => $host, 'port' => $port, 'auth' => true, 'username' => $username, 'password' => $password));
$mail = $smtp->send($to, $headers, $email_body);


if (PEAR::isError($mail)) {
echo("<p>" . $mail->getMessage() . "</p>");
var_dump($mail);
} else {
echo("<p>Message successfully sent!</p>");
}
?>

And received the following error:并收到以下错误:

" authentication failure [SMTP: STARTTLS failed (code: 220, response: 2.0.0 Ready to start TLS)]"

I've tried both ports 25 and 587 with the same result, though only 587 should work.我已经尝试了端口 25 和 587,结果相同,但只有 587 应该可以工作。 I've tried the auth parameter as true, false, and plain.我已经尝试将 auth 参数设置为 true、false 和 plain。 Commenting out the auth parameter is rejected by the mail server which requires STARTTLS.需要 STARTTLS 的邮件服务器拒绝注释掉 auth 参数。

Changing this line in Mail.php在 Mail.php 中更改此行

function auth($uid, $pwd, $method = '', $tls = true, $authz = '')

to false, of course, does not work because the server requires a STARTTLS.当然,设置为 false 是行不通的,因为服务器需要 STARTTLS。 Disabling TLS on both ends, might make it functional, but that doesn't solve the problem with TLS.在两端禁用 TLS 可能会使其正常工作,但这并不能解决 TLS 的问题。

Please, don't tell me to just use PHPmailer.请不要告诉我只使用 PHPmailer。

Thank you.谢谢你。

Since the very problem here is TLS not working, any suggestion that you should edit Mail.php由于这里的问题是 TLS 不起作用,因此您应该编辑 Mail.php 的任何建议

WRONG    function auth($uid, $pwd, $method = '', $tls = ??, $authz = '') (Don't do this)

to turn off TLS is nonsensical.关闭 TLS 是荒谬的。

The issue here is that the certificate on the mail server is for a particular name, not a LAN IP address.这里的问题是邮件服务器上的证书是针对特定名称的,而不是 LAN IP 地址。 I've tried to add a second certificate for the LAN IP address with a SAN, but that did not work.我尝试使用 SAN 为 LAN IP 地址添加第二个证书,但这不起作用。 If you are using a reverse proxy, like I am (HAproxy), then it also fails to use the FQDN, instead of the LAN IP address, if your container is resolving DNS with an external server (8.8.8.8), though the problem there might be NAT/PAT, instead.如果您使用的是反向代理,如我(HAproxy),那么它也无法使用 FQDN,而不是 LAN IP 地址,如果您的容器正在使用外部服务器(8.8.8.8)解析 DNS,虽然问题相反,可能有 NAT/PAT。

The solution I found is to setup a forwarding DNS server on the LAN, with a forward lookup zone for the same domain & hosts as the certificate.我找到的解决方案是在 LAN 上设置一个转发 DNS 服务器,具有与证书相同的域和主机的正向查找区域。 One way or another, the value for $host needs to match the name on the mail server's certificate, and you need some kind of DNS to resolve to an address that will get you to that server.一种或另一种方式,$host 的值需要与邮件服务器证书上的名称匹配,并且您需要某种 DNS 来解析将您带到该服务器的地址。

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

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