Skip to content

Instantly share code, notes, and snippets.

@Ljzn
Created July 16, 2019 16:59
Show Gist options
  • Save Ljzn/cffad91a3344e628411a5e609eb84702 to your computer and use it in GitHub Desktop.
Save Ljzn/cffad91a3344e628411a5e609eb84702 to your computer and use it in GitHub Desktop.
阿里云邮件推送 elixir sdk. aliyun mail elixir sdk
defmodule Aliyunmail do
@url "https://dm.ap-southeast-1.aliyuncs.com"
@access_key_id "LTAIBXtSnSsfyHPB"
@secret "xxxxx&"
def send_mail(to_address, html_body) do
unsigned = %{
"Action" => "SingleSendMail",
"AccountName" => "xxx@xxx.io",
"ReplyToAddress" => false,
"AddressType" => 1,
"ToAddress" => to_address,
"Subject" => "Subject",
"HtmlBody" => html_body |> replace_whitespace(),
"AccessKeyId" => @access_key_id,
"Version" => "2017-06-22",
"SignatureMethod" => "HMAC-SHA1",
"SignatureVersion" => "1.0",
"SignatureNonce" => (:os.system_time() |> to_string()) <> (:random.uniform() |> to_string()),
"Timestamp" => DateTime.utc_now() |> DateTime.to_iso8601() |> to_string()
}
query = %{"a" => URI.encode_query(unsigned)} |> URI.encode_query() |> String.trim_leading("a=")
tosign = "GET&%2F&" <> query |> IO.inspect(label: "to sign")
sig =
tosign
|> sign(@secret)
params =
unsigned
|> Map.put("Signature", sig)
HTTPoison.get(@url <> "?" <> URI.encode_query(params), [{"application", "x-www-form-urlencoded"}])
end
def sign(data, secret) do
:crypto.hmac(:sha, secret, data)
|> Base.encode64()
end
defp replace_whitespace(string) do
String.replace(string, " ", "&nbsp;")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment