我正在尝试使用交易电子邮件服务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."
发送电子邮件的脚本
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."
}'
我根本不知道R,但我猜你的不记名令牌是错误的。
头密钥是Authorization,值应该是"Bearer{将您的令牌放在这里,不带括号}">
你必须有这三个标题
-H 'Content-Type: application/json'
-H 'X-Requested-With: XMLHttpRequest'
-H 'Authorization: Bearer {place your token here without brackets}' `