关于thinkphp5中配置phpmailer邮件功能(php为5.5n)

首先,下载phpmailer包
CSDN下载网站(我懒得找,所以花点积分解决问题):https://download.csdn.net/download/qq_39545346/10312906
我配置时参考的网站:https://www.cnblogs.com/liu-heng/p/7252100.html
取出包內phpmailer.php和class.smtp.php文件(phpmailer.php有可能是class.phpmailer.php,去掉前面的class)

在thinkphp的extend文件下新建一个phpmailer文件夹,把两个文件放入phpmailer文件夹(我的理解,extend下的类可以自动加载)

两个文件中
添加namespace phpmailer;的命名空间
而调用时,先在开头添加一句use phpmailer\phpmailer;

我相信,了解过命名空间的小伙伴都知道是什么意思

除此之外还要把phpmailer里面的最后一个方法继承的Exception改成 \Exception(是的,你没看错就是多加一条杠)

//调用函数
//发送邮件
    public function email(){
        //$email=input("post.email");//获取收件人邮箱
        $email="xxxxxx@qq.com";
        //return $email;
        $sendmail = 'zzzzzzzz@qq.com'; //发件人邮箱
        $sendmailpswd = "zzzzzzzzpswd"; //客户端授权密码,而不是邮箱的登录密码,就是手机发送短信之后弹出来的一长串的密码
        $send_name = 'zzzzzzzz';// 设置发件人信息,如邮件格式说明中的发件人,
        $toemail = $email;//定义收件人的邮箱
        $to_name = 'hl';//设置收件人信息,如邮件格式说明中的收件人
        $mail = new PHPMailer();//因为前面添加了use phpmailer\phpmailer;就可以直接new新建类
        $mail->isSMTP();// 使用SMTP服务
        $mail->CharSet = "utf8";// 编码格式为utf8,不设置编码的话,中文会出现乱码
        $mail->Host = "smtp.qq.com";// 发送方的SMTP服务器地址(这是qq的SMTP服务器)
        $mail->SMTPAuth = true;// 是否使用身份验证
        $mail->Username = $sendmail; 发送方的
        $mail->Password = $sendmailpswd;//客户端授权密码,而不是邮箱的登录密码!
        $mail->SMTPSecure = "ssl";// 使用ssl协议方式
        $mail->Port = 465;//  qq端口465或587)
        $mail->setFrom($sendmail, $send_name);// 设置发件人信息,如邮件格式说明中的发件人,
        $mail->addAddress($toemail, $to_name);// 设置收件人信息,如邮件格式说明中的收件人,
        $mail->addReplyTo($sendmail, $send_name);// 设置回复人信息,指的是收件人收到邮件后,如果要回复,回复邮件将发送到的邮箱地址
        $mail->Subject = "这里是邮件标题";// 邮件标题

        $code=rand(100000,999999);//PHP的随机生成函数
        session("code",$code);//session的记录,类似cookie,但是是放置在服务器端的,通常用来作为登录状态记录
        //return $code."----".session("code");
        $mail->Body = "邮件内容是 <b>您的验证码是:$code</b>,如果非本人操作无需理会!";// 邮件正文
        //$mail->AltBody = "This is the plain text纯文本";// 这个是设置纯文本方式显示的正文内容,如果不支持Html方式,就会用到这个,基本无用
        if (!$mail->send()) { // 发送邮件
            echo "Message could not be sent.";
            echo "Mailer Error: " . $mail->ErrorInfo;// 输出错误信息
        } else {
            return “发送成功”;
        }
        /*//测试fsockopen这个功能是否正常
         * $fp = fsockopen("smtp.163.com",25,$errno,$errstr,60);
            if(! $fp)
                echo '$errstr ($errno) <br> \n ';
            else
                echo 'ok <br> \n ';*/
    }

如果出现SMTP Error: Could not connect to SMTP host. Message could not be sent.Mailer Error: SMTP Error: Coul错误

修改php配置环境的php.ini中,
extension=php_sockets.dll
extension=php_openssl.dll这两句


如果
;extension=php_sockets.dll
;extension=php_openssl.dll
去掉;


如果
extension=php_openssl.dll不存在
添加一句extension=php_openssl.dll

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
class PHPMailer { ///////////////////////////////////////////////// // PROPERTIES, PUBLIC ///////////////////////////////////////////////// /** * Email priority (1 = High, 3 = Normal, 5 = low). * @var int */ public $Priority = 3; /** * Sets the CharSet of the message. * @var string */ public $CharSet = 'iso-8859-1'; /** * Sets the Content-type of the message. * @var string */ public $ContentType = 'text/plain'; /** * Sets the Encoding of the message. Options for this are * "8bit", "7bit", "binary", "base64", and "quoted-printable". * @var string */ public $Encoding = '8bit'; /** * Holds the most recent mailer error message. * @var string */ public $ErrorInfo = ''; /** * Sets the From email address for the message. * @var string */ public $From = 'root@localhost'; /** * Sets the From name of the message. * @var string */ public $FromName = 'Root User'; /** * Sets the Sender email (Return-Path) of the message. If not empty, * will be sent via -f to sendmail or as 'MAIL FROM' in smtp mode. * @var string */ public $Sender = ''; /** * Sets the Subject of the message. * @var string */ public $Subject = ''; /** * Sets the Body of the message. This can be either an HTML or text body. * If HTML then run IsHTML(true). * @var string */ public $Body = ''; /** * Sets the text-only body of the message. This automatically sets the * email to multipart/alternative. This body can be read by mail * clients that do not have HTML email capability such as mutt. Clients * that can read HTML will view the normal Body. * @var string */ public $AltBody = ''; /** * Sets word wrapping on the body of the message to a given number of * characters. * @var int */ public $WordWrap = 0; /** * Method to send mail: ("mail", "sendmail", or "smtp"). * @var string */ public $Mailer = 'mail'; /** * Sets the path of the sendmail program. * @var string */ public $Sendmail = '/usr/sbin/sendmail'; /** * Path to PHPMailer plugins. Useful if the SMTP class * is in a different directory than the PHP include path. * @var string */ public $PluginDir = ''; /** * Sets the email address that a reading confirmation will be sent. * @var string */ 一个可以收发邮件的工具类

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值