电子邮件确认链接无法在PHP中运行

时间:2016-03-02 08:08:00

标签: php wordpress confirmation email-confirmation

我正在开发一个带有电子邮件确认的自定义页面模板。一切正常,除非我点击电子邮件中的链接,它给了我“找不到页面”。

PHP脚本中的问题是什么?

以下是片段:

ob_start();
$email= '';

if(isset($_POST['submit'])){
if(!($_POST['subscriptions']=="")){
    $email= isset($_POST['subscriptions']) ? $_POST['subscriptions'] : '';

function encryptLink($stringValue){
    $key = "12345";
    $qryStr = "subscriptions=".$stringValue;
    $query = base64_encode(urlencode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, md5($key), $qryStr, MCRYPT_MODE_CBC, md5(md5($key)))));
    $link = 'www.example.com/testEnvironment/confirmation/'.$query;

    return $link;
    }
    $string = $email;
    $pagelink = encryptLink($string);

    //Email Details
    $mail_to = $email;
    $from_mail = "dummyemail@example.com";
    $from_name = "";
    $reply_to = "";
    $subject = "Confirmation";
    $message_body =
'Hey,

<span style="overflow-wrap: break-word; word-wrap: break-word;">'.$pagelink.'</span>';

    //Generate a boundary
    $boundary = md5(uniqid(time()));

    //Email Header
    $header = "From: ".$from_mail." \r\n";
    $header .= "Reply-To: ".$reply_to."\r\n";
    $header .= "MIME-Version: 1.0\r\n";
    $header .= "Content-Type: multipart/mixed;\r\n";
    $header .= " boundary=\"".$boundary."\"";

    //Multipart wraps the Email Content
    $message_body .= "\r\n\r\n";
    $message_body  .= "--".$boundary."\r\n";
    $message_body  .= "Content-Type: text/html; charset=\"iso-8859-1\"\r\n";
    $message_body .= "Content-Transfer-Encoding: 7bit\r\n";
    $message_body  .= "\r\n";
    $message_body .= "$message_body\r\n";
    $message_body .= "--".$boundary."\r\n";

        //Send email
        if (mail($mail_to, $subject, $message_body, $header)){
            echo "Sent";
        } else {
            echo "Error";
        }
    }
}

确认页面PHP代码段:

$key = "12345";

$queryString = rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, md5($key), urldecode(base64_decode($_SERVER['QUERY_STRING'])), MCRYPT_MODE_CBC, md5(md5($key))), "\0");

parse_str($queryString);

if(!empty($stringValue)){
  echo $stringValue;
} else{
  exit("Invalid parameters passed");
}

2 个答案:

答案 0 :(得分:1)

我认为问题在于您正在使用服务器未准备好的链接 - base64编码的字符串似乎是生成的url中的页面或目录,因为它是随机的不存在 - 因此需要在php中重写规则和相关逻辑。

在htaccess文件中可能有这样的规则:

RewriteEngine On
RewriteBase /testEnvironment/   
RewriteRule ^confirmation/([a-zA-Z0-9_-]+)$ confirmation/index.php?data=$1 [NC,L]
/* this will need to be adjusted to your url - is it simply confirmation.php or other? */

注意data

<?php
    if( $_SERVER['REQUEST_METHOD']=='GET' && isset( $_GET['data'] ) ){

        $data=$_GET['data'];
        $key = "12345";


        $queryString = rtrim( mcrypt_decrypt( MCRYPT_RIJNDAEL_256, md5( $key ), urldecode( base64_decode( $data ) ), MCRYPT_MODE_CBC, md5( md5( $key ) ) ), "\0");


        /* there is no return value from parse_str() so you would need to manually retrieve the variables to find `subscriptions` */
        parse_str( $queryString );


        /* does this show anything ? */
        print_r( $queryString );


        /*
        if(!empty( $stringValue )){
          print_r( $stringValue );
        } else{
          exit("Invalid parameters passed");
        }
        */          
    }
?>


My test environment
-------------------

## .htaccess ##
RewriteEngine On
RewriteBase /
RewriteRule ^stackconfirm/([a-zA-Z0-9_-]+)(/)?$ /test/stackconfirm.php?data=$1 [NC,L]



/* PHP script to generate email and confirmation link*/

function encryptLink( $stringValue ){
    $key = "12345";
    $qryStr = "subscriptions=".$stringValue;
    $query = base64_encode( urlencode( mcrypt_encrypt( MCRYPT_RIJNDAEL_256, md5( $key ), $qryStr, MCRYPT_MODE_CBC, md5( md5( $key ) ) ) ) );

    /* for my test I changed this portion to match url structure on test server */
    $link = '/stackconfirm/'.$query;
    return $link;
}


$link=encryptLink('bobby.dazzler@razzledazzle.com');
echo "<a href='$link'>Click here to confirm membership</a>";



stackconfirm.php
----------------

<?php
    if( $_SERVER['REQUEST_METHOD']=='GET' && isset( $_GET['data'] ) ){

        $data=$_GET['data'];
        $key = "12345";

        $queryString = rtrim( mcrypt_decrypt( MCRYPT_RIJNDAEL_256, md5( $key ), urldecode( base64_decode( $data ) ), MCRYPT_MODE_CBC, md5( md5( $key ) ) ), "\0");

        echo '<pre>', $data, PHP_EOL, print_r( $queryString,true ), '</pre>';
    }
?>


Result
------
subscriptions=bobby.dazzler@razzledazzle.com

答案 1 :(得分:0)

您正在将密钥与确认网址直接连接。它总是会导致404错误页面,因为该链接不存在。 相反,您可以通过get请求获取查询并在确认脚本中处理它,如下所示。

$link = 'www.example.com/confirmation/?key='.$query;

在确认脚本中,通过get方法获取查询,如下所示。

$key=$_GET['key'];

让原始密钥存储为$original_key=12345; 现在检查传递的密钥是否与原始密钥类似

if($key==$original_key)
{
//confirm e-mail
}
else
{
//invalid key
}