zoukankan      html  css  js  c++  java
  • Thinkphp 使用gmail发送邮件

    1.Thinkphp 发送邮件内容来自:http://www.thinkphp.cn/code/32.html

    /**
     * 系统邮件发送函数
     * @param string $to    接收邮件者邮箱
     * @param string $name  接收邮件者名称
     * @param string $subject 邮件主题 
     * @param string $body    邮件内容
     * @param string $attachment 附件列表
     * @return boolean 
     */
    function think_send_mail($to, $name, $subject = '', $body = '', $attachment = null){
        $config = C('THINK_EMAIL');
        vendor('PHPMailer.class#phpmailer'); //从PHPMailer目录导class.phpmailer.php类文件
        $mail             = new PHPMailer(); //PHPMailer对象
        $mail->CharSet    = 'UTF-8'; //设定邮件编码,默认ISO-8859-1,如果发中文此项必须设置,否则乱码
        $mail->IsSMTP();  // 设定使用SMTP服务
        $mail->SMTPDebug  = 0;                     // 关闭SMTP调试功能
                                                   // 1 = errors and messages
                                                   // 2 = messages only
        $mail->SMTPAuth   = true;                  // 启用 SMTP 验证功能
        $mail->SMTPSecure = 'ssl';                 // 使用安全协议
        $mail->Host       = $config['SMTP_HOST'];  // SMTP 服务器
        $mail->Port       = $config['SMTP_PORT'];  // SMTP服务器的端口号
        $mail->Username   = $config['SMTP_USER'];  // SMTP服务器用户名
        $mail->Password   = $config['SMTP_PASS'];  // SMTP服务器密码
        $mail->SetFrom($config['FROM_EMAIL'], $config['FROM_NAME']);
        $replyEmail       = $config['REPLY_EMAIL']?$config['REPLY_EMAIL']:$config['FROM_EMAIL'];
        $replyName        = $config['REPLY_NAME']?$config['REPLY_NAME']:$config['FROM_NAME'];
        $mail->AddReplyTo($replyEmail, $replyName);
        $mail->Subject    = $subject;
        $mail->MsgHTML($body);
        $mail->AddAddress($to, $name);
        if(is_array($attachment)){ // 添加附件
            foreach ($attachment as $file){
                is_file($file) && $mail->AddAttachment($file);
            }
        }
        return $mail->Send() ? true : $mail->ErrorInfo;
    }

    此函数只能在ThinkPHP中使用且需要phpmailer扩展的支持;
    phpmailer扩展的放置目录为 ThinkPHP/Extend/Vendor/PHPMailer/class.phpmailer.php
    phpmail的下载地址:
    http://sourceforge.net/projects/phpmailer/files/

    然后下载 Download PHPMailer_v5.1.zip (111.7 kB)
    使用此函数 必须在项目中加入以下配置项

    //邮件配置
    'THINK_EMAIL' => array(
        'SMTP_HOST'   => 'smtp.aaa.com', //SMTP服务器
        'SMTP_PORT'   => '465', //SMTP服务器端口
        'SMTP_USER'   => 'mail@aaa.com', //SMTP服务器用户名
        'SMTP_PASS'   => 'password', //SMTP服务器密码
        'FROM_EMAIL'  => 'mail@aaa.com', //发件人EMAIL
        'FROM_NAME'   => 'ThinkPHP', //发件人名称
        'REPLY_EMAIL' => '', //回复EMAIL(留空则为发件人EMAIL)
        'REPLY_NAME'  => '', //回复名称(留空则为发件人名称)
    ),

    2.发送邮件更简单版,具体来自内容请参考 ThinkPHP 中使用 PHPMailer 发送邮件(支持163、QQ邮箱,附下载)

    3.解决smtp server connect failed(连接smtp服务器失败)

    Using WampServer 2.2 I just edit php.ini in C:wampinphpphp5.3.13 then uncommentextension=php_openssl.dll

    (使用的是wamp集成环境,所以要到具体的目录下面看php.ini中

    ;extension=php_openssl.dll

    前面的分号是否去除了),xampp的也是修改php.ini文件

    解决smtp服务器连接不上

    • 添加 C:PHPext 到系统变量中,具体根据你php安装路径为准
    • 将 php 根目录下的 libeay32.dll,ssleay32.dll 两个文件 复制到 C:WINDOWSsystem32 中
    • 重启服务器

    参考了:Socket transport “ssl” in PHP not enabled

  • 相关阅读:
    原子操作--sync/atomic的用法
    基础的排序算法以及查找算法
    (三)MySQL终极篇
    (二)MySQL中级篇
    数据库表添加索引对性能的影响
    事务的四大特性以及事务的隔离级别
    int 和Integer
    数据库三范式
    Java反射
    获取Class实例的三种方式
  • 原文地址:https://www.cnblogs.com/fsong/p/3204760.html
Copyright © 2011-2022 走看看