我希望我的网站有能力发送电子邮件而不刷新页面。我想用Javascript。

<form action="javascript:sendMail();" name="pmForm" id="pmForm" method="post">
Enter Friend's Email:
<input name="pmSubject" id="pmSubject" type="text" maxlength="64" style="width:98%;" />
<input name="pmSubmit" type="submit" value="Invite" />

这是我想调用的函数,但我不确定把什么放入javascript函数。从我所做的研究中,我发现了一个使用mailto方法的示例,但我的理解是,它实际上并不直接从站点发送。

所以我的问题是,我可以在哪里找到什么放入JavaScript函数直接从网站发送电子邮件。

function sendMail() {
    /* ...code here...    */
}

当前回答

因为这些都是很棒的信息,有一个叫做Mandrill的小api可以从javascript发送邮件,它工作得很完美。你可以试试。这里有一个开始的小教程。

其他回答

在sendMail()函数中,将ajax调用添加到后端,您可以在服务器端实现此功能。

使用JavaScript或jQuery发送邮件

var ConvertedFileStream;
var g_recipient;
var g_subject;
var g_body;
var g_attachmentname;


function SendMailItem(p_recipient, p_subject, p_body, p_file, p_attachmentname, progressSymbol) {

    // Email address of the recipient 
    g_recipient = p_recipient;

   // Subject line of an email
    g_subject = p_subject;

   // Body description of an email
    g_body = p_body;

    // attachments of an email
    g_attachmentname = p_attachmentname;

    SendC360Email(g_recipient, g_subject, g_body, g_attachmentname);

}

function SendC360Email(g_recipient, g_subject, g_body, g_attachmentname) {
    var flag = confirm('Would you like continue with email');
    if (flag == true) {

        try {
            //p_file = g_attachmentname;
            //var FileExtension = p_file.substring(p_file.lastIndexOf(".") + 1);
           // FileExtension = FileExtension.toUpperCase();
            //alert(FileExtension);
            SendMailHere = true;

            //if (FileExtension != "PDF") {

            //    if (confirm('Convert to PDF?')) {
            //        SendMailHere = false;                    
            //    }

            //}
            if (SendMailHere) {
                var objO = new ActiveXObject('Outlook.Application');

                var objNS = objO.GetNameSpace('MAPI');

                var mItm = objO.CreateItem(0);

                if (g_recipient.length > 0) {
                    mItm.To = g_recipient;
                }

                mItm.Subject = g_subject;

                // if there is only one attachment                 
                // p_file = g_attachmentname;
                // mAts.add(p_file, 1, g_body.length + 1, g_attachmentname);

                // If there are multiple attachment files
                //Split the  files names
                var arrFileName = g_attachmentname.split(";");
                 // alert(g_attachmentname);
                //alert(arrFileName.length);
                var mAts = mItm.Attachments;

                for (var i = 0; i < arrFileName.length; i++)
                {
                    //alert(arrFileName[i]);
                    p_file = arrFileName[i];
                    if (p_file.length > 0)
                    {                     
                        //mAts.add(p_file, 1, g_body.length + 1, g_attachmentname);
                        mAts.add(p_file, i, g_body.length + 1, p_file);

                    }
                }

                mItm.Display();

                mItm.Body = g_body;

                mItm.GetInspector.WindowState = 2;

            }
            //hideProgressDiv();

        } catch (e) {
            //debugger;
            //hideProgressDiv();
            alert('Unable to send email.  Please check the following: \n' +
                    '1. Microsoft Outlook is installed.\n' +
                    '2. In IE the SharePoint Site is trusted.\n' +
                    '3. In IE the setting for Initialize and Script ActiveX controls not marked as safe is Enabled in the Trusted zone.');
        }
    }
  }

你的问题没有一个直接的答案,因为我们不能只使用javascript发送电子邮件,但有一些方法可以使用javascript为我们发送电子邮件:

1)使用API并通过javascript调用API为我们发送电子邮件,例如https://www.emailjs.com说你可以在一些设置后使用下面的代码来调用他们的API:

var service_id = 'my_mandrill';
var template_id = 'feedback';
var template_params = {
name: 'John',
reply_email: 'john@doe.com',
message: 'This is awesome!'
};

emailjs.send(service_id,template_id,template_params);

2)创建一个后端代码来为你发送电子邮件,你可以使用任何后端框架来为你做这件事。

3)使用像这样的东西:

window.open('mailto:me@http://stackoverflow.com/');

这将打开你的电子邮件应用程序,这可能会进入阻止弹出在你的浏览器。

一般来说,发送电子邮件是一个服务器任务,所以应该在后端语言中完成,但我们可以使用javascript来收集所需的数据,并将其发送到服务器或api,我们也可以使用第三方应用程序,并通过浏览器使用javascript打开它们。

完整的反垃圾邮件版本:

<div class="at">info<i class="fa fa-at"></i>google.com</div>
OR
<div class="at">info&#x40;google.com</div>


<style>
.at {
  color: blue;
  cursor: pointer;
}
.at:hover {
  color: red;
}
</style>

<script>
const el33 = document.querySelector(".at");
el33.onclick = () => {
  let recipient="info";
  let at = String.fromCharCode(64);
  let dotcom="google.com";
  let mail="mailto:";
  window.open(mail+recipient+at+dotcom);
}
</script>

我知道我写这个问题的答案已经太迟了,但我认为这将用于任何想通过javascript发送电子邮件的人。

我建议的第一种方法是在服务器上使用回调来完成此操作。如果你真的想用javascript处理它,下面是我的建议。

我发现最简单的方法是使用smtpJs。一个可以用来发送电子邮件的免费图书馆。

1.包括如下所示的脚本

<script src="https://smtpjs.com/v3/smtp.js"></script>

2. 你可以发一封这样的邮件

Email.send ({ 主持人:“smtp.yourisp.com”, 用户名:" Username ", 密码:Password, 致:“them@website.com” 来自:“you@isp.com”, 主题:“这就是主题”, 身体:“这就是身体” }) ( 消息=>警报(消息) );

这是不可取的,因为它将显示您的密码在客户端。因此,您可以执行以下操作,加密SMTP凭据,并将其锁定到单个域,并传递一个安全令牌而不是凭据。

Email.send ({ SecureToken:“C973D7AD-F097-4B95-91F4-40ABC5567812”, 致:“them@website.com” 来自:“you@isp.com”, 主题:“这就是主题”, 身体:“这就是身体” }) ( 消息=>警报(消息) );

最后,如果你没有SMTP服务器,你可以使用SMTP中继服务,如弹性邮件

这里还有到官方SmtpJS.com网站的链接,您可以在其中找到所需的所有示例,以及创建安全令牌的位置。

我希望有人觉得这些细节有用。快乐的编码。