<p>You should checkout <a href="https://craftcms.com/docs/4.x/extend/controllers.html#actions" rel="nofollow noreferrer">this article</a> on Craft Docs about Controller actions. Inside your form you can put <code>{{ actionInput('my-plugin/widgets/save') }}</code> instead of using the action attribute on a form.</p>
<p>Can you share your controller code?</p>

CC BY-SA 4.0
2023-08-10T14:07:44.107Z
I will do that, but I will change my form as well because I've made some changes..
Stef Verniers
CC BY-SA 4.0
2023-08-10T19:39:44.473Z
For POST requests, this is the recommended way of ensuring your form data arrives at the desired controller. It will work regardless of what vanity URL rules are set up in the project!
August Miller
CC BY-SA 4.0
2023-08-08T14:30:51.617Z
2023-08-08T14:30:51.617Z
Kenneth
<p>I'm currently working on a school project for my finals using craft cms.</p>
<p>One of the targets I have to achieve are simply sending emails when someone fills in his email in an input mail and submits that form.</p>
<p>However, I've been looking for a way to this for two days and I just can't seem to figure it out how this whole email sending thing works.</p>
<p>At the moment I have a twig template for my homepage, a custom module for the mailing and a route to make it work. However everytime I fill out the form and submit, my route can't be founded whilst I've registered it..
I think it's because I don't get how craft works exactly.</p>
<p>My Email Template:</p>
<pre><code>&lt;form class=&quot;cta__mail__input&quot; method=&quot;POST&quot; id=&quot;emailForm&quot; action=&quot;{{ url('subscribe') }}&quot;&gt;
    {{ csrfInput() }}
    &lt;input type=&quot;text&quot; placeholder=&quot;Email&quot; name=&quot;email&quot; id=&quot;emailInput&quot;&gt;
    &lt;button type=&quot;submit&quot; id=&quot;submitButton&quot;&gt;Confirm &lt;/button&gt;
&lt;/form&gt;
</code></pre>
<p>I've read so many topics but it's all unclear so if anyone knows how to start, or has a link to a tutorial. Please let me know..</p>
<p><strong>UPDATE</strong></p>
<p>The code above me was replaced and that one works now, but I'm stuck with a same situation and whatever I try, I can't seem to fix it..</p>
<p>Whenever a user clicks on the button, the action method from the custom plugins controller should be called, but I always get a 404, page not found caused by invalid route. The weird fact is that this used to work for a long time and no it just gave up..</p>
<p>When I published the module to my production server, I also get the &quot;page not found&quot;.
Here's the code:</p>
<p>The form that is included on another template:</p>
<pre><code>&lt;form class=&quot;inquiry&quot; method=&quot;post&quot; accept-charset=&quot;UTF-8&quot; action=&quot;{{ url('tester/inquiry') }}&quot;&gt;
    {{ csrfInput() }}
    &lt;input type=&quot;hidden&quot; name=&quot;product_name&quot; value=&quot;{{ entry.title }}&quot;&gt;
    &lt;button type=&quot;submit&quot; id=&quot;submitButton&quot;&gt;{{ entry.productDetailInquiryButton }}&lt;/button&gt;
    &lt;a href=&quot;{{ url('tester/inquiry') }}&quot;&gt;testing&lt;/a&gt;

&lt;/form&gt;
</code></pre>
<p>The Module.php. I can confirm the module is loaded bc of the dd.</p>
<pre><code>&lt;?php 
namespace modules\tester;
use craft\base\Event;
use craft\events\RegisterUrlRulesEvent;
use craft\web\UrlManager;

class Module extends \yii\base\Module
{
    public function init()
    {
        parent::init();
        // dd('this module is loaded');

        Event::on(
            UrlManager::class,
            UrlManager::EVENT_REGISTER_SITE_URL_RULES,
            function(RegisterUrlRulesEvent $event) {
                $event-&gt;rules['tester/inquiry'] = 'tester/inquiry/send';
            }
        );
    }

}
</code></pre>
<p>The controller</p>
<pre><code>&lt;?php 

namespace modules\tester\controllers;

use Craft;
use craft\web\Controller;

class InquiryController extends Controller 
{

    protected bool|array $allowAnonymous = true;

    public function actionSend() {
        dd('test');
    }

}
</code></pre>
<p>composer.json</p>
<pre><code>  &quot;autoload&quot;: {
    &quot;psr-4&quot;: {
      &quot;modules\\&quot;: &quot;modules/&quot;,
      &quot;subscribe\\&quot;: &quot;modules/subscribe/&quot;,
      &quot;tester\\&quot;: &quot;modules/tester/&quot;
    }
  },
</code></pre>
<p><a href="https://i.sstatic.net/kGXtM.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/kGXtM.png" alt="Folder structure" /></a></p>
<p>I've also tried making the C for the controllers folder a capital letter but it had no effect.</p>

CC BY-SA 4.0
2023-07-17T18:06:47.273Z
What happens when the form is POSTed to the `/subscribe` URL? Is it just a Twig template (i.e. `templates/subscribe.twig`)?
Mats Mikkel Rummelhoff
CC BY-SA 4.0
2023-07-17T19:52:12.070Z
@MatsMikkelRummelhoff well in my logic when I submit that form the function in a controller should be called so it will send an email to the one who subscribed and to my that someone did subscribee. However that controller code is not given because I just can't find the right example.. :/
Stef Verniers
CC BY-SA 4.0
2023-08-10T19:36:27.297Z
Are you bootstrapping your module in `app.php`? It may not have an automatic ID of `tester`. Check out [this section](https://craftcms.com/docs/4.x/extend/module-guide.html#update-the-application-config) in the Modules documentation.
August Miller
<emails><craft4>
Sending emails with Craft 4 with custom Module
# Sending emails with Craft 4 with custom Module

(Asked by: username_0 on 2023-07-17)

I'm currently working on a school project for my finals using craft cms.
One of the targets I have to achieve are simply sending emails when someone fills in his email in an input mail and submits that form.
However, I've been looking for a way to this for two days and I just can't seem to figure it out how this whole email sending thing works.
At the moment I have a twig template for my homepage, a custom module for the mailing and a route to make it work. However everytime I fill out the form and submit, my route can't be founded whilst I've registered it..
I think it's because I don't get how craft works exactly.
My Email Template:

    {{ csrfInput() }}
    
    Confirm 


I've read so many topics but it's all unclear so if anyone knows how to start, or has a link to a tutorial. Please let me know..
UPDATE
The code above me was replaced and that one works now, but I'm stuck with a same situation and whatever I try, I can't seem to fix it..
Whenever a user clicks on the button, the action method from the custom plugins controller should be called, but I always get a 404, page not found caused by invalid route. The weird fact is that this used to work for a long time and no it just gave up..
When I published the module to my production server, I also get the "page not found".
Here's the code:
The form that is included on another template:

    {{ csrfInput() }}
    
    {{ entry.productDetailInquiryButton }}
    testing



The Module.php. I can confirm the module is loaded bc of the dd.
rules['tester/inquiry'] = 'tester/inquiry/send';
            }
        );
    }

}

The controller

composer.json
  "autoload": {
    "psr-4": {
      "modules\\": "modules/",
      "subscribe\\": "modules/subscribe/",
      "tester\\": "modules/tester/"
    }
  },


I've also tried making the C for the controllers folder a capital letter but it had no effect.

**Tags:** emails, craft4

> **Comments:**
> **username_1**: What happens when the form is POSTed to the `/subscribe` URL? Is it just a Twig template (i.e. `templates/subscribe.twig`)?  
> **username_0**: @MatsMikkelRummelhoff well in my logic when I submit that form the function in a controller should be called so it will send an email to the one who subscribed and to my that someone did subscribee. However that controller code is not given because I just can't find the right example.. :/  
> **username_2**: Are you bootstrapping your module in `app.php`? It may not have an automatic ID of `tester`. Check out [this section](https://craftcms.com/docs/4.x/extend/module-guide.html#update-the-application-config) in the Modules documentation.  

---

## 1 answers

### Answer by username_3 on 2023-08-08. Score: 2

You should checkout [this article](https://craftcms.com/docs/4.x/extend/controllers.html#actions) on Craft Docs about Controller actions. Inside your form you can put `{{ actionInput('my-plugin/widgets/save') }}` instead of using the action attribute on a form.

Can you share your controller code?

> **Comments:**
> **username_0**: I will do that, but I will change my form as well because I've made some changes..  
> **username_2**: For POST requests, this is the recommended way of ensuring your form data arrives at the desired controller. It will work regardless of what vanity URL rules are set up in the project!  

---

