• TP5使用phpmailer实现邮件发送


    1、从github下载PHPMailer,在vendor目录中新建文件夹phpmailer,将压缩包中的class.phpmailer.php和class.smtp.php复制到phpmailer中,更改class.phpmailer.php文件名为phpmailer.php

    2、在两个文件顶部加上命名空间namespace phpmailer;

    3将phpmailer.php中class phpmailerException extends Exception 改为class phpmailerException extends Exception 

    4、在app/common.php下加上

    use phpmailerphpmailer;
    /**
    * 发送邮箱
    * @param type $data 邮箱队列数据 包含邮箱地址 内容
    */
    function sendEmail($data = []) {
      Vendor('phpmailer.phpmailer');
      $mail = new PHPMailer(); //实例化
      $mail->IsSMTP(); // 启用SMTP
      $mail->Host = 'smtp.163.com'; //SMTP服务器 以163邮箱为例子 
      $mail->Port = 465;  //邮件发送端口
      $mail->SMTPAuth = true;  //启用SMTP认证
      $mail->SMTPSecure = "ssl";   // 设置安全验证方式为ssl
      $mail->CharSet = "UTF-8"; //字符集
      $mail->Encoding = "base64"; //编码方式
      $mail->Username = 'xxxx@163.com';  //你的邮箱 
      $mail->Password = 'xxxxxx';  //你的smtp密码 
      $mail->Subject = '资源鸟系统提示'; //邮件标题  
      $mail->From = 'xxxx@163.com';  //发件人地址(也就是你的邮箱)
      $mail->FromName = 'xxx';  //发件人姓名
      if($data && is_array($data)){
        foreach ($data as $k=>$v){
          $mail->AddAddress($v['user_email'], "亲"); //添加收件人(地址,昵称)
          $mail->IsHTML(true); //支持html格式内容
          $mail->Body = $v['content']; //邮件主体内容
          //发送成功就删除
          if ($mail->Send()) {
            echo "发送成功";
          }else{
              echo "Mailer Error: ".$mail->ErrorInfo;// 输出错误信息  
          }
        }
      }           
    }

    5、在任何地方都可以调用:sendEmail([['user_email'=>'XXXX@126.com','content'=>'邮件发送测试']]);

    6、如果想发送附件,则加上:

    $mail->addAttachment('文件路径','文件名'); 
    //$mail->addAttachment(iconv('utf-8', 'gb2312','文件路径'),'文件名');   //文件路径中文件名包含中文  
  • 相关阅读:
    邮件发送的存储过程写法
    FormsAuthentication.GetRedirectUrl 方法
    视图性能优化——索引视图
    删除多字段的重复行保留最大最小行
    c#知识点
    sql存储过程exec执行字符串select 的区别
    UVALive5198 UVA512 Spreadsheet Tracking
    UVA129 HDU1627 Krypton Factor
    UVA129 HDU1627 Krypton Factor
    UVA10603 Fill
  • 原文地址:https://www.cnblogs.com/lamp01/p/7793840.html
Copyright © 2020-2023  润新知