Email.php

<?php

namespace Tlf\User;

trait Email {

    public function sendRegisterEmail($user){
        $code = $this->newActivationCode($user);
        $url = $this->package->get('Site.url').$this->package->url("/register/{$code}/");
        $linkTitle = "Complete Registration";
        $siteName = $this->package->get('Site.name');
        $message = "Click the link below to register your {$siteName} account.\n<br>\n"
                ."<a href=\"{$url}\">{$linkTitle}</a>";
        $this->package->get('Site.name');
        $sent = $this->sendEmail(
            [   'to'=> $user->email,
                'to.name'=>$user->name,
                'subject'=>"Registration",
                'message'=>$message
            ]
        );
        
        return $sent;   
    }

    /**
     * Sends an email to someone from your no-reply address. Requires `Email.noreply` be set to an email & `Site.name` be set to a string. Both are set on the package, not passed as `$details`
     * 
     * You may set Debug.sendEmails to FALSE on the package, for testing. This will cause the email to be printed to your browser, instead of an email being actually sent.
     * 
     * @param array $details - Requires keys 'to' (email), 'to.name', 'subject', and 'message' (html string)
     * @throws \Exception
     * @return bool - true if mail() succeedss. false if mail() fails
     */
    public function sendEmail($details){
        //verify values are set
        $required = ['to','to.name','subject','message'];
//         print_r($details);exit;
        foreach ($required as $k){
            if (empty($details[$k])){
                throw new \Exception("\nCan't send email without all of the following set: ".implode(', ',$required));
            }
        }
        $fromEmail = $this->package->get('Email.noreply');
        if ($fromEmail==null)throw new \Exception("\nYou must set the Email.noreply configuration to an email address on the User package.\n");
        $siteName = $this->package->get('Site.name');
        if ($siteName==null)throw new \Exception("\nYou must set the Site.name configuration on the User Package to send emails.");
        //Set up paramaters
        $subject = $details['subject']. ' | '.$siteName;
        $fromName = $siteName.' no-reply';
        
        $toEmail = $details['to'];
        $toName = $details['to.name'];
        //send the email
        
        $message = $details['message'];
        
        if ($this->package->get('Debug.sendEmails')===false){
            $message = '<strong>Debugging, Email Message:</strong><br>'."\n".$message;
            echo $message;
            $mailSent = true;
            $this->lia->gotoSetMessage($message);
        } else {
            $mailSent = mail($toEmail,$subject,
                $message,
                "MIME-Version: 1.0\n"
                ."Content-type: text/html; charset=iso-8859-1\n"
                ."To: {$toName} <".$toEmail.">\n"
                    ."From: {$fromName} <{$fromEmail}>\n"
                    ."Reply-to: {$fromName} <{$fromEmail}>"
            );
        }
        return $mailSent;
    }

}