API page: https://api.drupal.org/api/drupal/includes!mail.inc/function/drupal_mail/7

A call of drupal_mail() in a php file located outside the DRUPAL_ROOT send an email containing incorrect activation link.

Drupal is installed at the top level folder : /site (regular install)
I use a myregistration.php file in site/myfolder/mysubfolder/ allowing to authenticate and register user from a mobile app :

...
define( 'DRUPAL_ROOT' , $_SERVER['DOCUMENT_ROOT'] );
// bootstrap include
require_once DRUPAL_ROOT.'/includes/bootstrap.inc';
// Load Drupal
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
	
$newUser = array(
	'name' => $username,
	'pass' => $password, // note: do not md5 the password
	'mail' => $email,
	'status' => 0,
	'init' => $email,
	'signature_format' => 'full_html',
	'language' => 'fr',
	'roles' => array( DRUPAL_AUTHENTICATED_RID => 'authenticated user' )
);
$account = user_save( null , $newUser );
drupal_mail('user', 'register_no_approval_required', $email, NULL, array('account' => $account),  variable_get('site_mail', 'noreply@example..com'));

What works well :
user creation
user email sending

What doesn't work :
in the email, the links are wrong, I get : http://www.site.com/myfolder/mysubfolder/user/onetimelink
instead of : http://www.site.com/user/onetimelink

Notes :
- manually deleting the extra wrong "myfolder/mysubfolder" makes a valid url which allows the user to finish registration
- the email template uses [user:one-time-login-url] and [site:login-url]
- same issue when working with the user registration password module and its [user:registrationpassword-url]

Comments

cilefen’s picture

You are using drupal_mail() in a way that is different than what is intended. Have you tried drupal_mail() inside a Drupal hook_menu callback function?

fvince’s picture

thks for your answer !
No, I haven't try yet, but it seems to be a huge programming step...
Do you think there's no other way to achieve what i need : register user outside Drupal (with email address verification) ?

cilefen’s picture

@fvince hook_menu() is widely used in modules so you should not find it hard to find documentation and examples. I do not understand what you are trying to accomplish with this project. If you give more information maybe I could suggest something.

fvince’s picture

Let me explain...
In my mobile app (not a web app), users can log in to access special services. The app do not have access to my Drupal site.
Since Drupal registration process is complete, secure and flexible, and, users can access other services from a browser in my Drupal site, I figured out that they can have an unique account for both (app and Drupal site).
In the app, a register form mimics the Drupal one with email, id & pwd inputs, these fields being locally stored.
Thanks for your time !

fvince’s picture

I'll get a try on DrupalGap module and sdk... and report back !

cilefen’s picture

Component: documentation » base system
Category: Bug report » Support request

Cool. I am changing this to a "Support request" because drupal_mail() is probably working as designed.

fvince’s picture

Finally, I'll use the Services module for log in and register users outside drupal.
IMO, I still think there is an issue with the generated paths, they are simply wrong as they should sticked to http://www.mysite.com/DRUPAL_ROOT/user/... It's not caused by drupal_mail() itself, but the function in charge of creating the paths.
Thanks for your time !

amlynarcik’s picture

Hey FVince,

I completely agree with you, the registration links should be created using the drupal root, not the path from which they were created. We have setup an api outside of Drupal to allow a third party marketing service to send us JSON data to register users. Having the incorrect registration links is completely ruining my afternoon.

cilefen’s picture

Component: base system » user.module
Issue tags: -user registration, -activation email

This is in the user module. The link is generated in this function in user.module:

function user_pass_reset_url($account) {
  $timestamp = REQUEST_TIME;
  return url("user/reset/$account->uid/$timestamp/" . user_pass_rehash($account->pass, $timestamp, $account->login, $account->uid), array('absolute' => TRUE));
}

You should probably investigate the url() function as regards the Drupal root.