首页 > 学院 > 开发设计 > 正文

使用邮箱激活用户(TP框架)

2019-11-08 01:05:29
字体:
来源:转载
供稿:网友

1.下载phpmailer  http://webscripts.softpedia.com/PHPMailer-Codeworx-Technologies/download/解压到ThinkPHP/Library/Vendor目录下

2.在ThinkPHP/Common/中添加一个函数

function think_send_mail($email, $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调试功能    $mail->SMTPAuth   = true;                  // 启用 SMTP 验证功能       $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($email, $name);    if(is_array($attachment)){ // 添加附件        foreach ($attachment as $file){            is_file($file) && $mail->AddAttachment($file);        }    }    return $mail->Send() ? true : $mail->ErrorInfo;}

3.开启QQ邮箱里的SMTP,QQ会给你一个密码;

4.在App/Common/Conf/config.php中添加

//邮件配置   'THINK_EMAIL' => array(         'SMTP_HOST'   => 'smtp.qq.com', //SMTP服务器         'SMTP_PORT'   => '25', //SMTP服务器端口         'SMTP_USER'   => '你的邮箱', //SMTP服务器用户名         'SMTP_PASS'   => 'QQ给你的密码,不是登录密码', //SMTP服务器密码         'FROM_EMAIL'  => '你的邮箱', //发件人EMAIL         'FROM_NAME'   => '', //发件人名称         'REPLY_EMAIL' => '', //回复EMAIL(留空则为发件人EMAIL         'REPLY_NAME'  => '', //回复名称(留空则为发件人名称)   ),5.开启php.ini里的extension=php_openssl.dll;

6.在控制器里调用函数

//导入文件vendor('PHPMailer.PHPMailerAutoload');$email = I('post.email');$name = I('post.username');think_send_mail($email, $name, $subject = '用户激活', $body = "   尊敬的客户:<br/>感谢您在我站注册了新帐号。<br/>请点击链接激活您的帐号。<br/>            <a href='http://你的网址/index.php/Home/Login/verify/id/{$id}'             target='_blank'>点击这里</a><br/>");


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表