Send E-mail

Use composer install phpmailer

composer require phpmailer/phpmailer

method:

// Send E-mail
    public function getEmail(){

        $data = $_POST;

        require dirname(dirname(dirname(__DIR__))).'/vendor/autoload.php';

        // random factor
        $arr = [1,2,3,4,5,6,7,8,9,'a','b','c','d','e','f','g','h','i','j','k','m','n','p','r','s','t','u','v','w','x','y','z','A','B','C','D','E','F','G','H','J','K','M','N','P','Q','R','S','T','U','V','W','X','Y','Z'];
        // four codes obtained by cyclically

        $str = '0';

        for($i = 0;$i < 6;$i++){
            $key = array_rand($arr);
            $str .= $arr[$key];
        }

        STR $ = substr ($ STR,. 1);   // custom random codes

        $mail = new PHPMailer(true);                              // Passing `true` enables exceptions

        try {
            //Server settings
//            $mail->SMTPDebug = 2;                                 // Enable verbose debug output
            $mail->isSMTP();                                      // Set mailer to use SMTP
            $mail->Host = 'smtp.163.com';  // Specify main and backup SMTP servers
            $mail->SMTPAuth = true;                               // Enable SMTP authentication
            // smtp account log in here to fill qq number to string format
            $mail->Username = '[email protected]';                 // SMTP username
            // smtp login password generated authorization code (qq mailbox to live through the authorization code)
            $mail->Password = '****';                           // SMTP password
            // set using login authentication ssl encryption
            $mail->SMTPSecure = 'ssl';                            // Enable TLS encryption, `ssl` also accepted
            // Set the port number to connect remote server ssl smtp server, the previous default is 25, but now seems to have not used the new optional 465 or 587
            $mail->Port = 465;                                    // TCP port to connect to

            //Recipients
            $mail->setFrom('[email protected]', 'Mailer');
            // set the recipient email address This method has two parameters The first parameter is the recipient email address
            // The second parameter is the nickname for a different e-mail address will be automatically set up to process variations of little significance here is the second parameter
            $mail->addAddress('163.com', 'Joe User');     // Add a recipient
            //$mail->addAddress('[email protected]');               // Name is optional
            //reply
            $mail->addReplyTo('[email protected]', 'Information');
            //$mail->addCC('[email protected]');
            //$mail->addBCC('[email protected]');

            // Attachments Accessories
            //$mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
            //$mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name

            //Content
            $mail->isHTML(true);                                  // Set email format to HTML
            $ Mail-> Subject = 'title';
            $ mail-> Body = 'content' </ B > ';
            $ Mail-> AltBody = 'content';

            $mail->send();

            return json_encode(true);die();
            echo 'Message has been sent';
        } catch (Exception $e) {

            echo 'Message could not be sent.';
            echo 'Mailer Error: ' . $mail->ErrorInfo;
        }
    }

 

Guess you like

Origin www.cnblogs.com/dream-meng/p/12640173.html