Page 1 of 1

Sending emails programmatically

Posted: Tue Aug 24, 2021 2:50 pm
by markS
I have a module which used the CMSMailer module to send emails when it wasn't built-in to the cms. I'd like to upgrade my module to use the built-in mailer functionality rather than needing to load a deprecated module.
As it's still just a wrapper around phpmailer, I am thinking that it's probably just a question of initialising differently and then I can reuse most of the existing code?

I have had a search around and can't see any example code or notes on this, can anyone point me in the right direction or at a module which has updated in this way?

Thanks,

Mark.

Re: Sending emails programmatically

Posted: Tue Aug 24, 2021 3:43 pm
by DIGI3
I'm not positive, but I think CGBetterForms (and now its fork, SmartForms) does this.

[Solved] Re: Sending emails programmatically

Posted: Tue Aug 24, 2021 4:26 pm
by markS
Thank you, that's incredibly helpful! I reviewed the SmartForms module and found the answer.

For future reference, it appears that you simply need to replace the call to CMSMailer with the following:

Code: Select all

$mailer = new \cms_mailer;
Then you can carry on as before. A quick tested example:

Code: Select all

$mailer = new \cms_mailer;
$mailer->AddAddress("test@example.com");
$mailer->SetSubject("This is a test email");
$mailer->SetFrom("test@example.com");
$mailer->SetBody("This is a test email.");
$mailer->IsHTML(false);
$mailer->Send();
Thanks once again.