我在rails应用程序中使用RestClient发送电子邮件。 以下Curl命令有效:
curl -s "https://api:key-XYZ@api.mailgun.net/v2/sandbox30000.mailgun.org/messages" \
-F from='Mailgun Sandbox <postmaster@sandbox30000.mailgun.org>' \
-F to='me <my-email-id@gmail.com>'\
-F subject='Hello XYZ' \
-F text='Congratulations, you just sent an email with Mailgun! You are truly awesome!'
但是当我使用RestClient尝试相同时,我收到以下错误消息:
error response !!{
"message": "'from' parameter is missing"
}
错误消息!! 400错误请求
RestClient电话:
RestClient::Request.execute(
:url => "https://api:key-XYZ@api.mailgun.net/v2/sandbox0000.mailgun.org/messages",
:method => :post,
:from => 'Mailgun Sandbox <postmaster@sandbox30000.mailgun.org>',
:sender => 'Mailgun Sandbox <postmaster@sandbox30000.mailgun.org>',
:to => "my-email-id@gmail.com",
:subject => "Hello XYZ",
:text => "Text body",
:"h:X-My-Header" => "www/mailgun-email-send",
:verify_ssl => false)
答案 0 :(得分:6)
当您使用带有多个有效负载参数和标头的Request.execute时,您需要告诉RestClient它是什么类型的数据。
您需要使用这三个修复程序来获取与RestClient相同的curl请求。
更改的请求:
RestClient::Request.execute(
:url => "https://api:key-XYZ@api.mailgun.net/v2/sandbox0000.mailgun.org/messages",
:method => :post,
:payload => {
:from => 'Mailgun Sandbox <postmaster@sandbox30000.mailgun.org>',
:sender => 'Mailgun Sandbox <postmaster@sandbox30000.mailgun.org>',
:to => "my-email-id@gmail.com",
:subject => "Hello XYZ",
:text => "Text body",
:multipart => true
},
:headers => {
:"h:X-My-Header" => "www/mailgun-email-send"
},
:verify_ssl => false
)