简体   繁体   English

使用Mailgun从Chrome扩展程序发送电子邮件

[英]Send an email from Chrome extension with Mailgun

I have a small Chrome extension that I use just for myself. 我有一个小的Chrome扩展程序,仅供我自己使用。 Currently it uses Mandrill, but Mandrill is going to stop being free in about a month and I am looking for other ways to send emails from the extension. 目前,它使用Mandrill,但Mandrill将在大约一个月后停止免费,我正在寻找其他方法来从扩展程序发送电子邮件。

What I need is something really simple, and pure Javascript-jQuery. 我需要的是非常简单的纯Javascript-jQuery。 I don't mind exposing an API key in the code since the extension is just for my own use. 我不介意在代码中公开API密钥,因为扩展名仅供我自己使用。 I've been looking at documentation in sites like Mailgun or SparkPost, but I can't find a simple and pure JavaScript example. 我一直在查看Mailgun或SparkPost等网站中的文档,但是找不到简单,纯净的JavaScript示例。 For instance, this is the code I tried for Mailgun; 例如,这是我为Mailgun尝试的代码; it's raising a 404 even though I'm using my sandbox URL and it is active: 即使我使用我的沙箱网址,它也会显示404并处于活动状态:

$.ajax({
    type: "POST",
    url: "MY SANDBOX URL",
    data: {
      'user':'api',
      'key': 'MY KEY',
      'from': 'SENDER EMAIL ADDRESS',
      'to': 'RECIPIENT EMAIL ADDRESS',
      'subject': 'Subject',
      'text': 'Body'
      }
    }).done(function(response) {
        console.log("Email sent");
 });

Please, could you help me? 拜托,你能帮我吗? Thanks! 谢谢!

Sorry for the delay, I was really busy and thought this would take many hours of struggling so I did not try it until I had some time. 对不起,很抱歉,我真的很忙,并认为这将需要很多小时的努力,因此直到有一段时间我才尝试。 In fact all the process took less than 30 minutes because SendGrid has a nice documentation and is very easy to use, at least for what I need. 实际上,所有过程花费了不到30分钟的时间,因为SendGrid有一个很好的文档并且非常易于使用,至少满足我的需要。 Thanks Adnan Umer for pointing me to this tool. 感谢Adnan Umer向我指出该工具。

Here is what I did: 这是我所做的:

1) Create a free account. 1)创建一个免费帐户。 Allows up to 12k emails per month, more than enough for me. 每月最多允许1万2千封电子邮件,对我来说绰绰有余。

2) Wait until the account is provisioned. 2)等待,直到提供帐户。 In my case this was instant, but there could be a delay sometimes, allegedly up to several hours. 就我而言,这是即时的,但有时可能会延迟一些,据称可能长达几个小时。

3) Generate a general API key here: https://app.sendgrid.com/settings/api_keys with full access to "Mail Send". 3)在此处生成常规API密钥: https : //app.sendgrid.com/settings/api_keys,具有对“邮件发送”的完全访问权限。

4) Send an email adding an "Authorization" header, and the key as a "Bearer" token in that header. 4)发送一封电子邮件,其中添加“ Authorization”标题,并在该标题中添加密钥作为“ Bearer”令牌。

In my case this is working ok: 就我而言,这可以正常工作:

$.ajax({
    type: "POST",
    url: "https://api.sendgrid.com/api/mail.send.json",
    headers: {
        'Authorization': 'Bearer ' + SENDGRID_API_KEY
    },
    data: {
        'to': EMAIL_ADDRESS_TO,
        'from': EMAIL_ADDRESS_FROM,
        'subject': 'Email subject',
        'html': 'Email body',
    }
}).done(function(response) {
    console.log("Email sent");
});

Thank you all! 谢谢你们!

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

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