2014-07-22 61 views
1

我在我的Rails應用程序中使用RestClient發送電子郵件。 下面的curl命令作品:RestClient mailgun錯誤「message」:「'from'參數丟失」

curl -s "https://api:[email protected]/v2/sandbox30000.mailgun.org/messages" \ 
    -F from='Mailgun Sandbox <[email protected]>' \ 
    -F to='me <[email protected]>'\ 
    -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:[email protected]/v2/sandbox0000.mailgun.org/messages", 
    :method => :post, 
    :from => 'Mailgun Sandbox <[email protected]>', 
    :sender => 'Mailgun Sandbox <[email protected]>', 
    :to => "[email protected]", 
    :subject => "Hello XYZ", 
    :text => "Text body", 
    :"h:X-My-Header" => "www/mailgun-email-send", 
    :verify_ssl => false) 

回答

6

當你使用Request.execute和m多個有效載荷參數和頭部,你需要告訴RestClient它是什麼類型的數據。

您需要對這三個修補程序進行RestClient的等效curl請求。

  1. 集團所有領域的後進入有效載荷的哈希
  2. 添加單頭放入頭散列
  3. 告訴RESTClient實現有效載荷應多發送。

更改後的請求:

RestClient::Request.execute(
    :url => "https://api:[email protected]/v2/sandbox0000.mailgun.org/messages", 
    :method => :post, 
    :payload => { 
    :from => 'Mailgun Sandbox <[email protected]>', 
    :sender => 'Mailgun Sandbox <[email protected]>', 
    :to => "[email protected]", 
    :subject => "Hello XYZ", 
    :text => "Text body", 
    :multipart => true 
    }, 
    :headers => { 
    :"h:X-My-Header" => "www/mailgun-email-send" 
    }, 
    :verify_ssl => false 
) 
相關問題