Send Email function in Controller

Hi,
I am trying to write a function in my Controller to send emails through my application in nextcloud. Here is the SendUserMail function that is being used to send emails :

public function sendUserMail($toUserEmail, $organizations, $toDisplayName) {


    if (!$toUserEmail || !filter_var($toUserEmail, FILTER_VALIDATE_EMAIL)) {
            throw new Exception('Invalid email address (' . $toUserEmail . ')');
    }
   
    $emailTemplate = $this->mailer->createEMailTemplate('test@gmail.com', [
                            'owner' => 'tester',
                            'title' => 'Assign Value',
                            'link' => 'http://google.com'
                    ]);

   $emailTemplate->setSubject('App - New Activity');
                    $emailTemplate->addHeader();
                    $emailTemplate->addHeading('App - New Activity', false);
                    $emailTemplate->addBodyText('Your details have been updated');
                    $emailTemplate->addBodyText($variable);
                    $emailTemplate->addBodyText('Updated results');

   $emailTemplate->addFooter('This email is sent to you on behalf of the application. If you want to get removed from this app, contact the site administrator);

    try {
            $message = $this->mailer->createMessage();
            $message->setTo([$toUserEmail => $toDisplayName]);
            $message->useTemplate($emailTemplate);
            $this->mailer->send($message);

            return new Dataresponse(1);

    } catch (\Exception $e) {
            $this->logger->logException($e);
            throw $e;
    }

}

The function is written directly in controller to check but is giving a

[HTTP/1.1 405 Method Not Allowed 125ms]

error when being called from the vue js frontend using axios.

I am pretty new to app development so if anyone can guide me how can I make a function to send out emails through my app, I will be really grateful. Thank you

Still looking for an answer, if someone can guide me through. Thank you

the controller you are calling seems to not have the method (GET, POST, … ) you are using.
Maybe you can share a link to your repository, so someone can assist you.

Thank you for the reply, I will update the git and post a link here so you can check in like 3 - 5 minutes

@nickvergessen… here is the repository address… I have simplified files so only the send Mail Controller and function is there for better understanding. Thank you

Response Header is :
When I call the function through a controller or vice versa in both cases…

XHRPOST  https://site.com/index.php/apps/organization/eusers/sendUserMail
[HTTP/1.1 405 Method Not Allowed 113ms]

	
POST
	
scheme
	https
host
	dev3.nextcloud.veelix-it-solutions.com
filename
	/index.php/apps/organization/eusers/sendUserMail
Address
	18.184.66.60:443
Status405
Method Not Allowed
VersionHTTP/1.1
Transferred1.81 KB (0 B size)

    	
    HTTP/1.1 405 Method Not Allowed

    Date: Mon, 17 Aug 2020 12:25:27 GMT

    Server: Apache/2.4.29 (Ubuntu)

    Expires: Thu, 19 Nov 1981 08:52:00 GMT

    Cache-Control: no-store, no-cache, must-revalidate

    Pragma: no-cache

    Content-Security-Policy: default-src 'self'; script-src 'self' 'nonce-R05vcks3SU9xczI4RVpHNnRaU25zemdUbVU0QWRSTCtkc0taeTlpc2grZz06ZHEwYUd1dE0zLzZLVitMNzRLemszQXdrckFwb09XYVpCNHZkb2EzL3pxOD0='; style-src 'self' 'unsafe-inline'; frame-src *; img-src * data: blob:; font-src 'self' data:; media-src *; connect-src *; object-src 'none'; base-uri 'self';

    Referrer-Policy: no-referrer

    X-Content-Type-Options: nosniff

    X-Download-Options: noopen

    X-Frame-Options: SAMEORIGIN

    X-Permitted-Cross-Domain-Policies: none

    X-Robots-Tag: none

    X-XSS-Protection: 1; mode=block

    Content-Length: 0

    Keep-Alive: timeout=5, max=84

    Connection: Keep-Alive

    Content-Type: text/html; charset=UTF-8
    	
    Accept
    	application/json, text/plain, */*
    Accept-Encoding
    	gzip, deflate, br
    Accept-Language
    	en-US,en;q=0.5
    Connection
    	keep-alive
    Content-Length
    	2
    Content-Type
    	application/json;charset=utf-8
    Cookie
    	ocuprigmi569=3cpll44d7rjoe6s0c2rtkp044n; oc_sessionPassphrase=2um4jSRlX8RW6%2F6Uham6EL7VMsg%2F7gTVtx39pEXaP8qr7%2FAPMIudEmmFBhttg9wGnGwpmfRlG2LKOji8FkyEeXNoYY6L3cJo57EJmyuM%2B0pDsxcmDFJaltZq0WBVf108; __Host-nc_sameSiteCookielax=true; __Host-nc_sameSiteCookiestrict=true; nc_username=RichardH; nc_token=Su0p2iUx8Hq9vgU8t0p1EZXEMCCzuj6H; nc_session_id=3cpll44d7rjoe6s0c2rtkp044n
    Host
    	Host.com
    Origin
    	https://origin.com
    requesttoken
    	fGmCVNlum2mUJD4OHY1dNdIP7ddWoWMqSJ6rSwV90u4=:Eh6zZYAs7lqiYk1PSLUeWuY42JM+7RdNOdfvIXAum6k=
    User-Agent
    	Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:79.0) Gecko/20100101 Firefox/79.0

Can’t find any

@nickvergessen. Sorry, here it is :

https://bitbucket.org/rajahadi/nextcloud/src/master/

I also tried calling the function from a single controller directly… Have updated the response headers in a post above.

Seems to be not accessible?

Yes, do not know why but making a new controller with the same file just changing the controller name worked out.