最近在博客加入了注册系统,注册怎么能少了邮箱验证呢
扒了好多博客,都没几个完整好用的
但是还是扒到了有用的插件 -- swiftmailer
首先下载 swiftmailer ,下载地址:https://github.com/swiftmailer/swiftmailer
下载解压后把lib文件夹重命名为 swiftmailer ,放入 ThinkPHP\Library\Vendor\文件夹下
再去控制器引用
$email_set = M('web_set')->where(array('id' => 1))->field('web_name,web_email,email_smtp,email_port,email_pwd')->find(); //从数据库获取邮箱配置,包括帐号,密码,服务器类型,端口$regtime = time();$token = md5($name . $pwd . $regtime);$token_exptime = $regtime + 24 * 3600;$user = array('name' => $name, 'email' => $email, 'pwd' => md5($pwd), 'status' => '0', 'token' => $token, 'token_exptime' => $token_exptime);$add = M('user')->add($user);if ($add) { /*----------------------------------------------- 这里是引用第三方类库
. 之前表示文件夹,之后表示要引用的文件
-----------------------------------------------*/vendor('swiftmailer.swift_required');$transport = \Swift_SmtpTransport::newInstance($email_set['email_smtp'], $email_set['email_port']); //这里因为路径问题要加上 \ $transport->setUsername($email_set['web_email']);$transport->setPassword($email_set['email_pwd']);$mailer = \Swift_Mailer::newInstance($transport);$message = \Swift_Message::newInstance();$message->setFrom(array($email_set['web_email'] => $email_set['web_name']));$message->setTo(array($email => $name));$message->setSubject('' . $email_set['web_name'] . ' 激活');
$str ='这里是邮件内容,本博客所用的邮件格式是贤心大神那里的验证邮件,但是发上来会被格式化,有需要的自己去扒吧';
$message->setBody("{$str}", 'text/html', 'utf-8');
$mailer->send($message);
if ($mailer) {
echo '{"status":"y","info":"发送成功,请在24小时内激活,否则帐号失效","code":"0"}';
} else {
$dele = M('user')->where(array('name' => $name, 'email' => $email))->delete();
echo '{"status":"n","info":"注册失败,请重新注册"}';
}
好了,到这里就搞定了,赶快去试一下吧!