Jump to content

Send an email to a particular admin


kUdtiHaEX

Recommended Posts

I am building a custom module and one of the things I need to do is to be able to send an email to the admin of my choice.

I know that there is a 'SendAdminEmail` API that can be used but I need to be able to send the message using email template I've previously registered to one or more admins. How can I achieve this? 

Link to comment
Share on other sites

That is not what I've asked. Please read my question before answering - I have 15 admin accounts and I want to send an email to one of them. I know how to load the template and all of that, but how can I send it to a specific e-mail address of a particular admin account?

Link to comment
Share on other sites

To give an answer to me. 

WHMCS has a WHMCS\Mail class but it cannot be used, at least not publicly as the documentation states that this class has no methods. 

So what you need to do instead is to write own mailing class:

<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;


class EmailDispatcher {
    private $smtpUsername;
    private $smtpPassword;
    private $smtpHost;
    private $smtpPort;
    private $fromEmail;
    private $fromName;
    private $mailer;
function __construct()
    {
        $this->setSmtpUsername();
        $this->setSmtpPassword();
        $this->setSmtpHost();
        $this->setSmtpPort();
        $this->setFromEmail();
        $this->setFromName();
        $this->initMailer();
    }

    private function setSmtpUsername()
    {
        $this->smtpUsername = Configuration::get('SMTPUsername');
    }

    private function setSmtpPassword()
    {
        $command = 'DecryptPassword';
        $data = [
            'password2' => Configuration::get('SMTPPassword')
        ];
        $decryptedPassword = \localAPI($command, $data);

        $this->smtpPassword = $decryptedPassword['password'];
    }

    /**
     * SMTP Host setter
     *
     * @return void
     */
    private function setSmtpHost()
    {
        $this->smtpHost = Configuration::get('SMTPHost');
    }

    /**
     * SMTP port setter
     *
     * @return void
     */
    private function setSmtpPort()
    {
        $this->smtpPort = Configuration::get('SMTPPort');
    }

    /**
     * From email setter
     *
     * @return void
     */
    private function setFromEmail()
    {
        $this->fromEmail = Configuration::get('SystemEmailsFromEmail');
    }

    /**
     * From name setter
     *
     * @return void
     */
    private function setFromName()
    {
        $this->fromName = Configuration::get('SystemEmailsFromName');
    }

    /**
     * Configures PHPMailer
     *
     * @return void
     */
    private function initMailer()
    {
        $this->mailer = new PHPMailer();
        $this->mailer->isSMTP();
        $this->mailer->isHTML(true);
        $this->mailer->Host = $this->smtpHost;
        $this->mailer->SMTPAuth = true;
        $this->mailer->Username = $this->smtpUsername;
        $this->mailer->Password = $this->smtpPassword;
        $this->mailer->Port = $this->smtpPort;
        $mail->SMTPSecure = "tls";
        $this->mailer->setFrom($this->fromEmail, $this->fromName);
        $this->mailer->Subject = 'Subject';

        $adminUsers = new \WhmcsAdmin();
        $settings = new Settings();
        $contact = $adminUsers->getAdmin($settings->get('notification_contact'));

        $this->mailer->addAddress($contact->email, $contact->firstname . ' ' . $contact->lastname);
    }
}
?>

And then add a `send()` method to this class, append Body and you're good to go. 

It beats my why this is not available from the start. 

Edited by kUdtiHaEX
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use & Guidelines and understand your posts will initially be pre-moderated