0

I am getting trouble with the following lines of code. Actually I have created a custom file confirm_email.php in which I wrote the following piece of code. When I'm removing $temp_dir variable it works fine, but when I include it, this page returns internal server error

$temp_dir   = get_template_directory_uri();

$url        = $temp_dir."/confirm_email.php?id=".$_REQUEST['id']."&key=".$_REQUEST['key'];
$message    = "Username:".$_REQUEST['name']."Click on below link to confirm your email;".$url;

$subject    = "Email confirmation Link";
$headers    = "From: wordpress@property-penang.com" . "\r\n";

if ( mail( $_REQUEST['email'], $subject, $message, $headers ) ) {
    echo "1";
} else {
    echo "0";
}
  • Where and how is $url used later? – Jacob Peattie May 12 '18 at 8:38
  • i will send it to email – Mohd Hasan May 12 '18 at 8:41
  • No, the code. Show the code. That's where the issue will be. – Jacob Peattie May 12 '18 at 8:44
  • i updated the above code – Mohd Hasan May 12 '18 at 8:48
  • This script looks exploitable, with no validation/sanitazion. Also you don't want external evil bots calling this script 1000 times a minute. – birgire May 12 '18 at 10:50
1

Got the problem, Your confirm_email.php function should be inside the scope of wordpress. If you include the file into functions.php file or some file that is inside the wp scope then get_template_directory_uri() function will work.

You can get around the problem by adding the code into a filter or action hook instead of a file. You can try init hook. you can add some parameter in the url to check like http://your_domain.com/?is_email=true and then check it in the code like this:

if($_GET['is_email'] == true){
    // do your stuffs
}

Your solution code might be something like this:

function my_email_function(){
    if(isset($_GET['is_email']) && $_GET['is_email'] == true){
        $temp_dir   = get_template_directory_uri();

        $url        = $temp_dir."/confirm_email.php?id=".$_REQUEST['id']."&key=".$_REQUEST['key'];
        $message    = "Username:".$_REQUEST['name']."Click on below link to confirm your email;".$url;

        $subject    = "Email confirmation Link";
        $headers    = "From: wordpress@property-penang.com" . "\r\n";

        if ( mail( $_REQUEST['email'], $subject, $message, $headers ) ) {
            echo "1";
        } else {
            echo "0";
        }
    }
}
add_action('init', 'my_email_function');
  • Not working. giving same result – Mohd Hasan May 12 '18 at 10:08
  • Now try this solution. :) – Aqib Ashef May 12 '18 at 10:32
  • this is working – Mohd Hasan May 12 '18 at 14:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.