php发送email(swiftmailer)

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('[email protected]');
            $transport->setPassword($emailPassword);
            // 得到发送邮件对象Swift_Mailer对象
            $mailer = Swift_Mailer::newInstance($transport);

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

            // 得到邮件对象
            $message = Swift_Message::newInstance();
            // 设置管理员的对象
            $message->setFrom(array(
                '[email protected]' => '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 = <<
请点击此链接激活账号即可登录!
{$urlencode}
如果点此链接无反应,可以将其复制到浏览器中来执行,链接的有效时间为24小时。 EOF; $message->setBody("{$str}", 'text/html', 'utf-8'); try { if ($mailer->send($message)) { echo "恭喜您{$username}注册成功,请到邮箱激活之后登录
"; echo '3秒钟后跳转到登录界面'; echo ''; } } catch (Swift_ConnectionException $e) { echo '邮件发送错误' . $e->getMessage(); } } else { echo '用户注册失败,3秒钟后跳转到注册页面'; echo ''; } } 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 ''; } else { echo '激活失败,请重新激活'; echo ''; } } /**********************************************************************/ /** * 登录 */ 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 ''; } else { echo '登录成功,3秒钟后跳转到首页'; echo ''; } } 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); } }

你可能感兴趣的:(PHP基础)