I'm looking to call the following Module function from a front end form. I've looked through numerous answers but I can't seem to find a definitive answer on this.
Here's my Module's code, but obviously I don't want this to run every time and entry get's saved, but when the form on the front-end calls it.
<?php
namespace modules;
use Craft;
use yii\base\Module;
use yii\base\Event;
use craft\services\Elements;
class ShifterModule extends Module
{
public function init()
{
parent::init();
Event::on(
Elements::class,
Elements::EVENT_AFTER_SAVE_ELEMENT,
function (ElementEvent $event){
// Get the correct Entry that's currently saving
if ($event->element instanceof craft\elements\Entry) {
$entry = $event->element;
// Set $field to the Field Value
$field = $entry->getFieldValue('proposalStatus');
// Set the Proposals > Campaign Authors Email
$owner = $entry->getFieldValue('proposalCampaign')->first()->getAuthor()->emailAddress;
if($field == 'delivered'){
return Craft::$app
->getMailer()
->compose()
->setFrom('test@test.com')
->setTo($owner)
->setSubject('Proposal Delivered!')
->send();
}
}
} // Close Function
); // Close Event
} // Close Public Function
} // Close Module Off