kUdtiHaEX Posted March 11, 2020 Share Posted March 11, 2020 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? 0 Quote Link to comment Share on other sites More sharing options...
Kian Posted March 12, 2020 Share Posted March 12, 2020 Load you template, replace variables (e.g. Dear {admin_name} becomes Dear Sara) and put it in custommessage API parameter. 0 Quote Link to comment Share on other sites More sharing options...
kUdtiHaEX Posted March 12, 2020 Author Share Posted March 12, 2020 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? 0 Quote Link to comment Share on other sites More sharing options...
kUdtiHaEX Posted March 13, 2020 Author Share Posted March 13, 2020 (edited) 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 March 13, 2020 by kUdtiHaEX 0 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.