Jump to content

New User Welcome Message?


Lance O.
 Share

Recommended Posts

Does ProcessWire have include the ability to automatically send a welcome message to new users when their account is created? I'd like to send new users their login information, as well as some helpful links related to ProcessWire and their project. Ideally, I'd like to be able to edit the message.

Currently, I have the following set up in TextExpander. The capitalized words are changed using TextExpander's fill-in functionality.

Dear EMAIL,

Your new site has been successfully set up at:
http://DOMAIN/

You'll use ProcessWire to manage your site's content. You can log into your administrator account with the following information:

http://DOMAIN/processwire/
Username: USERNAME
Password: PASSWORD (case-sensitive)

Need to reset your password? Click on the 'Forgot your password?' link on the login page:
http://DOMAIN/processwire/

Learn how to create a strong and secure password:
http://www.google.com/goodtoknow/online-safety/passwords/

What makes ProcessWire so great? Learn about the set of features designed to make working with your content easy and enjoyable:
http://processwire.com/

Find out what makes ProcessWire unique from other content management systems:
http://processwire.com/about/what/

We hope you enjoy your new site!
 
Link to comment
Share on other sites

There's no built-in feature for this as far as I know, but something like this could be turned into a module with relative ease. Since users are essentially pages, your module could hook into saveReady() method of pages and do it's magic (such as sending an email using user credentials, essentially info taken from that page) to this user.

Password field is one I'm not very familiar with and thus I can't really say for sure if you'd be able to use it's actual value in this way. You may have to add another hook just to get that. Another option would be to avoid sending passwords and just instruct user to get a new password with "forgot your password" feature -- this is what many sites already do.

Anyway, in init() of your new module you'd add a hook:

$this->pages->addHook('saveReady', $this, 'sendEmail');

.. and that sendEmail method could look something like this, though probably you'd want to add some checks etc. (does email exists and so on):

public function sendEmail(HookEvent $event) {
    $page = $event->arguments[0];
    if ($page->template != "user") return; // stop here if this isn't a new user
    $message = "your message goes here.. or you could specify it in module settings. Hello {$page->name}!";
    mail($page->email, "welcome, {$page->name}!", $body);
}

You'll find plenty of examples on creating modules around the forum. I hope this helps you get started!

Edit: moved hook to "saveReady" instead of "saved."

Edited by teppo
  • Like 2
Link to comment
Share on other sites

I think you might get access to the password value if you hook before the page is saved, but I don't have time to give an example just now (Soma or someone else with fast typing fingers will doubtless be here soon with an example).

  • Like 1
Link to comment
Share on other sites

Actually, now that Pete mentioned it (kind of), it might be better to add that hook to saveReady instead of saved. You might have access to password in plain text at that point, but you'd still be (nearly) sure that page will get saved. When hooking to save, there's always the possibility that page won't get saved after all, and in that case it'd be awfully early to send that email :)

I'd still strongly suggest not sending passwords in plain text over unsecure mail protocol. That's why instruction to use "forgot your password?" feature is IMHO better choice.

  • Like 2
Link to comment
Share on other sites

  • 1 month later...
  • 8 months later...

There's no built-in feature for this as far as I know, but something like this could be turned into a module with relative ease. Since users are essentially pages, your module could hook into saveReady() method of pages and do it's magic (such as sending an email using user credentials, essentially info taken from that page) to this user.

Password field is one I'm not very familiar with and thus I can't really say for sure if you'd be able to use it's actual value in this way. You may have to add another hook just to get that. Another option would be to avoid sending passwords and just instruct user to get a new password with "forgot your password" feature -- this is what many sites already do.

Anyway, in init() of your new module you'd add a hook:

$this->pages->addHook('saveReady', $this, 'sendEmail');

.. and that sendEmail method could look something like this, though probably you'd want to add some checks etc. (does email exists and so on):

public function sendEmail(HookEvent $event) {
    $page = $event->arguments[0];
    if ($page->template != "user") return; // stop here if this isn't a new user
    $message = "your message goes here.. or you could specify it in module settings. Hello {$page->name}!";
    mail($page->email, "welcome, {$page->name}!", $body);
}

You'll find plenty of examples on creating modules around the forum. I hope this helps you get started!

Edit: moved hook to "saveReady" instead of "saved."

@teppo Thanks a lot for this example! I'm getting into learning hooks and this kind of examples are a lifesaver!!

I think that you missed an "@" before the mail function. Also, shouldn't this mail fire only when a new user is created? If new user ... fire the email. How would this be translated into code here?

Link to comment
Share on other sites

Let me elaborate a little bit more: I've been trying to pull this module off, and I haven't found the correct way to hook it. If I'm not mistaken, hooking into 'saveReady' in this case will trigger after saving AND editing the user. So In this case, it should trigger when the user page is Published (a.k.a the user is live and should be welcomed! :rolleyes: ). Looking into the hooks' list, I haven't found said hook or similar. So, I must be missing something, right?

Note that i've tried to hook after 'added' as well, and in this case it triggers after the name is set (and the page is first saved), so the user still hasn't filled his email to begin with.

<?php


class EmailNewUser extends WireData implements Module {

	/**
	 * getModuleInfo is a module required by all modules to tell ProcessWire about them
	 *
	 * @return array
	 *
	 */
	public static function getModuleInfo() {

		return array(

			// The module'ss title, typically a little more descriptive than the class name
			'title' => 'Email New Users', 

			// version: major, minor, revision, i.e. 100 = 1.0.0
			'version' => 101, 

			// summary is brief description of what this module is
			'summary' => 'An example to email New users',
			
			// Optional URL to more information about the module
			'href' => '',

			// singular=true: indicates that only one instance of the module is allowed.
			// This is usually what you want for modules that attach hooks. 
			'singular' => true, 

			// autoload=true: indicates the module should be started with ProcessWire.
			// This is necessary for any modules that attach runtime hooks, otherwise those
			// hooks won't get attached unless some other code calls the module on it's own.
			// Note that autoload modules are almost always also 'singular' (seen above).
			'autoload' => true, 
			);
	}

	/**
	 * Initialize the module
	 *
	 * ProcessWire calls this when the module is loaded. For 'autoload' modules, this will be called
	 * when ProcessWire's API is ready. As a result, this is a good place to attach hooks. 
	 *
	 */
	public function init() {

		
		$this->pages->addHookAfter('added', $this, 'sendEmail');

		

	}

	public function sendEmail(HookEvent $event) {
   		$page = $event->arguments[0];
    	if ($page->template != "user") return; // stop here if this isn't a new user
    	$body = "your message goes here.. or you could specify it in module settings. Hello {$page->name}!";
    	$this->message("Hello World! You created {$page->email}."); 
    	@mail($page->email, "welcome, {$page->name}!", $body, "From:xxxx@xxxx.com");
	}
	
}

Link to comment
Share on other sites

@landitus: you're right in that using the code I posted earlier message would get sent every time user is saved, which probably isn't correct behavior. One way to achieve what you're describing here would be by simply adding a check for changed status (published). You might want to take a look at Process Changelog for some sample code.

"@" character was missing by purpose; in PHP adding @ before function such as mail() suppresses errors. If you're relying on these emails getting sent, this makes very little sense, as it just tells PHP that "if this fails, no worries, just keep going as if nothing happened!" :)

  • Like 2
Link to comment
Share on other sites

Good to now! 

I was able to pull this off like this for now. Thanks a lot for your input! Maybe a config page will be better for a public module, off course.

<?php

class EmailNewUser extends WireData implements Module {

	/**
	 * getModuleInfo is a module required by all modules to tell ProcessWire about them
	 *
	 * @return array
	 *
	 */
	public static function getModuleInfo() {

		return array(

			// The module's title, typically a little more descriptive than the class name
			'title' => 'Email New Users', 

			// version: major, minor, revision, i.e. 100 = 1.0.0
			'version' => 101, 

			// summary is brief description of what this module is
			'summary' => 'Send New users a welcome email',
			
			// Optional URL to more information about the module
			'href' => '',

			// singular=true: indicates that only one instance of the module is allowed.
			// This is usually what you want for modules that attach hooks. 
			'singular' => true, 

			// autoload=true: indicates the module should be started with ProcessWire.
			// This is necessary for any modules that attach runtime hooks, otherwise those
			// hooks won't get attached unless some other code calls the module on it's own.
			// Note that autoload modules are almost always also 'singular' (seen above).
			'autoload' => true, 
			);
	}

	/**
	 * Initialize the module
	 *
	 * ProcessWire calls this when the module is loaded. For 'autoload' modules, this will be called
	 * when ProcessWire's API is ready. As a result, this is a good place to attach hooks. 
	 *
	 */
	public function init() {
		$this->pages->addHook('saveReady', $this, 'sendEmail');
	}

	public function sendEmail(HookEvent $event) {
   		$page = $event->arguments[0];
    	
    	if ($page->template != "user") return; // stop here if this isn't a User
    	
    	// Only send an email if the User page is published (A new User goes live)
    	if($page->isChanged('status') && !$page->is(Page::statusUnpublished)) {
    		$body = "Welcome {$page->name}!";
    		$this->message("Welcome {$page->name}! A welcome email has been sent to {$page->email}."); 
    		@mail($page->email, "welcome, {$page->name}!", $body, "From:xxxxx@xxxxx.com");
    	}
    	
	}

}

  • Like 6
Link to comment
Share on other sites

  • 6 months later...

Just wanted to note here in this post that I've built a configurable module for this and also included optional support for automatically generating the password for the new user. You can read more about it here: https://processwire.com/talk/topic/7051-email-new-user/

Also, there was some discussion above about how to get the entered password in plain text, but I don't think anyone actually came up with a working solution. I found that you can hook on InputfieldPassword::processInput and then you can get the plain text password with $event->object->value

  • Like 5
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...