php发送email(swiftmailer)

<?php
/**
 * swiftmailer 发送邮件
 * DB:https://github.com/ThingEngineer/PHP-MySQLi-Database-Class
 *
 *  1,链接mysql  pdo
 * $this->pdo = new PDO("mysql:host=localhost;dbname=test_db", "root", "");
 *
 * 2,链接mysql  mysqli
 * $connect=mysqli_connect('localhost','root','','test_db','3306');
 * $sql='select * from liukai';
 * mysqli_query($connect,'set names utf8');
 *
 * 3,qq邮箱  POP3/SMTP服务选中
 *
 */
header("Content-type:text/html;charset=utf-8");
require_once 'swiftmailer-master/lib/swift_required.php';
require_once 'DB/MysqliDb.php';

class SendEmail
{
    public $db = null;

    public function __construct()
    {
        $this->db = new MysqliDb (
            array(
                'host' => 'localhost',
                'username' => 'root',
                'password' => '',
                'db' => 'test_db',
                'port' => 3306,
                'prefix' => '',
                'charset' => 'utf8')
        );
    }

    /**
     * 注册
     * http://www.lar_v5.com/sendmail/sendmail.php?act=reg&username=abc&password=123456&email=2543645328@qq.com&token=c590a011e387088b1d8b52b0ee76cfcf
     */
    public function register()
    {
        $username = $_REQUEST['username'];
        $password = $_REQUEST['password'];
        $email = $_REQUEST['email'];
        $token = $this->getToken();
        $token_exptime = time() + 24 * 3600;
        $regtime = time();

        $data = compact('username', 'password', 'email', 'token', 'token_exptime', 'regtime');
        $id = $this->db->insert('test_db.email_user', $data);

        if ($id) {
            // 发送邮件,以QQ邮箱为例
            $emailPassword = '你自己的QQ邮箱密码';
            // 配置邮件服务器,得到传输对象
            $transport = Swift_SmtpTransport::newInstance('smtp.qq.com', 25);
            // 设置登录账号和密码
            $transport->setUsername('2397946202@qq.com');
            $transport->setPassword($emailPassword);
            // 得到发送邮件对象Swift_Mailer对象
            $mailer = Swift_Mailer::newInstance($transport);

            /************************************************/

            // 得到邮件对象
            $message = Swift_Message::newInstance();
            // 设置管理员的对象
            $message->setFrom(array(
                '2397946202@qq.com' => 'guyun'
            ));
            // 发送邮件给谁
            $message->setTo(array($email => 'test_mail'));
            $message->setSubject('激活邮件');
            $url = "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'] . "?act=active&token=" . $token;
            $urlencode = urlencode($url);

            $str = <<<EOF
亲爱的{$username}您好~!感谢您注册我们网站<br/>
请点击此链接激活账号即可登录!<br/>
<a href="{$url}">{$urlencode}</a>
<br/>
如果点此链接无反应,可以将其复制到浏览器中来执行,链接的有效时间为24小时。
EOF;
            $message->setBody("{$str}", 'text/html', 'utf-8');

            try {
                if ($mailer->send($message)) {
                    echo "恭喜您{$username}注册成功,请到邮箱激活之后登录<br/>";
                    echo '3秒钟后跳转到登录界面';
                    echo '<meta http-equiv="refresh" content="3;url=index.php"/>';
                }
            } catch (Swift_ConnectionException $e) {
                echo '邮件发送错误' . $e->getMessage();

            }
        } else {
            echo '用户注册失败,3秒钟后跳转到注册页面';
            echo '<meta http-equiv="refresh" content="3;url=index.php#toregister"/>';
        }
    }

    public function active()
    {
        $token = addslashes($_GET['token']);
        $sql = "SELECT * FROM test_db.email_user WHERE token={$token} AND status=0";
        $row = $this->db->query($sql);
        $now = time();
        if ($now > $row['token_exptime']) {
            echo '激活时间过期,请重新登录激活';
        } else {
            // 邮箱注册成功
            $dataRaw = array('status' => 1);
            $this->db->where('id', $row['id']);
            $result = $this->db->update('test_db.email_user', $dataRaw);
        }

        if ($result) {
            echo '用户注册失败,3秒钟后跳转到注册页面';
            echo '<meta http-equiv="refresh" content="3;url=index.php"/>';
        } else {
            echo '激活失败,请重新激活';
            echo '<meta http-equiv="refresh" content="3;url=index.php"/>';
        }
    }

    /**********************************************************************/

    /**
     * 登录
     */
    public function login()
    {
        $username = $_REQUEST['username'];
        $password = $_REQUEST['password'];

        $sql = "SELECT * FROM test_db.email_user WHERE username={$username} AND password={$password}";

        $row = $this->db->query($sql);
        if ($row['status'] == 0) {
            echo '请先激活再登录';
            echo '<meta http-equiv="refresh" content="3;url=index.php"/>';
        } else {
            echo '登录成功,3秒钟后跳转到首页';
            echo '<meta http-equiv="refresh" content="3;url=index.php"/>';
        }
    }


    public function getToken()
    {
        $string = 'werttyuiurw234';
        $token = md5($string . $_REQUEST['username'] . $_REQUEST['password'] . $_REQUEST['email']);
        return $token;
    }
}

magicQuotesGpc();

$model = new SendEmail();
switch ($_REQUEST['act']) {
    case 'reg':
        $model->register();
        break;
    case 'login':
        $model->login();
        break;
    default:
        // $to = $this->getToken();
        break;
}

function magicQuotesGpc()
{
    // stripslashes_deep (php5.2使用PHP5.4方法) 遍历数组,删除斜杠
    if (!function_exists("stripslashes_deep")) {
        function stripslashes_deep(&$value)
        {
            $value = is_array($value) ? array_map('stripslashes_deep', $value) : stripslashes($value);
            return $value;
        }
    }
    // 获取当前 magic_quotes_gpc 的配置选项设置
    if (get_magic_quotes_gpc()) {
        $_REQUEST = array_map("stripslashes_deep", $_REQUEST);
        $_GET = array_map("stripslashes_deep", $_GET);
        $_POST = array_map("stripslashes_deep", $_POST);
        $_COOKIE = array_map("stripslashes_deep", $_COOKIE);
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值