Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Customising the CMS /

Send welcome e-mail to new users


Reply


7 Posts   1203 Views

Avatar
nekranox

Community Member, 31 Posts

11 March 2011 at 5:09am

Edited: 11/03/2011 5:10am

Hey guys,

There has been a few previous threads on this which have helped me work out what I need to do but I'm stuck at the last hurdle.

I'm decorating the Member class, adding a bit of code to e-mail a welcome message to users added into the CMS. The e-mail has dynamic data in for the users name, login details and e-mail address. Here's my decorator class (MemberDecorator.php):

<?php

class MemberDecorator extends DataObjectDecorator {

   function onAfterWrite() {
   
   parent::onAfterWrite();
   
    if (isset($this->Created)) {
      
   
         // send e-mail
         $From = 'noreply@some.domain';
         $To = $this->Email;
         $Subject="Your account details";
         $Body = '<h3>Your account details</h3>
   <p>Hello '.$this->FirstName.' '.$this->LastName.',</p>
   <p>This is an automated message from the website to let you know that we\'ve set up an account for you to access the Members Extranet. Here are your details:</p>
   <p>Login: <strong>'.$this->Email.'</strong><br />Password: <strong>'.$this->Password.'</strong></p>
   <p>If you have any problems with these details, please contact us. Details can be found on the website.</p>';
         
         $email = new Email($From, $To, $Subject, $Body);
         $email->send();
   
   
   
      
    }
   }
}

?>

I think the code is sound in theory but I'm not accessing the Member properties properly, the e-mail isn't showing up unless I hard-code the "To" variable and then all of the dynamic content in the e-mail is missing. Do I have to query or pass the member record somehow? I could just query the latest entry in the table but that's not elegant. Please help!

Thanks

Avatar
swaiba

Forum Moderator, 1799 Posts

11 March 2011 at 6:04am

Might some crazy, but try doing a $doMember = DataObject::get_by_id('Member',$this->ID); then use $doMember instead of $this. I think I may have done something like this before. But whenever I have members added it is usually funnelled through a form and I do the email send there (the same placed I'd add them to a newsletter group for example)

Avatar
nekranox

Community Member, 31 Posts

12 March 2011 at 3:52am

Even $this->ID is blank! If I paste the code directly into the onBeforeWrite in Member.php then it works fine. I want to do it properly though :(

Avatar
swaiba

Forum Moderator, 1799 Posts

12 March 2011 at 4:37am

hmmmmm
strange that Created is there but ID is not, why not try is_numeric($this->ID) instead?

Avatar
simon_w

Forum Moderator, 474 Posts

12 March 2011 at 8:01am

To access values on the object you're decorating, you need to use $this->owner->Value, so $this->owner->Email.

Avatar
nekranox

Community Member, 31 Posts

18 March 2011 at 2:16am

Thanks Simon!

It's working now but I'm getting an e-mail every time I log in rather than just when the account is created. Is my e-mail code assigned to the wrong event?

<?php

class MemberDecorator extends DataObjectDecorator {

   function onAfterWrite() {
   
   parent::onAfterWrite();
   
    if (isset($this->owner->Created)) {
      
   
         // send e-mail
         $From = 'noreply@some.domain';
         $To = $this->owner->Email;
         $Subject="Your account details";
         $Body = '<h3>Your account details</h3>
   <p>Hello '.$this->owner->FirstName.' '.$this->owner->LastName.',</p>
   <p>This is an automated message from the website to let you know that we\'ve set up an account for you to access the Members Extranet. Here are your details:</p>
   <p>Login: <strong>'.$this->owner->Email.'</strong><br />Password: <strong>'.$this->owner->Password.'</strong></p>
   <p>If you have any problems with these details, please contact us. Details can be found on the website.</p>';
         
         $email = new Email($From, $To, $Subject, $Body);
         $email->send();
   
   
   
      
    }
   }
}

?>

Avatar
simon_w

Forum Moderator, 474 Posts

18 March 2011 at 7:20am

Yup. You'll want it in onBeforeWrite() and have a check for !$this->owner->ID as it gets set on the first write, so will only be false-equivalant once.