SchumacherFM / mailout

mailout - a https://caddyserver.com/ V1 SMTP client email middleware with PGP encryption
https://caddyserver.com/docs
Apache License 2.0
78 stars 13 forks source link

405: Method Not Allowed #3

Closed cmelone closed 8 years ago

cmelone commented 8 years ago

I just downloaded the caddy binary with mailout included, here is my Caddyfile

localhost:3000
gzip
root /User/caetano/Desktop/ape

mailout /contact {
    maillog         mail.log
    errorlog        error.log

    to              xxx@gmail.com
    subject         "Email from {{.firstname}} {{.lastname}}"
    body            template.txt

    username        "xxx@gmail.com"
    password        "xxx"
    host            "smtp.gmail.com"
    port             587

}

template.txt:

Hello,

please find below a new contact:

Name            {{.Form.Get "name"}}
Email           {{.Form.Get "email"}}

Message:
{{.Form.Get "message"}}

User Agent: {{.Form.Get "user_agent"}}

And contact/index.html:

<div id="contactThankYou" style="display:hidden;">Thank you for contacting us!</div>
<form action="#" id="myContactForm" method="POST">
  <div class="row uniform 50%">
    <div class="6u 12u$(xsmall)">
      <input type="text" name="name" id="name"
             placeholder="{{ .Site.Params.contact.form.name }}" required/>
    </div>
    <div class="6u$ 12u$(xsmall)">
      <input type="email" name="email" id="email"
             placeholder="{{ .Site.Params.contact.form.email }}" required/>
    </div>
    <div class="12u$">
      <textarea name="message" id="message" placeholder="{{ .Site.Params.contact.form.message }}"
                rows="4" required></textarea>
    </div>
    <input type="hidden" name="user_agent" value="Will be filled out via JavaScript"/>
    <ul class="actions">
      <li><input type="submit" value="{{ .Site.Params.contact.form.submit }}"/></li>
    </ul>
  </div>
</form>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script>

<script>
$(document).ready(function() {

    $('#myContactForm').submit(function(event) {

        $.ajax({
            type        : 'POST',
            url         : 'http://localhost:3000/contact',
            data        : $('#myContactForm').serialize(),
            dataType    : 'json',
            encode      : true
        })
        .done(function(data) {
            console.log(data);
            $('#contactThankYou').show();
            $('#myContactForm').hide();

        })
         .fail(function() {
            alert( "error" );
         });

        event.preventDefault();
    });

});
</script>

But when I navigate to http://localhost:3000/contact, i get {"code":405,"error":"Method Not Allowed"}

SchumacherFM commented 8 years ago

You must do a post request to http://localhost:3000/contact because it is only a REST endpoint without any hard disk interaction.

You can find a working example on my homepage.

cmelone commented 8 years ago

I just copied and pasted this code from the repositories README. Which part of the code is causing the issue, because isnt

        $.ajax({
            type        : 'POST',
            url         : 'http://localhost:3000/contact',
            data        : $('#myContactForm').serialize(),
            dataType    : 'json',
            encode      : true
        })

already posting it to http://localhost:3000/contact?

SchumacherFM commented 8 years ago

Yes it is posting to /contact but you cannot open /contact by navigating to it with your browser.

Place the HTML form and the script somewhere else on your website and let the JS do the POST to the /contact route.