使用mailgunApi并使用javascript发送电子邮件


use mailgun api and send email with javascript?

我正在尝试使用mailgun.com发送电子邮件。但碰巧我需要用js发送它(因为有时我用rubyonrails建立网站,有时用python。现在我需要用邮件发送建立一个简单的登录页。)。和主机(这是免费的广告适合我只支持php,我不知道)所以我决定使用js来混淆这些代码,并将其粘贴到某个库中。所以没人会找到我的秘密钥匙)有人能帮忙把这些例子翻译成js代码吗?

这是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!"})

这是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);
}

这是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!'
));

这是轨道示例:

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

混乱的代码只会减慢那些试图搞砸的顽皮程序员的速度,这不是一个完美的安全层。既然您说您的主机支持php,请使用php代码。您所要做的就是向php脚本发送一个post请求考虑使用JavaScript的jQuery库发送的示例代码

    <?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代码

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

上面的代码向您的php文件发送一个post请求,php文件在其中验证它是否包含变量1和2,并继续执行