• PHPMailer类 发送邮件


    /**
     * [sendMail 邮件发送类]
     * @param  [string] $address    [收件人的邮件地址]
     * @param  [string] $nickname   [收件人的昵称]
     * @param  [string] $subject    [邮件的标题]
     * @param  [string] $content    [邮件的内容]
     * @param  [string] $attachment    [邮件的附件]
     * @return [boolean]            [返回结果,要么true,要么false]
     */
    // 邮箱
    function sendMail( $address ,$nickname ,$subject ,$content,$attachment=''){
        //引入文件
        require './vendor/mail/class.smtp.php';
        require './vendor/mail/class.phpmailer.php';
    
        // 实例化 PHPMailer类
        $mail = new PHPMailer;
        // 告诉 PHPMailer类 使用 SMTP 发送邮件
        $mail->isSMTP();
        // 启用SMTP调试
        // 0 = 关闭 (项目上线时)
        // 1 = 客户端 消息
        // 2 = 客户端 和 服务端消息[这里的客户端指的是我们,服务端指的是网易、QQ ]
        // $mail->SMTPDebug = 2;
        // 设置邮件的编码格式
        $mail->CharSet = 'utf-8';
        // 设置调试输出的内容是HTML格式
        // $mail->Debugoutput = 'html';
        // 邮箱的smtp服务器的地址[邮局的地址,QQ的邮局地址是smtp.qq.com ]
        $mail->Host = "smtp.mxhichina.com";
        // 设置SMTP端口号 - 例如 25, 465 or 587[ 网易使用的是25,而QQ使用的465,因为QQ的是加密的 ]
        $mail->Port = 465;
        // 是否使用SMTP认证[帐号和授权码认证]
        $mail->SMTPAuth = true;
        // 设置使用ssl加密方式登录鉴权
        $mail->SMTPSecure = 'ssl';
        // 帐号[ 邮箱帐号,登录邮箱的帐号,如果是QQ,则是QQ号码 ]
        $mail->Username = "xx@xx.com";
        // 授权码[我们在服务商的个人后台开启smtp时设置的授权码,如果是QQ,则随机串,而网易的是我们自定义,阿里云邮箱无法设置,直接是邮箱登陆密码]
        $mail->Password = "xxxxxxx";
        // 邮件发件人[完整的邮箱地址,发件人的昵称]
        $mail->setFrom('xx@xx.com', 'xxx');
        // 邮件回复人[网站的邮箱地址和昵称,一般和上面的发件人是同一个]
        $mail->addReplyTo('xx@xx.com', 'xxx');
        // 邮件收件人[网站的邮箱地址,昵称]
        $mail->addAddress( $address, $nickname );
        // 邮件的标题
        $mail->Subject = $subject;
        //Read an HTML message body from an external file, convert referenced images to embedded,
        // 邮件的主体内容
        $mail->msgHTML( $content );
        // 当邮箱不识别HTML的时候,替换文本
        $mail->AltBody = 'This is a plain-text message body';
        // 邮件的附件[文件地址(一般是本地文件),重命名附件]
        //可以增加多个,再设置一行即可
        $mail->addAttachment($attachment,'我的附件');
    
        // 发送邮件,返回值是true/false
        // return $mail->send();    //直接返回邮件发送结果
        if (!$mail->send()) {
            echo "发送邮件失败!: " . $mail->ErrorInfo;
        } else {
            return 1;
        }
  • 相关阅读:
    Singleton 单例模式 泛型 窗体控制
    SpringMVC异常处理注解@ExceptionHandler
    如何实现beanutils.copyProperties不复制某些字段
    Spring 注解学习笔记
    使用客户端SVN在Update时遇到Previous operation has not finished; run 'cleanup' if it was interrupted,需要cheanup文件夹,解决方式
    jQuery的选择器中的通配符
    mysql str_to_date字符串转换为日期
    Mybatis中mapper.xml文件判断语句中的单双引号问题
    Jquery中each的三种遍历方法
    Javascript 中动态添加 对象属性
  • 原文地址:https://www.cnblogs.com/bk233/p/9529366.html
Copyright © 2020-2023  润新知