• PHP利用PHPMailer组件的Gmail发信能力发送电子邮件


    系统环境:windows7+wamp5

    环境要求:Gmail账号+open_ssl+PHPMailer;

    1、默认情况下wamp5是没有打开Open_ssl的,这是需要修改php.ini,将extension=php_openssl.dll前面的分号去掉;

    2、下载PHPMailer PHPMailer是一个用PHP写的用于邮件发送的类。可以下載最新的版本,我用的是PHPMailer_v5.0.2

    3、将下好的包解压到服务器上,打开examples文件夾,然後在裏邊單獨建立一個PHP的測試文件(自己命名就好了),代碼為:

    //如果要轉載本文請注明出處,免的出現版權紛爭,我不喜歡看到那種轉載了我的作品卻不注明出處的人 Seven{See7di#Gmail.com}

    <?php
    //error_reporting(E_ALL);
    error_reporting(E_STRICT);
    date_default_timezone_set('America/Toronto');
    require_once('../class.phpmailer.php');
    //include("class.smtp.php"); //可选,会自动从class.phpmailer.php加载
    $mail=new PHPMailer();
    $body=file_get_contents('contents.html');
    $body=Strtr($body,Array("\\"=>""));//$body= eregi_replace("[\]",'',$body);
    $mail->IsSMTP();                            // 告诉程式要使用SMTP
    $mail->SMTPDebug  = 2;                        // 开启 SMTP debug 信息 (测试时使用)// 1 = 错误和消息// 2 = 只有消息
    $mail->SMTPAuth   = true;                    // 开启 SMTP 验证
    $mail->SMTPSecure = "ssl";                    // sets the prefix to the servier
    $mail->Host       = "smtp.gmail.com";        // sets GMAIL as the SMTP server
    $mail->Port       = 465;                    // set the SMTP port for the GMAIL server
    $mail->Username   = "see7di@gmail.com";        // GMAIL用户名
    $mail->Password   = "******";                // GMAIL密码
    $mail->CharSet = "utf-8";        //加入该行代码可以防止信件内容乱码

    $mail->SetFrom('see7di@gmail.com','张三');        //发信人邮件地址及用户名
    //$mail->AddReplyTo("see7di@gmail.com","张三");    //回复地址及用户名

    $subject='這是郵件標題';
    $mail->Subject    = "=?UTF-8?B?".base64_encode($subject)."?= ";//使用base64编码是为了防止信件标题乱码

    $mail->MsgHTML($body);

    $mail->AddAddress("see7di@msn.com","李四");        //接收者邮件地址及用户名
    //附件
    //$mail->AddAttachment("images/phpmailer.gif");      // attachment
    //$mail->AddAttachment("images/phpmailer_mini.gif"); // attachment

    if(!$mail->Send()) {
        echo "Mailer Error: ".$mail->ErrorInfo;
    }else{
        echo "Message sent!";
    }
    ?>

    OK!

  • 相关阅读:
    跳表(SkipList)及ConcurrentSkipListMap源码解析
    动态主机配置协议DHCP
    电子邮件
    万维网WWW
    运维技巧(4):管理邮箱收发限制
    运维技巧(3):管理邮箱配额限制:重点是powershell命令操作
    运维技巧(2):创建邮箱方法详解
    运维技巧(1):如何通过邮件头和传输跟踪日志查看原始客户端IP
    管理客户端连接(6):客户端软件可以连接上邮箱
    配置Exchange 防病毒和反垃圾邮件(9)
  • 原文地址:https://www.cnblogs.com/see7di/p/2239803.html
Copyright © 2020-2023  润新知