zoukankan      html  css  js  c++  java
  • php 使用phpmailer 发送邮件(附带中文乱码的解决方法)

    下载phpmailer ,在程序里包含class.phpmailer.php 类  ,这里有中文乱码的解决方法

    实例代码如下

    <html>
        <head>
            <title>PHPMailer - Mail() basic test</title>
        </head>
        <body>
    
            <?php
            //header( "Content-type: text/html; charset=UTF-8" );    //设置本地编码
            //setlocale( LC_ALL, 'GBK' );
            //error_reporting(E_ALL);
            error_reporting( E_STRICT );
    
            date_default_timezone_set( 'America/Toronto' );
    
            require_once('class.phpmailer.php');    //必须包含的文件
    //include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded
    
            $mail = new PHPMailer();
            $mail->CharSet = "UTF-8";        //中文环境下需要设置编码
            //  $body = file_get_contents( 'contents.html' );    //包含网页的使用方法
            //$body = eregi_replace( "[]", '', $body );
            $body = '测试邮件,附上中文乱码解决方法:' . "<br>" .'123';   //设置邮件内容   使用 <br> 换行
            $mail->IsSMTP(); // telling the class to use SMTP
            $mail->Host = "smtp.163.com"; // SMTP server
            $mail->SMTPDebug = 2;                     // enables SMTP debug information (for testing)
            // 1 = errors and messages
            // 2 = messages only
            $mail->SMTPAuth = true;                  // enable SMTP authentication
            $mail->Host = "smtp.163.com"; // 邮箱服务器地址
            $mail->Port = 25;                    // 邮箱服务器端口
            $mail->Username = "1xxxxxxx@163.com"; // 你的邮箱用户名
            $mail->Password = "abcdefg@0";        // 你的邮箱密码
    
            $mail->SetFrom( '1xxxxxxx@163.com', '小----洋 ' );  //发送人
    
            //$mail->AddReplyTo( "2xxxxxxxx@qq.com", "亲" );    //接收方
    
    
            $mail->Subject = "=?utf-8?B?" . base64_encode( "我是标题" ) . "?=";      //解决中文标题乱码问题  设置标题
    
            $mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
    
            $mail->MsgHTML( $body );
            $address = "sheapchen@163.com";
            $mail->AddAddress( $address, "John Doe" );
    
            //$mail->AddAttachment( "附件1" );      // attachment      //这里可以添加附件
            // $mail->AddAttachment( "附件2" ); // attachment      
    
            if ( !$mail->Send() ) {
                echo "Mailer Error: " . $mail->ErrorInfo;
            } else {
                echo "Message sent!";
            }
            ?>
    
        </body>
    </html>
    
  • 相关阅读:
    Spring MVC Ajax 嵌套表单数据的提交
    Spring MVC 过滤静态资源访问
    Spring MVC 页面跳转时传递参数
    IDEA Maven 三层架构 2、运行 springMVC
    IDEA Maven 三层架构 1、基本的Archetype 搭建
    EasyUI DataGrid 基于 Ajax 自定义取值(loadData)
    Spring MVC Ajax 复杂参数的批量传递
    Mybatis Sql片段的应用
    在 Tomcat 8 部署多端口项目
    自动升级的设计思路与实现
  • 原文地址:https://www.cnblogs.com/sheapchen/p/3361930.html
Copyright © 2011-2022 走看看