use PHPMailerPHPMailerPHPMailer;
use PHPMailerPHPMailerException;
use PHPMailerPHPMailerSMTP;
//Load Composer's autoloader
require 'vendor/autoload.php';
function send_password_reset($get_name, $get_email, $token)
{
$mail = new PHPMailer(true);
$mail->SMTPDebug = 3;
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = '';
$mail->Password = '';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->setFrom('[email protected]','E-TICKETING REMBANGAN');
$mail->addAddress($get_email);
$mail->isHTML(true);
$mail->Subject = 'Reset Password Notification';
$email_template = "
<html>
<head>
<style>
.button {
display: inline-block;
padding: 10px 20px;
text-align: center;
text-decoration: none;
background-color: #4CAF50;
color: white;
border-radius: 10px;
margin: auto;
display: block;
width: 200px;
text-decoration: none; /* Removes underline */
color: white; /* Makes text color white */
}
.button-container {
text-align: center;
}
</style>
</head>
<body>
<h2>Anda menerima email karena kami mendapatkan permintaan Pembaharuan password untuk akun anda pada laman <b>Wisata Rembangan</b></h2>
<h5>Jangan bagikan link ini kepada siapapun, abaikan email ini jika bukan ada yang meminta perubahan password!</h5>
<div class='button-container'>
<a href='https://wisata-rembangan.mifc.myhost.id/password-change.php?token=$token&email=$get_email' class='button'>Klik Disini</a>
</div>
</body>
</html>";
$mail->Body = $email_template;
$mail->send();
}
- 2023-11-30 07:42:23 Connection: opening to smtp.gmail.com:25, timeout=300, options=array()
- 2023-11-30 07:42:23 Connection: opened
- 2023-11-30 07:42:23 SERVER -> CLIENT: 220-odin.idserverhost.com ESMTP Exim 4.96.2 #2 Thu, 30 Nov 2023 14:42:23 +0700 220-We do not authorize the use of this system to transport unsolicited, 220 and/or bulk e-mail.
- 2023-11-30 07:42:23 CLIENT -> SERVER: EHLO wisata-rembangan.mifc.myhost.id
- 2023-11-30 07:42:23 SERVER -> CLIENT: 250-odin.idserverhost.com Hello wisata-rembangan.mifc.myhost.id [103.66.86.234]250-SIZE 52428800250-8BITMIME250-PIPELINING250-PIPECONNECT250-AUTH PLAIN LOGIN250-STARTTLS250 HELP
- 2023-11-30 07:42:23 CLIENT -> SERVER: STARTTLS
- 2023-11-30 07:42:23 SERVER -> CLIENT: 220 TLS go ahead
- 2023-11-30 07:42:23 Connection failed. Error #2: stream_socket_enable_crypto(): Peer certificate CN=`odin.idserverhost.com’ did not match expected CN=`smtp.gmail.com’ [/home/mifcmyho/wisata-rembangan.mifc.myhost.id/vendor/phpmailer/phpmailer/src/SMTP.php line 476]
- SMTP Error: Could not connect to SMTP host. Connection failed. stream_socket_enable_crypto(): Peer certificate CN=`odin.idserverhost.com’ did not match expected CN=`smtp.gmail.com’
- 2023-11-30 07:42:23 CLIENT -> SERVER: QUIT
- 2023-11-30 07:42:23
- 2023-11-30 07:42:23
- 2023-11-30 07:42:23 Connection: closed
- SMTP Error: Could not connect to SMTP host. Connection failed. stream_socket_enable_crypto(): Peer certificate CN=`odin.idserverhost.com’ did not match expected CN=`smtp.gmail.com’
from the provider say, nothing port is block. and my lecture said is from verison of my php mailer.
can someone tell me what a problem?
2
Answers
This error indicates that the server certificate presented by odin.idserverhost.com does not match the expected certificate for smtp.gmail.com. The SSL/TLS negotiation failed because the server you are connecting to (odin.idserverhost.com) is not the expected SMTP server (smtp.gmail.com).
Here are a few things you can check or consider:
Correct SMTP Server Settings:
Make sure that you are using the correct SMTP server settings for Gmail. The correct server for SMTP with SSL for Gmail is smtp.gmail.com on port 465.
Verify Hostname:
Double-check the hostname you are using for your SMTP connection. It should be smtp.gmail.com, not odin.idserverhost.com. The error indicates that the server you are connecting to has a certificate issued to odin.idserverhost.com, and this doesn’t match the expected smtp.gmail.com.
Use TLS Instead of SSL:
Instead of using SSL, you might want to try using TLS. For Gmail, you should connect to port 587 with TLS. This can be set in your mail configuration.
Check Server Certificate:
Ensure that the certificate for odin.idserverhost.com is valid and correctly configured on your server. If there’s any issue with the certificate, it might cause the SSL/TLS negotiation to fail.
Firewall and Network Issues:
Check if there are any firewall or network issues preventing your server from connecting to smtp.gmail.com on the required port.
Debugging Tools:
You can use tools like OpenSSL to debug SSL/TLS connections and inspect certificates. This can help you identify any issues with the SSL/TLS handshake.
Remember to avoid hardcoding specific server information in your code and use configuration files or environment variables for flexibility.
By addressing these points, you should be able to troubleshoot and resolve the SMTP connection issue. If the problem persists, you may want to contact your hosting provider or system administrator for assistance.
As the PHPMailer docs on this exact issue say, this will be because your hosting provider is redirecting your SMTP traffic to their own mail server, so what they are saying about not blocking is simply untrue. This is why when you ask to connect to
smtp.gmail.com
, you end up connected to something else, which is causing the certificate mismatch. This is exactly the kind of man-in-the-middle attack that TLS is designed to combat, and it’s working exactly as it should by refusing to proceed.It also has nothing to do with your PHPMailer or PHP version.
To solve this, read the docs, but it’s likely you’ll need to alter your sending configuration to send through their mail servers (which will mean you can’t send from a gmail address as that would be forgery that will be blocked by gmail’s DMARC policy), or ask them to unblock the port properly.
Either that or find a better hosting provider.