I started off with the following:
composer require phpmailer/phpmailer
Followed by placing require once
require_once "../vendor/autoload.php";
Then
$mail = new PHPMailer;
That's where PHP has a fatal error:
Fatal error: Class 'PHPMailer' not found
The following is my code: use PHPMailer\PHPMailer\PHPMailer; use PHPMailer\PHPMailer\Exception;
# require php mailer
require_once "../vendor/autoload.php";
//PHPMailer Object
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPDebug = 3;
$mail->SMTPAuth = true;
// $mail->SMTPSecure = "ssl";
$mail->SMTPSecure = "tls";
$mail->Host = "smtp-relay.gmail.com";
$mail->Port = "587";
$mail->Username = "some gmail address";
$mail->Password = "some password";
$mail->SetFrom("[email protected]");
$mail->Subject = $subject;
$mail->Body = $htmlMessage;
$mail->AddAddress($recpEmails);
$mail->Send();
$htmlMessage
is a variable containing both plain text and html with mime_boundary but it never gets there as it cannot find PHPMailer.
This is what I have tried to do to fix the issue:
I have created another directory called PHPMailer in the same directory as vendor, this new directory containing the contents of phpmailer/phpmailer/src, but it still cannot find the PHPMailer class.
Do I have to require the PHPMailer.php file as well? I thought that was what the autoload was supposed to do?
Thanks in advance
UPDATE
After @Phil in the comments pointed out that I was missing the use namespace, I have updated the code, which has lead to a new error:
Fatal error: Uncaught exception 'PHPMailer\PHPMailer\Exception' with message 'SMTP connect() failed
I read that $mail->SMTPDebug = 3;
gives more details, which revealed the following:
2019-02-11 01:51:57 Connection: opening to ssl://smtp-relay.gmail.com:587, timeout=300, options=array()
2019-02-11 01:51:58 Connection failed. Error #2: stream_socket_client(): SSL operation failed with code 1. OpenSSL Error messages:error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol [v2018/assets/api/vendor/phpmailer/phpmailer/src/SMTP.php line 327]
2019-02-11 01:51:58 Connection failed. Error #2: stream_socket_client(): Failed to enable crypto [v2018/assets/api/vendor/phpmailer/phpmailer/src/SMTP.php line 327]
2019-02-11 01:51:58 Connection failed. Error #2: stream_socket_client(): unable to connect to ssl://smtp-relay.gmail.com:587 (Unknown error) [v2018/assets/api/vendor/phpmailer/phpmailer/src/SMTP.php line 327]
2019-02-11 01:51:58 SMTP ERROR: Failed to connect to server: (0)
SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
I then read to on another post to change the following settings, which allowed me to see that it was connecting, but it was not accepting my credentials:
$mail->SMTPSecure = "tls";
2019-02-11 02:00:26 SERVER -> CLIENT: 535-5.7.8 Username and Password not accepted. Learn more at535 5.7.8 https://support.google.com/mail/?p=BadCredentials f2sm562482otb.6 - gsmtp
2019-02-11 02:00:26 SMTP ERROR: Password command failed: 535-5.7.8 Username and Password not accepted. Learn more at535 5.7.8 https://support.google.com/mail/?p=BadCredentials f2sm562482otb.6 - gsmtp
I do not know what to do at this point, as I know these are the credentials for my gmail account. Do I have to have a gmail business account or something like that?
One more update
I was using incorrect password. Once I entered the correct password, I began receiving the following message:
2019-02-11 14:33:44 SERVER -> CLIENT: 550-5.7.0 Mail relay denied [172.117.2.44]. Invalid credentials for relay for550-5.7.0 one of the domains in: (as obtained from HELO and MAIL FROM).550-5.7.0 Email is being sent from a domain or IP address which isn't registered550-5.7.0 in your G Suite account. Please login to your G Suite account and550-5.7.0 verify that your sending device IP address has been registered within550-5.7.0 the G Suite SMTP Relay Settings. For more information, please visit550 5.7.0 https://support.google.com/a/answer/6140680#maildenied h192sm1484247qke.14 - gsmtp
Turns out that I have to have a G-Suite account to use smtp-relay.gmail.com. I only have a regular gmail account. When I changed to smtp.gmail.com, then I am back to the bad credentials errors. I will just wait for my G-Suite account to be verified and I will update with the results.
use
statement for the fully-qualified classnamePHPMailer\PHPMailer\PHPMailer
– Phil