【问题标题】:Why emailjs is not sending me emailjs? in HTML? How can i send email with emailjs in HTML?为什么 emailjs 不向我发送 emailjs?在 HTML 中?如何使用 HTML 中的 emailjs 发送电子邮件?
【发布时间】:2022-11-18 11:39:06
【问题描述】:

为什么 emailjs 不向我发送 emailjs?在 HTML 中?如何使用 HTML 中的 emailjs 发送电子邮件?早些时候我使用 Reactjs 通过 emailjs 发送电子邮件并且一切都很好但是当我尝试在我的 html css JavaScript 项目中这样做时它甚至没有工作任何人都可以帮助我如何发送电子邮件以为 html 中的 emailjs 你可以看到并检查我​​的我遵循 emailjs 文档的代码我只想在他订阅时接收用户电子邮件

 <div class="main-content container">
        <div class="banner-content">
            <h1>Great sofware is built with amazing developers</h1>
            <p>We help build and manage a team of world-class developers to bring your vision to life</p>
            <div class="input-subscribe">
                <input type="email" id="email" placeholder="Subscribe newsletter"  onclick="sendMail()">
                <button class="btn-subscribe btn-item">
                    Subscribe
                </button>
            </div>
            <div class="logo-sponsor">
                <p>Sponsored by:</p>
                <img src="images/paypal.png" alt="paypal-logo">
                <img src="images/google.png" alt="google-logo">
                <img src="images/dropbox.png" alt="dropbox-logo">
            </div>
        </div>

js代码:

  <script type="text/javascript" src="https://cdn.jsdelivr.net/npm/@emailjs/browser@3/dist/email.min.js"></script>
    <script type="text/javascript">
        (function () {
            emailjs.init('HXOYlFkZOUDFI1oCe');
        })();
    </script>
    <script type="text/javascript">
        window.onload = function () {
            document.getElementById('input-subscribe').addEventListener('submit', function (event) {
                event.preventDefault();
                // generate a five digit number for the contact_number variable
                this.contact_number.value = Math.random() * 100000 | 0;
                // these IDs from the previous steps
                emailjs.sendForm('contact_service', 'contact_form', this)
                    .then(function () {
                        console.log('SUCCESS!');
                    }, function (error) {
                        console.log('FAILED...', error);
                    });
            });
        }

        function sendMail() {
            var params = {
                name: document.getElementById("name").value,
                email: document.getElementById("email").value,
                message: document.getElementById("message").value,
            };

            const serviceID = "service_qfcqnmh";
            const templateID = "template_jeyyx8j";

            emailjs.send(serviceID, templateID, params)
                .then(res => {
                    document.getElementById("name").value = "";
                    document.getElementById("email").value = "";
                    document.getElementById("message").value = "";
                    console.log(res);
                    alert("Your message sent successfully!!")

                })
                .catch(err => console.log(err));

        }
    </script>

【问题讨论】:

    标签: javascript html email


    【解决方案1】:

    您正在使用 document.getElementById() 选择类为 input-subscribe&lt;div&gt;div没有id input-subscribe,所以你需要把class改成id = "input-subscribe"

    div.input-subscribe 中的&lt;button&gt; 不能用作提交按钮,因此您需要将&lt;div&gt; 更改为form 或添加按钮type = "submit"。没有这个,提交事件永远不会发生,因为提交按钮的默认行为仅在表单内有效。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-10-18
      • 2021-04-02
      • 2023-02-21
      • 2020-11-25
      • 1970-01-01
      • 2016-11-28
      • 2022-11-13
      • 2022-12-07
      相关资源
      最近更新 更多