Reputation: 75
I have spent the last four hours reading examples, watching YouTube videos etc and I cannot for the life of me get an email to send. I have tried using a G-mail account with the settings changed to allow for less secure apps and also a privately hosted email address from my business.
I loaded PHPMailer with composer
When I use the following code which is literally the example from PHPMailer's github page, I get the errors listed below.
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
require_once 'vendor/autoload.php';
$mail = new PHPMailer(true);
try {
//Server settings
$mail->SMTPDebug = SMTP::DEBUG_SERVER; //Enable verbose debug output
$mail->isSMTP(); //Send using SMTP
$mail->Host = 'smtp.gmail.com'; //Set the SMTP server to send through
$mail->SMTPAuth = true; //Enable SMTP authentication
$mail->Username = 'my gmail email here'; //SMTP username
$mail->Password = 'my gmail password here'; //SMTP password
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS; //Enable implicit TLS encryption
$mail->Port = 465; //TCP port to connect to; use 587 if you have set `SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS`
//Recipients
$mail->setFrom('my gmail email address', 'Mailer');
$mail->addAddress('different email address', 'Joe User'); //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}";
}
This is the error message that I get: 2021-08-09 20:04:25 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
I am 100% sure that the email addresses and passwords are correct and listed in the proper locations.
Does anyone know what is going on or what I can do to fix?
I am so desperate I am about to just switch programming languages because this is the most insanely complicated thing I have ever came across!
Thanks so much to anyone that has a solution or tips!
PS I am currently using localhost... I will answer any questions that you need answered Thanks!!!
UPDATE >>> When I change to this line
$mail->SMTPDebug = SMTP::DEBUG_CONNECTION;
I get this output:
2021-08-09 21:02:10 Connection: opening to ssl://smtp.gmail.com:465, timeout=300, options=array() 2021-08-09 21:02:10 Connection failed. Error #2: stream_socket_client(): SSL operation failed with code 1. OpenSSL Error messages:error:1416F086:SSL routines:tls_process_server_certificate:certificate verify failed [C:\xampp\htdocs\sheri\vendor\phpmailer\phpmailer\src\SMTP.php line 388] 2021-08-09 21:02:10 Connection failed. Error #2: stream_socket_client(): Failed to enable crypto [C:\xampp\htdocs\sheri\vendor\phpmailer\phpmailer\src\SMTP.php line 388] 2021-08-09 21:02:10 Connection failed. Error #2: stream_socket_client(): unable to connect to ssl://smtp.gmail.com:465 (Unknown error) [C:\xampp\htdocs\sheri\vendor\phpmailer\phpmailer\src\SMTP.php line 388] 2021-08-09 21:02:10 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
Upvotes: 0
Views: 1295
Reputation: 75
When I changed SMTPSecure = 'tls'
and Port = 587
this still did not work in local host but did work when I uploaded it on my server to test it.
Upvotes: 1
Reputation: 48357
SMTP ERROR: Failed to connect to server: (0) SMTP connect() failed
That implies a firewall issue - but its actually rather misleading.
SSL operation failed with code 1. OpenSSL Error messages:error:1416F086:SSL routines:tls_process_server_certificate:certificate verify failed
Your PHP installation could not validate the certificate presented by Google. They are usually quite good with certificates - its an issue with the installation of openssl on your machine. You need to configure openssl properly, specifically the CAcerts database.
update You installed MS-Windows on a namecheap VM and are using it as a webserver?????? OMG.
Upvotes: 0