I'm running a script that sends mail, but I can't get it to work.
This is my code. Any observation?:
<?php
// Import PHPMailer classes into the global namespace
// These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
//Load composer's autoloader
require 'vendor/autoload.php';
$mail = new PHPMailer(true); // Passing `true` enables exceptions
try {
//Server settings
$mail->SMTPDebug = 2; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.serviciodecorreo.es'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'admin@*****.**'; // SMTP username
$mail->Password = '********'; // SMTP password
$mail->SMTPSecure = 'ssl'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 465; // TCP port to connect to
//Recipients
$mail->setFrom('admin@*****.**');
$mail->addAddress('******@**.com'); // Add a recipient
//Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}
?>
I have changed the debug to the value 3 and this is what I get. Does anyone know what happens?
2018-02-23 09:05:56 Connection: opening to
ssl://smtp.serviciodecorreo.es:465, timeout=300, options=array()
2018-02-23 09:05:56 Connection failed. Error #2: stream_socket_client(): SSL
operation failed with code 1. OpenSSL Error messages:error:14090086:SSL
routines:ssl3_get_server_certificate:certificate verify failed
[C:\inetpub\SIADE\PHPMAIL\vendor\phpmailer\phpmailer\src\SMTP.php line 325]
2018-02-23 09:05:56 Connection failed. Error #2: stream_socket_client():
Failed to enable crypto
[C:\inetpub\SIADE\PHPMAIL\vendor\phpmailer\phpmailer\src\SMTP.php line 325]
2018-02-23 09:05:56 Connection failed. Error #2: stream_socket_client():
unable to connect to ssl://smtp.serviciodecorreo.es:465 (Unknown error)
[C:\inetpub\SIADE\PHPMAIL\vendor\phpmailer\phpmailer\src\SMTP.php line 325]
2018-02-23 09:05:56 SMTP ERROR: Failed to connect to server: (0)
SMTP connect() failed.
https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
Message could not be sent. Mailer Error: SMTP connect() failed.
https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
Does it have something to do with the IIS configuration?
PHP 5.6 introduces SSL certificate verification. It is the same as an SSL certificate for a web server: it must match the domain, it must be signed by a trusted CA, and it must have a SHA2 hash and 2048-bit key. If you don't want to reconfigure your server, it's possible to force your script to revert to the behavior before checking certificates, ie tell it not to check the certificate. In this way, you will stop getting error for incorrect certificate