Jump to:

5540 Posts in 1738 Topics by 1224 members

Customising the CMS

SilverStripe Forums » Customising the CMS » Send welcome e-mail to new users

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 1158 Views
  • nekranox
    Avatar
    Community Member
    31 Posts

    Send welcome e-mail to new users Link to this post

    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

  • swaiba
    Avatar
    Forum Moderator
    1792 Posts

    Re: Send welcome e-mail to new users Link to this post

    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)

  • nekranox
    Avatar
    Community Member
    31 Posts

    Re: Send welcome e-mail to new users Link to this post

    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

  • swaiba
    Avatar
    Forum Moderator
    1792 Posts

    Re: Send welcome e-mail to new users Link to this post

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

  • simon_w
    Avatar
    Forum Moderator
    473 Posts

    Re: Send welcome e-mail to new users Link to this post

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

  • nekranox
    Avatar
    Community Member
    31 Posts

    Re: Send welcome e-mail to new users Link to this post

    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();
       
       
       
          
        }
       }
    }

    ?>

  • simon_w
    Avatar
    Forum Moderator
    473 Posts

    Re: Send welcome e-mail to new users Link to this post

    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.

    1158 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.