Current section

Files

Jump to
aliyun_api lib aliyun_api email cli.ex
Raw

lib/aliyun_api/email/cli.ex

defmodule AliyunAPI.Email do
import AliyunAPI, only: [url_encode: 1]
@doc """
发送邮件
doc:https://help.aliyun.com/document_detail/29444.html
## opts
* `app` 必须
* `to_address` 必须
* `from_alias` 必须
* `subject` 必须
* `html_body`
* `text_body`
* `click_trace` true/false
* `region`
## Examples
```
## 发送纯文本
AliyunAPI.Email.send(
app: :worth,
to_address: "andywang9527@qq.com",
subject: "Hello Test from aliyun util",
text_body: "Hello Worth"
)
## 发送富文本
AliyunAPI.Email.send(
app: :worth,
to_address: "andywang9527@qq.com",
subject: "Hello Test from aliyun util",
html_body: "<h1>Hello Worth</h1><div style=\"color: red;\">hello world</div>"
)
```
"""
def send(opts \\ []) do
email_opts = AliyunAPI.Env.email_opts(opts[:app])
IO.inspect(email_opts)
url = "https://#{AliyunAPI.Env.email_endpoint(opts[:app])}"
params =
"SingleSendMail"
|> _format_arguments(opts[:app])
|> Map.merge(%{
"AccountName" => email_opts[:from_address],
"ReplyToAddress" => true,
"AddressType" => 1,
"ToAddress" => opts[:to_address],
"FromAlias" => opts[:from_alias] || email_opts[:from_alias],
"Subject" => opts[:subject],
"ClickTrace" => "1",
"HtmlBody" => opts[:html_body],
"TextBody" => opts[:text_body]
})
|> _normalize_params(opts[:app], "GET")
# AliyunAPI.http_request(url, :post, body: URI.encode_query(params))
AliyunAPI.http_request(url <> "?" <> URI.encode_query(params), :get)
end
defp _normalize_params(arguments, app, http_method) do
arguments = arguments |> AliyunAPI.filter_params()
signature = arguments |> _get_signature(app, http_method)
arguments |> Map.put("Signature", signature)
end
defp _get_signature(arguments, app, method) do
args =
arguments
|> Enum.sort_by(fn {key, _} -> key end)
|> Enum.map(fn {key, val} -> {url_encode(key), url_encode(val)} end)
|> Enum.map(fn {key, val} -> "#{key}=#{val}" end)
|> Enum.join("&")
string_to_sign =
"#{String.upcase(method)}" <> "&" <> url_encode("/") <> "&" <> url_encode(args)
:sha
|> :crypto.hmac("#{AliyunAPI.Env.email_access_key_secret(app)}&", string_to_sign)
|> Base.encode64()
end
defp _format_arguments(action, app) do
%{
"Format" => "JSON",
"Version" => "2015-11-23",
"SignatureMethod" => "HMAC-SHA1",
"SignatureNonce" => AliyunAPI.SecureRandom.uuid(),
"SignatureVersion" => "1.0",
"Action" => action,
"AccessKeyId" => AliyunAPI.Env.email_access_key_id(app),
"Timestamp" => Timex.format!(Timex.now(), "%Y-%m-%dT%H:%M:%SZ", :strftime)
}
end
end