Send user email help

Forum
Last Post
Threads / Messages

GeneticAlpha

Loving Mysidia!
Member
Joined
May 24, 2020
Messages
287
Points
18
Age
31
Location
Tennessee
Mysidian Dollar
13,377
I was hoping to get help on being able to have a skelton code that could automatically send a user an email. I noticed the donate system had one, but I couldn't figure out how to alter it. I'm wanting one for passwords and email resets.

The donate email user was this:

$recipient->getoptions();
if($recipient->options->newmessagenotify == 1){
// We are sending this user an email about the donation
$headers = "From: {$mysidia->settings->systememail}";
$sitename = $mysidia->settings->sitename;
$message = "Hello {$recipient->username};\n\nYou have received {$amount} {$mysidia->settings->cost} donation from {$this->username} at {$sitename}.
Thank You. The {$siteName} team.";

mail($recipient->email, $sitename." - You Have Received a {$mysidia->settings->cost} Donation", $message, $headers);
 
The email system uses a simple PHP mail function, which has 4 parts:

1. the recipient email.
2. the email subject.
3. the email body.
4. the email headers.

Also this code checks if the user has enabled email notification for certain actions:

PHP:
if($recipient->options->newmessagenotify == 1){
    // code that executes if user has email notification enabled.
}

For your usecase, you may write something like this for updating email in the file account.php(at around line 31):

PHP:
    public function email(){
        $mysidia = Registry::get("mysidia");       
        if($mysidia->input->post("submit")){
            $validator = new UserValidator($mysidia->user, array("email" => $mysidia->input->post("email")));
            $validator->validate("email");
           
            if(!$validator->triggererror()){
                $mysidia->db->update("users", array("email" => $mysidia->input->post("email")), "username = '{$mysidia->user->username}'");
                $options = $mysidia->user->getoptions();
                if($options->newmessagenotify == 1){
                    $headers = "From: {$mysidia->settings->systememail}";
                    $subject = "{$mysidia->settings->sitename} - You have updated your email address";
                    $message = "Congrats, you have successfully changed your email address to: {$mysidia->input->post('email')}";
                    mail($mysidia->input->post("email"), $subject, $message, $headers);
                }
            }
            else throw new EmailException("email_invalid");
        }
    }
 
The email system uses a simple PHP mail function, which has 4 parts:

1. the recipient email.
2. the email subject.
3. the email body.
4. the email headers.

Also this code checks if the user has enabled email notification for certain actions:

PHP:
if($recipient->options->newmessagenotify == 1){
    // code that executes if user has email notification enabled.
}

For your usecase, you may write something like this for updating email in the file account.php(at around line 31):

PHP:
    public function email(){
        $mysidia = Registry::get("mysidia");     
        if($mysidia->input->post("submit")){
            $validator = new UserValidator($mysidia->user, array("email" => $mysidia->input->post("email")));
            $validator->validate("email");
         
            if(!$validator->triggererror()){
                $mysidia->db->update("users", array("email" => $mysidia->input->post("email")), "username = '{$mysidia->user->username}'");
                $options = $mysidia->user->getoptions();
                if($options->newmessagenotify == 1){
                    $headers = "From: {$mysidia->settings->systememail}";
                    $subject = "{$mysidia->settings->sitename} - You have updated your email address";
                    $message = "Congrats, you have successfully changed your email address to: {$mysidia->input->post('email')}";
                    mail($mysidia->input->post("email"), $subject, $message, $headers);
                }
            }
            else throw new EmailException("email_invalid");
        }
    }

So I got it working but I had to add this to the view file instead of the plain account.php file.
 
Last edited:
Yeah you can add the code anywhere you want to as long as it works, I recommend in the controller since ideally the view should not be handling application logic, but only with anything related to presentation.
 
Yeah you can add the code anywhere you want to as long as it works, I recommend in the controller since ideally the view should not be handling application logic, but only with anything related to presentation.
Thank you I appreciate it :) I originally did try it in the controller, but the code didn't work. So I tried it in the view file just to see what it'd do and the function worked. I'm not sure what I did wrong, but I installed it where it was supposed to go I'm pretty sure in controller, it just didn't function, no errors either. But the same code worked for view file.
 
Oh I see, I assumed you were talking about Mys v1.3.5. For Mys v1.3.6, the code in controller is a bit different, so here you go:

PHP:
    public function email(){
        $mysidia = Registry::get("mysidia");    
        if($mysidia->input->post("submit")){
            if(!$this->accountService->isValidEmail($mysidia->input->post("email"))){
                throw new EmailException("email_invalid");
            }
            $mysidia->user->setEmail($mysidia->input->post("email"), Model::UPDATE);
            $options = $mysidia->user->getOption();
            if($options->hasNewMessageNotify()){
                $headers = "From: {$mysidia->settings->systememail}";
                $subject = "{$mysidia->settings->sitename} - You have updated your email address";
                $message = "Congrats, you have successfully changed your email address to: {$mysidia->input->post('email')}";
                mail($mysidia->input->post("email"), $subject, $message, $headers);
            }
        }
    }

To avoid confusion, next time make sure to mention which version of Mysidia you are talking about.
 
Last edited:
Oh I see, I assumed you were talking about Mys v1.3.5. For Mys v1.3.6, the code in controller is a bit different, so here you go:

PHP:
    public function email(){
        $mysidia = Registry::get("mysidia");   
        if($mysidia->input->post("submit")){
            if(!$this->accountService->isValidEmail($mysidia->input->post("email"))){
                throw new EmailException("email_invalid");
            }
            $mysidia->user->setEmail($mysidia->input->post("email"), Model::UPDATE);
            $options = $mysidia->user->getOption();
            if($options->hasNewMessageNotify()){
                $headers = "From: {$mysidia->settings->systememail}";
                $subject = "{$mysidia->settings->sitename} - You have updated your email address";
                $message = "Congrats, you have successfully changed your email address to: {$mysidia->input->post('email')}";
                mail($mysidia->input->post("email"), $subject, $message, $headers);
            }
        }
    }

To avoid confusion, next time make sure to mention which version of Mysidia you are talking about.
Oh uh, I must of said something wrong by saying controller earlier, because this was for 1.3.5.. Not my friend's 1.3.6. Sorry for the confusion. I just meant the code didn't function in my account.php controller file, but it did in the accountview.php for email.
 
Oh uh, I must of said something wrong by saying controller earlier, because this was for 1.3.5.. Not my friend's 1.3.6. Sorry for the confusion. I just meant the code didn't function in my account.php controller file, but it did in the accountview.php for email.
Then its even more strange, 'cause with Mys v1.3.5 the code earlier should be working.
 
I'll try it again soon to see if I can get it to function from the regular file instead of the view file again. :)
 

Similar threads

Users who are viewing this thread

  • Forum Contains New Posts
  • Forum Contains No New Posts

Forum statistics

Threads
4,274
Messages
33,115
Members
1,602
Latest member
BerrieMilk
BETA

Latest Threads

Latest Posts

Top