Click here to Skip to main content
16,016,623 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Team

I have a code that sends verification code to a user email, but when debugging as to find out why this is not sending. Its showing an invalid address from and to. But these address i am using them for my outlook and do work, checked under spam no such email. What could be the reason and how do i solve this problem?

What I have tried:

PHP
<pre><?php



use PHPMailer\PHPMailer\PHPMailer;

require_once(__DIR__ . '/sendEmails/vendor/phpmailer/phpmailer/src/Exception.php');
require_once(__DIR__ . '/sendEmails/vendor/phpmailer/phpmailer/src/PHPMailer.php');
require_once(__DIR__ . '/sendEmails/vendor/phpmailer/phpmailer/src/SMTP.php');

error_reporting(-1);
ini_set('display_errors', 'On');
set_error_handler("var_dump");


class VerificationCode
{
    public $smtpHost;
    public $sender;
    public $smtpPort;
    public $password;
    public $receiver;
    public $code;


    // some function to receive,send and port.

    public function _constructor($receiver) {
        $this->sender = "2023@outlook.com";
        $this->password = "***";
        $this->smtpHost = "smtp-mail.outlook.com";
        $this->smtpPort = 587;

    }
    // function to send email().
    public function sendMail(){

        $mail= new PHPMailer();
        $mail->isSMTP();
        $mail->SMTPAuth = true;
        $mail->SMTPDebug = 3;
        $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
        $mail->SMTPOptions = array(
            'ssl'=> array(
                'verify_peer'=>false,
                'verify_peer_name' => false,
                'allow_self_signed'=> true
            )
            );

            $mail->Host = $this->smtpHost;
            $mail->Port =$this->smtpPort;
            $mail->IsHTML(true);
            $mail->Username =$this->sender;
            $mail->Password= $this->password;
            $mail->Body=$this->getHTMLMessage();
            $mail->Subject = "Your verification code is {$this->code}";
            $mail->SetFrom($this->sender, "Verification Code");
            $mail->AddAddress($this->receiver);
            if($mail->send()) {
                echo "Mail Sent Successfully";
                exit;
            }
            echo "Failed to Send Mail";
    }

    // function to get html message from the email.
    public function getHTMLMessage() {
        $this->code=$this->getVerificationCode();
        $htmlMessage=<<<MSG
        <!DOCTYPE html>
        <html>
            <body>
                <h1>Your verification code is {$this->code}</h1>
                <p>Use this code to verify your account.</p>
            </body>
        </html>
        MSG;
        return $htmlMessage;
    }

    // get verificationCode.
    public function getVerificationCode() {
        return (int) substr(number_format(time() * rand(), 0,  '', ''), 0, 6);
    }

}

// instantiate VerificationCode and send email
$vc=new VerificationCode('2023@outlook.com'); 
$vc->sendMail();

?>
Posted
Updated 9-May-23 5:06am

We can't really help - you need to use the debugger to look at exactly what $this->receiver contains while your code is running, and we can't do that. Also try check the $mail Username and Password are still set correctly.
 
Share this answer
 
PHP
$mail->SetFrom($this->sender, "Verification Code");
$mail->AddAddress($this->receiver);

You have not initialised $this->receiver anywhere, but you are using the name 2023@outlook.com as sender. So you need to check both fields before sending.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900