简体   繁体   English

使用mailgun api并使用javascript发送电子邮件?

[英]use mailgun api and send email with javascript?

I am trying to use mailgun.com for sending emails. 我正在尝试使用mailgun.com发送电子邮件。 But it happened that I need to send it with js (cause sometimes I built websites with rubyonrails, sometimes with python. And now I need to built a simple landing page with mail sending. And hosting (which is free ad suits me only supports php which I don't know) So I decided to use js and obfuscate this code and paste it somewhere in somelibrary.So no one will ever find my secret key) Can someone help with translating some of this examples into js code? 但是碰巧我需要用js发送它(因为有时我用rubyonrails来构建网站,有时是用python。现在我需要用邮件发送来构建一个简单的登陆页面。并且托管(这是免费广告,适合我,仅支持php)我不知道),所以我决定使用js并对其进行混淆处理,然后将其粘贴到某个库中的某个地方。因此,没人会找到我的秘密密钥)有人可以帮忙将其中的一些示例翻译成js代码吗?

This is python example: 这是python示例:

def send_simple_message():
    return requests.post(
        "https://api.mailgun.net/v3/YOUR_DOMAIN_NAME/messages",
        auth=("api", "YOUR_API_KEY"),
        data={"from": "Excited User <mailgun@YOUR_DOMAIN_NAME>",
              "to": ["bar@example.com", "YOU@YOUR_DOMAIN_NAME"],
              "subject": "Hello",
              "text": "Testing some Mailgun awesomness!"})

This is c# example 这是C#示例

public static IRestResponse SendSimpleMessage() {
       RestClient client = new RestClient();
       client.BaseUrl = new Uri("https://api.mailgun.net/v3");
       client.Authenticator =
               new HttpBasicAuthenticator("api",
                                          "YOUR_API_KEY");
       RestRequest request = new RestRequest();
       request.AddParameter("domain",
                            "YOUR_DOMAIN_NAME", ParameterType.UrlSegment);
       request.Resource = "{domain}/messages";
       request.AddParameter("from", "Excited User <mailgun@YOUR_DOMAIN_NAME>");
       request.AddParameter("to", "bar@example.com");
       request.AddParameter("to", "YOU@YOUR_DOMAIN_NAME");
       request.AddParameter("subject", "Hello");
       request.AddParameter("text", "Testing some Mailgun awesomness!");
       request.Method = Method.POST;
       return client.Execute(request);
}

This is php example 这是PHP的例子

# Include the Autoloader (see "Libraries" for install instructions)
require 'vendor/autoload.php';
use Mailgun\Mailgun;

# Instantiate the client.
$mgClient = new Mailgun('YOUR_API_KEY');
$domain = "YOUR_DOMAIN_NAME";

# Make the call to the client.
$result = $mgClient->sendMessage($domain, array(
    'from'    => 'Excited User <mailgun@YOUR_DOMAIN_NAME>',
    'to'      => 'Baz <YOU@YOUR_DOMAIN_NAME>',
    'subject' => 'Hello',
    'text'    => 'Testing some Mailgun awesomness!'
));

This is rails example: 这是rails示例:

def send_simple_message
  RestClient.post "https://api:YOUR_API_KEY"\
  "@api.mailgun.net/v3/YOUR_DOMAIN_NAME/messages",
  :from => "Excited User <mailgun@YOUR_DOMAIN_NAME>",
  :to => "bar@example.com, YOU@YOUR_DOMAIN_NAME",
  :subject => "Hello",
  :text => "Testing some Mailgun awesomness!"
end

Obfuscated code just slows down the naughty coder who is trying to mess up, its not a perfect layer of security. 混淆的代码只会减慢试图搞乱顽皮的编码器的速度,这不是完美的安全层。 Since you say your host supports php use the php code. 既然您说您的主机支持php,请使用php代码。 all you have to do is send a post request to the php script example code considering you are sending using jQuery library for javascript 您要做的就是考虑到您正在使用jQuery库发送javascript来向php脚本示例代码发送请求请求

    <?php
require 'vendor/autoload.php';
use Mailgun\Mailgun;

if(isset($_POST['variable1']) && isset($_POST['variable2']))
{
$msg = $_POST['variable1']." ".$_POST['variable1'];
$mgClient = new Mailgun('key');
$domain = "your domain";
$result = $mgClient->sendMessage($domain, array(
    'from'    => 'from adress',
    'to'      => 'to adress',
    'subject' => 'some subject',
    'html'    => $msg
));
$result = objectToArray($result);
echo json_encode($result);
}
?>

jquery code to send post request jQuery代码发送帖子请求

$("button").click(function(){
    $.post("mailer.php",
    {
        variable1: "Donald Duck",
        variable2: "Duckburg"
    },
    function(data, status){
        console.log(data);
    });
});

The above code sends a post request to your php file, where the php file validates if it contains the variables 1 and 2 and continues with the execution 上面的代码向您的php文件发送了一个发布请求,该php文件将验证它是否包含变量1和2并继续执行

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

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