Gcobani Mkontwana

Gcobani Mkontwana

  • 565
  • 1.9k
  • 407.5k

Failed to send Mail: Debugging(Invalid mail address from and to)

May 9 2023 1:07 PM

Hi Team

I need some clarity on the way i am sending my email, i am using phpmailer. this is without composer. tried to use with composer i was still getting this same error. How has a knowldge of where could it the problem? Does this mean smtp settings are incorrect?

<?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 = "[email protected]";
        $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('[email protected]'); 
$vc->sendMail();

?>

 


Answers (1)