登录
首页 >  Golang >  Go问答

如何使用 Golang Link A Contaxt Form 发送电子邮件

来源:stackoverflow

时间:2024-04-15 11:06:35 276浏览 收藏

目前golang学习网上已经有很多关于Golang的文章了,自己在初次阅读这些文章中,也见识到了很多学习思路;那么本文《如何使用 Golang Link A Contaxt Form 发送电子邮件》,也希望能帮助到大家,如果阅读完后真的对你学习Golang有帮助,欢迎动动手指,评论留言并分享~

问题内容

我尝试将 golang 服务器链接到我的 html 联系表单,这是我的文件夹结构下图是文件夹结构..以下是 server.go 文件:

package main

import (
    "html/template"
    "net/http"
)

type contactdetails struct {
    fname string
    email   string
    telephone string
    subject string
    message string
}

func main() {
    tmpl := template.must(template.parsefiles("contact.html"))

    http.handlefunc("/", func(w http.responsewriter, r *http.request) {
        if r.method != http.methodpost {
            tmpl.execute(w, nil)
            return
        }

        details := contactdetails{
            fname:  r.formvalue("fname"),
            email:   r.formvalue("email"),
            telephone: r.formvalue("telephone"),
            subject: r.formvalue("subject"),
            message: r.formvalue("message"),
        }

        // do something with details
        _ = details

        tmpl.execute(w, struct{ success bool }{true})
    })

    http.listenandserve(":8080", nil)
}

以下是 contact.html 代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <!-- MDB icon -->
    <link rel="icon" href="img/sacheal logo.ico" type="image/x-icon">
    <!-- Font Awesome -->
    <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.11.2/css/all.css">
    <!-- Google Fonts Roboto -->
    <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700&display=swap">
    <!-- Bootstrap core CSS -->
    <link rel="stylesheet" href="css/bootstrap.min.css">
    <!-- Material Design Bootstrap -->
    <link rel="stylesheet" href="css/mdb.min.css">
    <!-- Your custom styles (optional) -->
    <link rel="stylesheet" href="css/style.css">
    <title>Sacheal Consult | Contact Us</title>
</head>
<body>
    <header> 
     
        <nav class="navbar fixed-top navbar-expand-lg navbar-dark scrolling-navbar">
          <a class="navbar-brand" href="#"><img src="./img/sacheal logo.png" height="120px" width="120px" alt="logo"></a>
          <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
            <span class="navbar-toggler-icon"></span>
          </button>
          <div class="collapse navbar-collapse" id="navbarSupportedContent">
            <ul class="navbar-nav mr-auto font-weight-bold">
              <li class="nav-item">
                <a class="nav-link" href="./index.html">Home</a>
              </li>
              <li class="nav-item">
                <a class="nav-link" href="./about.html">About</a>
              </li>
              <li class="nav-item">
                <a class="nav-link" href="./Service.html">Services</span></a>
              </li>
              <li class="nav-item active">
                <a class="nav-link" href="./contact.html">Contact <span class="sr-only">(current)</a>
              </li>
            </ul>
          </div>
        </nav>
        
      
        <div class="view view-cont intro-2">
          <div class="full-bg-img">
            <div class="mask rgba-green-light flex-center">
              <div class="container text-center white-text wow fadeInUp">
                <div class=""></div>
                    <h2 class="font-weight-bold text-uppercase">Contact Us</h2>
    
                    <h5 class="text-white font-weight-bold" align="center">You can contact us on </h5>
                    
                   
                
              </div>
            </div>
          </div>
        </div>
      
    </header>

    <main>
        <div class="container-fluid my-5 py-5 z-depth-1">


            <!--Section: Content-->
            <section class="px-md-5 mx-md-5 text-center text-lg-left dark-grey-text">
          
              <style>
                .map-container {
                  height: 200px;
                  position: relative;
                }
          
                .map-container iframe {
                  left: 0;
                  top: 0;
                  height: 100%;
                  width: 100%;
                  position: absolute;
                }
              </style>
          
              <!--Grid row-->
              <div class="row d-flex justify-content-center mb-4">
          
                <!--Grid column-->
                <div class="col-md-6 text-center">
                  <form action="">
                  
                      <!-- Material outline input -->
                      <div class="md-form md-outline mt-3">
                        &lt;input type=&quot;name&quot; id=&quot;form-name&quot; class=&quot;form-control&quot;&gt;
                        <label for="form-name">Full Name</label>
                      </div>
                      <div class="md-form md-outline mt-3">
                        &lt;input type=&quot;email&quot; id=&quot;form-email&quot; class=&quot;form-control&quot;&gt;
                        <label for="form-email">E-mail</label>
                      </div>
                      
                      <div class="md-form md-outline mt-3">
                        &lt;input type=&quot;telephone&quot; id=&quot;tel&quot; class=&quot;form-control&quot;&gt;
                        <label for="form-email">Telephone</label>
                      </div>
                      <!-- Material outline input -->
                      <div class="md-form md-outline">
                        &lt;input type=&quot;text&quot; id=&quot;form-subject&quot; class=&quot;form-control&quot;&gt;
                        <label for="form-subject">Subject</label>
                      </div>
              
                      <!--Material textarea-->
                      <div class="md-form md-outline mb-3">
                        &lt;textarea id=&quot;form-message&quot; class=&quot;md-textarea form-control&quot; rows=&quot;5&quot;&gt;&lt;/textarea&gt;
                        <label for="form-message">Message</label>
                      </div>
              
                      <button type="submit" class="btn btn-green btn-sm ml-0">Submit<i class="far fa-paper-plane ml-2"></i></button>
                  </form>
                </div>
                <!--Grid column-->
          
              </div>
              <!--Grid row-->
          
              <!--Google map-->
              <div id="map-container-google-1" class="z-depth-1 map-container">
                  <iframe src="https://www.google.com/maps/embed?pb=!1m18!1m12!1m3!1d31765.215550392822!2d-0.2790558016341382!3d5.618302097542889!2m3!1f0!2f0!3f0!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x0%3A0xa281a924604df2f9!2sMambo%20Spot!5e0!3m2!1sen!2sgh!4v1602553528014!5m2!1sen!2sgh" frameborder="0"
                    style="border:0" allowfullscreen></iframe>
                </div>
                <!--Google Maps-->
          
          
            </section>
            <!--Section: Content-->
          
          
        </div>
    </main>
    

    <footer class="page-footer font-small success-color success-color-dark">

        <!-- Footer Elements -->
        <div class="container">
      
          <!-- Grid row-->
          <div class="row">
      
            <!-- Grid column -->
            <div class="col-md-12 py-5">
              <div class="mb-5 flex-center">
      
                <!-- Facebook -->
                <a class="fb-ic">
                  <i class="fab fa-facebook-f fa-lg white-text mr-md-5 mr-3 fa-2x"> </i>
                </a>
                <!-- Twitter -->
                <a class="tw-ic">
                  <i class="fab fa-twitter fa-lg white-text mr-md-5 mr-3 fa-2x"> </i>
                </a>
                <!-- Google +-->
                <a class="gplus-ic">
                  <i class="fab fa-google-plus-g fa-lg white-text mr-md-5 mr-3 fa-2x"> </i>
                </a>
                <!--Linkedin -->
                <a class="li-ic">
                  <i class="fab fa-linkedin-in fa-lg white-text mr-md-5 mr-3 fa-2x"> </i>
                </a>
                <!--Instagram-->
                <a class="ins-ic">
                  <i class="fab fa-instagram fa-lg white-text mr-md-5 mr-3 fa-2x"> </i>
                </a>
                <!--Pinterest-->
                <a class="pin-ic">
                  <i class="fab fa-pinterest fa-lg white-text fa-2x"> </i>
                </a>
              </div>
            </div>
            <!-- Grid column -->
      
          </div>
          <!-- Grid row-->
      
        </div>
        <!-- Footer Elements -->
      
        <!-- Copyright -->
        <div class="footer-copyright text-center py-3">© 2020 Copyright:
          <a href="https://mdbootstrap.com/"> MDBootstrap.com</a>
        </div>
        <!-- Copyright -->
      
      </footer>
      <!-- Footer -->
    <!-- jQuery -->
    <script type="text/javascript" src="js/jquery.min.js"></script>
    <!-- Bootstrap tooltips -->
    <script type="text/javascript" src="js/popper.min.js"></script>
    <!-- Bootstrap core JavaScript -->
    <script type="text/javascript" src="js/bootstrap.min.js"></script>
    <!-- MDB core JavaScript -->
    <script type="text/javascript" src="js/mdb.min.js"></script>
    <!-- Your custom scripts (optional) -->
    <script type="text/javascript">
        
    </script>
</body>
</html>

该程序似乎无法运行。我需要帮助才能完成这项工作。如果有办法可以链接文件或重组文件夹。


解决方案


在 html 中,您有:

&lt;input type=&quot;name&quot; id=&quot;form-name&quot; class=&quot;form-control&quot;&gt;

在 go 代码中,您有:

fname:  r.formvalue("fname"),

传递给r.formvalue的参数需要与输入标签中的“名称”匹配:

&lt;input type=&quot;name&quot; id=&quot;form-name&quot; class=&quot;form-control&quot; name=&quot;fName&quot;&gt;

您正在使用不存在的表单值填充结构。完整的示例可以在这里找到:https://astaxie.gitbooks.io/build-web-application-with-golang/content/en/04.1.html

今天关于《如何使用 Golang Link A Contaxt Form 发送电子邮件》的内容介绍就到此结束,如果有什么疑问或者建议,可以在golang学习网公众号下多多回复交流;文中若有不正之处,也希望回复留言以告知!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>