Code to php.ini:
[mail function]
; For Win32 only.
; http://php.net/smtp
;SMTP=localhost
; http://php.net/smtp-port
SMTP=smtp.gmail.com
smtp_port=587
sendmail_from = my-gmail-id@gmail.com--- I have changed this to be only my gmail email without the my-gmail-.... and with that... either way it made no difference
;sendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" -t"
sendmail_path = "C:\xampp\htdocs\mysite\includes\PHPMailer\src\PHPMailer.php" --- I have made the link look like the one above as well and either way it makes no difference.
; For Win32 only.
;http://php.net/sendmail-from -- I have uncommented this line as well and either way it made no difference.
sendmail_from = admin@mywebsite.com
PHP function to send the mail:
- Code: Select all
<?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; ---------------------- I have tried putting the use inside the function and I get errors if I do.
use PHPMailer\PHPMailer\SMTP; ---------------------- I have tried putting the use inside the function and I get errors if I do.
use PHPMailer\PHPMailer\Exception; ---------------------- I have tried putting the use inside the function and I get errors if I do.
function send_code($code, $email) {
// 'PHPMailer' here actually is the original folder 'PHPMailer-master'
// from unpacking the downloaded file PHPMailer-master.zip
require 'PHPMailer/src/Exception.php';
require 'PHPMailer/src/PHPMailer.php';
require 'PHPMailer/src/SMTP.php';
$mail = new PHPMailer(true);
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = 'mygmail@gmail.com';
$mail->Password = 'mypass';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->setFrom('admin@mywebsite.com', 'Admin');
$mail->addAddress($email);
$mail->isHTML(true);
$mail->Subject = 'Confirmation Code';
$mail->Body = "Thank you for registering, your confirmation code is: ".$code;
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
}
?>
As I said, it says the message has been sent, but for two days now I can't seem to get an email. What am I doing wrong? I have also stopped the service and restarted after every change. I know that's a common mistake, so I make sure to do that. It also sends users information to the database, so I can't figure out why it's not sending mail
Thank you in advance, and blessings to all.