美文网首页
邮件验证

邮件验证

作者: Maiiiiiiiiiiiid | 来源:发表于2019-04-28 12:51 被阅读0次

去官方的GitHub下载包

"phpmailer/phpmailer": "~6.0"
composer require phpmailer/phpmailer
<?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;
use PHPMailer\PHPMailer\Exception;

// Load Composer's autoloader
require 'vendor/autoload.php';      //框架支持自动加载就不用写

// Instantiation and passing `true` enables exceptions
$mail = new PHPMailer(true); //发送失败抛出异常

try {
    //Server settings
    $mail->SMTPDebug = 2;                                       // Enable verbose debug output
    $mail->isSMTP();                                            // Set mailer to use SMTP
    $mail->Host       = 'smtp1.example.com;smtp2.example.com';  // Specify main and backup SMTP servers
    $mail->SMTPAuth   = true;                                   // Enable SMTP authentication
    $mail->Username   = 'user@example.com';                     // SMTP username
    $mail->Password   = 'secret';                               // SMTP password
    $mail->SMTPSecure = 'tls';                                  // Enable TLS encryption, `ssl` also accepted
    $mail->Port       = 587;                                    // TCP port to connect to

    //Recipients
    $mail->setFrom('from@example.com', 'Mailer');
    $mail->addAddress('joe@example.net', 'Joe User');     // Add a recipient
    $mail->addAddress('ellen@example.com');               // Name is optional
    $mail->addReplyTo('info@example.com', 'Information');
    $mail->addCC('cc@example.com');
    $mail->addBCC('bcc@example.com');

    // Attachments
    $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 = 'Here is the subject';
    $mail->Body    = 'This is the HTML message body <b>in bold!</b>';
    $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

    $mail->send();
    echo 'Message has been sent';
} catch (Exception $e) {
    echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}

另外一个方法:

https://blog.csdn.net/qq_39864544/article/details/86655656

public function send(){
    $mail = new PHPMailer();
    $toemail = '1374624878@qq.com';//收件人
    $mail->isSMTP();// 使用SMTP服务
    $mail->CharSet = "utf8";// 编码格式为utf8,不设置编码的话,中文会出现乱码
    $mail->Host = "smtp.163.com";// 发送方的SMTP服务器地址
    $mail->SMTPAuth = true;// 是否使用身份验证
    $mail->Username = "maiiiiiid@163.com";/// 发送方的163邮箱用户名,就是你申请163的SMTP服务使用的163邮箱
    $mail->Password = "maiiiiiid142857";// 发送方的邮箱密码,注意用163邮箱这里填写的是“客户端授权密码”而不是邮箱的登录密码!
    $mail->SMTPSecure = "ssl";// 使用ssl协议方式
    $mail->Port = 465;// 163邮箱的ssl协议方式端口号是465/994

    $mail->setFrom("maiiiiiid@163.com","Maid");// 设置发件人信息,如邮件格式说明中的发件人,这里会显示为Mailer(xxxx@163.com),Mailer是当做名字显示
    $mail->addAddress($toemail,'Wang');// 设置收件人信息,如邮件格式说明中的收件人,这里会显示为Liang(yyyy@163.com)
    $mail->addReplyTo("maiiiiiid@163.com","Reply");// 设置回复人信息,指的是收件人收到邮件后,如果要回复,回复邮件将发送到的邮箱地址
    //$mail->addCC("xxx@163.com");// 设置邮件抄送人,可以只写地址,上述的设置也可以只写地址(这个人也能收到邮件)
    //$mail->addBCC("xxx@163.com");// 设置秘密抄送人(这个人也能收到邮件)
    //$mail->addAttachment("bug0.jpg");// 添加附件

    $mail->Subject = "注册验证";// 邮件标题

    $num = rand(100000,999999);

    $mail->Body = "邮件内容是 :您的验证码是:".$num;// 邮件正文
    //$mail->AltBody = "This is the plain text纯文本";// 这个是设置纯文本方式显示的正文内容,如果不支持Html方式,就会用到这个,基本无用

    if(!$mail->send()){// 发送邮件
        // echo "Message could not be sent.";
        // echo "Mailer Error: ".$mail->ErrorInfo;// 输出错误信息
        echo 2;
    }else{
        echo 1;  //成功
    }

}

邮件内容必须是带有中文吗?

在common.php进行封装:

use PHPMailer\PHPMailer;
use PHPMailer\Exception;
function send_mail($to,$title = "注册验证",$content = "邮件内容是 :您的验证码是:"){
    $mail = new PHPMailer();
    $mail->isSMTP();// 使用SMTP服务
    $mail->CharSet = "utf8";// 编码格式为utf8,不设置编码的话,中文会出现乱码
    $mail->Host = "smtp.163.com";// 发送方的SMTP服务器地址
    $mail->SMTPAuth = true;// 是否使用身份验证
    $mail->Username = "maiiiiiid@163.com";/// 发送方的163邮箱用户名,就是你申请163的SMTP服务使用的163邮箱
    $mail->Password = "maiiiiiid142857";// 发送方的邮箱密码,注意用163邮箱这里填写的是“客户端授权密码”而不是邮箱的登录密码!
    $mail->SMTPSecure = "ssl";// 使用ssl协议方式
    $mail->Port = 465;// 163邮箱的ssl协议方式端口号是465/994

    $mail->setFrom("maiiiiiid@163.com","Maid");// 设置发件人信息,如邮件格式说明中的发件人,这里会显示为Mailer(xxxx@163.com),Mailer是当做名字显示
    $mail->addAddress($to,'Wang');// 设置收件人信息,如邮件格式说明中的收件人,这里会显示为Liang(yyyy@163.com)
    $mail->addReplyTo("maiiiiiid@163.com","Reply");// 设置回复人信息,指的是收件人收到邮件后,如果要回复,回复邮件将发送到的邮箱地址
    //$mail->addCC("xxx@163.com");// 设置邮件抄送人,可以只写地址,上述的设置也可以只写地址(这个人也能收到邮件)
    //$mail->addBCC("xxx@163.com");// 设置秘密抄送人(这个人也能收到邮件)
    //$mail->addAttachment("bug0.jpg");// 添加附件

    $mail->Subject = $title;// 邮件标题

    $num = rand(100000,999999);

    $mail->Body = $content . $num;// 邮件正文
    //$mail->AltBody = "This is the plain text纯文本";// 这个是设置纯文本方式显示的正文内容,如果不支持Html方式,就会用到这个,基本无用

    if(!$mail->send()){// 发送邮件
        // echo "Message could not be sent.";
        // echo "Mailer Error: ".$mail->ErrorInfo;// 输出错误信息
        echo 2;
    }else{
        echo 1;  //成功
    }
}

相关文章

  • 邮件验证

    去官方的GitHub下载包 另外一个方法: https://blog.csdn.net/qq_39864544/a...

  • 邮件发送注册验证码

    如何实现网站注册验证码邮件自动发送功能?如何接入邮件API接口?免费使用 网站通过接入邮件验证码接口(邮件api接...

  • 忘记密码发送邮件验证

    登陆用户忘记密码① 验证用户② 验证通过发送邮件到指定邮箱,同时将uuid设置到缓存中③ 点击邮件中的url地址(...

  • php 邮件验证 图片验证 手机验证

    邮件验证逻辑 图片与手机验证的逻辑 通常情况下的流程手机号输入和图片验证输入在同一个界面。验证图片&&手机号,然后...

  • vapor 邮件发送确认码

    接上节发送邮件,这次我们结合数据库实现发送邮件验证码我们假设的是用户注册验证码: 首先邮箱唯一,其次验证码有有效期...

  • dede开启邮件验证

    发送邮件的邮箱设置:http://www.dede58.com/a/dedesp/1637.html 页面内代码修...

  • 密码找回业务模块

    输入个人邮箱 向sever提交发送邮件请求 server收到app请求并发送邮件 邮件内容:随机生成四位验证码、用...

  • 发送测试电子邮件消息: 无法发送此邮件

    发送测试电子邮件消息: 无法发送此邮件。请在帐户属性中验证电子邮件地址。 响应服务器: 553 authenti...

  • Gmail收不到验证邮件

    刚在过内某网站注册了一下,验证邮箱留的Gmail,结果刷新半天也收不到信。后来发现,验证邮件是自动放到垃圾邮件中的...

  • NPM验证邮件,手机接受验证时出现service unavail

    NPM验证邮件,手机接受验证时出现service unavailable 博客说明 文章所涉及的资料来自互联网整理...

网友评论

      本文标题:邮件验证

      本文链接:https://www.haomeiwen.com/subject/kujdnqtx.html