Thinkpphp phpmailer mailbox verification

Posted by DigitalExpl0it on Mon, 09 Dec 2019 00:43:22 +0100

thinkphp about phpmailer's mailbox verification

First,

Log in to your email, for example: qq email. Log in qq email to open the smtp service in account settings:

  

 

Then send back an authorization code, which will be saved first and used later.

 

Two.

Download phpmailer using composer

Open your tp framework path in cmd and enter composer require phpmailer/phpmailer directly

After that, there will be another phpmailer folder under your third-party class library. Open this folder;

  

Then copy the src resources;

Create a phpmailer folder under the extensions folder of the tp framework;

Paste the resource you just copied under this folder.

  

Modify the namespace of three files: namespace phpmailer

 

III. use phpmailer

Write in common.php under the tp framework:

  

<?php
    
    function sendMail($mail , $to ,$title , $content)
    {
      try{
 $mail->SMTPDebug = 0;  //SMTP Debug function 0=Close, 1=Errors and messages 2=news
 $mail->isSMTP(); Set use SMTP service;
 $mail->CharSet = 'utf-8'; //Mail encoding;
 $mail->Host = 'smtp.qq.com'; //smtp The server;
 $mail->SMTPAuth = true; //Enable smtp Verification function;
 $mail->Username = '******@qq.com'; //SMTP Server user name;
 $mail->Password = '**********'; //This is the authorization code you started to get;It can also be your email password;
$mail->SMTPSecure = 'ssl'; //Use security protocol;

    //Recipients / / recipient information settings
$mail->setForm('*******@qq.com' , 'in order to php'); //The first parameter is the recipient mailbox , The second parameter is the subject of the message;
$mail->addAddress($to); //Incoming sender's email address; 

//Content message content
$mail->isHTML(true); 
$mail->Subject = $title;
$mail->Body = $content;
return $mail->send()
}  catch (Exception $e){
 echo 'Message could not sent.Mailer Error:',$email->ErrorInfo;    
}

}                                        

 

 

use phpmailer/PHPMaileron in the index.php file of application contoller

 

<?php

use app\index\controller;
use think\Controller;
use think\View;
use phpmailer\PHPMailer;

class Index extends Controller
{
  public $view;
  public function __construct()
{
  $this->view = new View;
}
public function index()
{
  $this->view->fetch('index/index');
}


public function sendemail() { $code = rand(10000 , 99999); $data = array_values($_POST); $user = implode('' , $data); $emailuser = str_replace('' , '.' , $user); $email = new PHPMailer(true); $res = sendMail($mail , $emailuser , 'php Funny' , 'Hello!Thank you for being[php It's fun, member] , <br />Have a good time , Have a good time!'); if($res){ return json(['status'=>1 , 'msg'=>'Email sent successfully']); } else { return json(['status'=>0 , 'msg'=>'Failed to send mailbox']); } } }

 

Create an index folder under the view file of the controller, and write an index.html file under the index file;

<html>
    <head>
        <meta charset="utf-8" />
        <title>index</title>
        
     </head>
    <body>
        <input type="text" placeholder="Please enter email" id="email">
        <input type="button" id="btn" value="Mailbox validation">
    </body>
    <script src="[Here's your quote jquery Route]"></script>
    <script type="text/javascript">
        $(function(){

            $("#btn").click(function{
    
            $.post(":url('index/index/sendemail')",
            {"email":$("#email").val()},
            function(data){
                if(data.status){
                   alert(data.msg); 
} else {
                    alert(data.msg);
}
})
});

})

    </script>
</html>

Topics: PHP JSON encoding SSL