curl 使用MailerSend API和R发送电子邮件

xtupzzrd  于 2022-11-13  发布在  其他
关注(0)|答案(2)|浏览(184)

我正试图使用交易电子邮件服务MailerSend从R.他们有cURL说明如何做到这一点。
我不认为我有格式非常正确,因为我得到了错误:

> response
Response [https://api.mailersend.com/v1/email]
  Date: 2021-02-26 19:49
  Status: 401
  Content-Type: application/json
  Size: 30 B

> rsp_content <- content(response, as = "parsed", type = "application/json")
> rsp_content
$message
[1] "Unauthenticated."

R发送电子邮件的脚本

library("httr")

# MailerSend API Key
apiKeyMS <- Sys.getenv("MS_KEY")

# Email header info 
email_from = "noreply@example.ca"
email_from_name = "Joe"
email_subject <- "Joe Update"
email_to <- "joe@gmail.com"

# Email Body
email_text <- "This is my email"
email_html <- "This is my email"

# Send Email
ms_url <- paste0("https://api.mailersend.com/v1/email")

response <- POST(ms_url,
                 add_headers("Authorization: Bearer" = apiKeyMS, 
                             "X-Requested-With" = "XMLHttpRequest",
                             "Content-Type" = "application/json"),
                 body = list(
                   "subject" = email_subject,
                   "from" = email_from,
                   "from_name" = email_from_name,
                   "to" = email_to,
                   "text" = email_text,
                   "html" = email_html
                 ), encode = "json")
#############################################################

MailerSend cURL的基本说明

curl -X POST \
https://api.mailersend.com/v1/email \
-H 'Content-Type: application/json' \
-H 'X-Requested-With: XMLHttpRequest' \
-H 'Authorization: Bearer {place your token here without brackets}' \
-d '{
"from": {
"email": "your@email.com"
},
"to": [
{
"email": "your@email.com"
}
],
"subject": "Hello from MailerSend!",
"text": "Greetings from the team, you got this message through MailerSend.",
"html": "Greetings from the team, you got this message through MailerSend."
}'
mzaanser

mzaanser1#

我根本不知道R,但我猜你的不记名令牌是不正确的。
标头关键字为Authorization,值应为“Bearer {将令牌放在此处,不带括号}”

cbeh67ev

cbeh67ev2#

你必须有这三个标题

-H 'Content-Type: application/json' \
-H 'X-Requested-With: XMLHttpRequest' \
-H 'Authorization: Bearer {place your token here without brackets}' \`

相关问题