繁体   English   中英

为什么 emailjs 不向我发送 emailjs? 在 HTML? 如何在 HTML 中使用 emailjs 发送 email?

[英]Why emailjs is not sending me emailjs? in HTML? How can i send email with emailjs in HTML?

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

 <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>

您正在使用document.getElementById()选择带有 class input-subscribe<div> div没有 id input-subscribe ,因此您需要将 class 更改为id = "input-subscribe"

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM