PHPMailer — решение проблем

  • Проблема с проверкой сертификатов.
    Соединение устанавливается (ответ сервера: 250), но при попытке проверки сертификатов происходит ошибка
Could not access file:
2022-05-19 22:11:43 Connection: opening to mail.company.com:587, timeout=300, options=array()
2022-05-19 22:11:43 Connection: opened
2022-05-19 22:11:44 SMTP INBOUND: "220 mail.company.com ESMTP Postfix (Debian/GNU)"
2022-05-19 22:11:44 SERVER -> CLIENT: 220 mail.company.com ESMTP Postfix (Debian/GNU)
2022-05-19 22:11:44 CLIENT -> SERVER: EHLO mysite.space
2022-05-19 22:11:44 SMTP INBOUND: "250-mail.company.com"
2022-05-19 22:11:44 SMTP INBOUND: "250-PIPELINING"
2022-05-19 22:11:44 SMTP INBOUND: "250-SIZE 52428800"
2022-05-19 22:11:44 SMTP INBOUND: "250-ETRN"
2022-05-19 22:11:44 SMTP INBOUND: "250-STARTTLS"
2022-05-19 22:11:44 SMTP INBOUND: "250-ENHANCEDSTATUSCODES"
2022-05-19 22:11:44 SMTP INBOUND: "250-8BITMIME"
2022-05-19 22:11:44 SMTP INBOUND: "250 DSN"
2022-05-19 22:11:44 SERVER -> CLIENT: 250-mail.company.com250-PIPELINING250-SIZE 52428800250-ETRN250-STARTTLS250-ENHANCEDSTATUSCODES250-8BITMIME250 DSN
2022-05-19 22:11:44 CLIENT -> SERVER: STARTTLS
2022-05-19 22:11:44 SMTP INBOUND: "220 2.0.0 Ready to start TLS"
2022-05-19 22:11:44 SERVER -> CLIENT: 220 2.0.0 Ready to start TLS
2022-05-19 22:11:44 Connection failed. Error #2: stream_socket_enable_crypto(): SSL operation failed with code 1. OpenSSL Error messages:error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed [/volume1/web/mysite.space/PHPMailer/SMTP.php line 468]
SMTP Error: Could not connect to SMTP host. Connection failed. stream_socket_enable_crypto(): SSL operation failed with code 1. OpenSSL Error messages:error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed
2022-05-19 22:11:44 CLIENT -> SERVER: QUIT
2022-05-19 22:11:44
2022-05-19 22:11:44
2022-05-19 22:11:44
2022-05-19 22:11:44
2022-05-19 22:11:44
2022-05-19 22:11:44 Connection: closed
SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

Решение 1:
Добавить следующие строки в ваш файл php

$mail->SMTPOptions = array(
	'ssl' => array(
	'verify_peer' => false,
	'verify_peer_name' => false,
	'allow_self_signed' => true
	)
	);

Часть кода для большей наглядности

$mail = new PHPMailer;
	$mail->IsSMTP();								
	$mail->Host = 'mail.company.com';		
	$mail->Port = '587';								
	$mail->SMTPAuth = true;							
	$mail->Username = 'mail@company.com';					
	$mail->Password = '6565dsgsdgsdggd654';

	$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS; 	

	$mail->SMTPSecure = 'tls';							
        $mail->setFrom('admin@company.com'); 
        $mail->addAddress('sales@company.com');    

	$mail->SMTPOptions = array(
	'ssl' => array(
	'verify_peer' => false,
	'verify_peer_name' => false,
	'allow_self_signed' => true
	)
	);

	//$mail->SMTPDebug = 4;
	$mail->CharSet = 'UTF-8';
        $mail->Encoding = '8bit';

Решение 2:
Смените хостинг или используйте чистый Linux, на Linux Server 20 все работает с дефолтными настройками

  • Нет соединения с сервером
    Некоторые особо интересные хостинги запрещают указывать хост в виде: «mail.company.com«
    Пример ошибки
2022-05-19 22:29:22 Connection: opening to mail.company.com:587, timeout=300, options=array ( 'ssl' => array ( 'verify_peer' => false, 'verify_peer_name' => false, 'allow_self_signed' => true, ),)
2022-05-19 22:29:22 Connection failed. Error #2: stream_socket_client(): unable to connect to mail.company.com:587 (Connection refused) [//volume1/web/mysite.space/PHPMailer/SMTP.php line 389]
2022-05-19 22:29:22 SMTP ERROR: Failed to connect to server: Connection refused (111)
SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

Решение:
Укажите значение Host как localhost

$mail = new PHPMailer;
	$mail->IsSMTP();								
	$mail->Host = 'localhost';		
	$mail->Port = '587';								
	$mail->SMTPAuth = true;							
	$mail->Username = 'mail@company.com';					
	$mail->Password = '6565dsgsdgsdggd654';
Наверх