2017-03-08 161 views
1

我很困惑這裏的$ pass和$ api_key 它們是否相同,因爲最初$ pass被賦予了SendGrid用戶名的密碼,但是後來api_key被賦予了$ pass。 如果它們相同,我們將使用我們在SendGrid上生成的api_key? 請幫忙!!!使用Web Api發送帶有SendGrid的電子郵件php

<?php 
$url = 'http://sendgrid.com/'; 
$user = 'USERNAME'; 
$pass = 'PASSWORD'; 

$params = array(
    'api_user' => $user, 
    'api_key' => $pass, 
    'to'  => '[email protected]', 
    'subject' => 'testing from curl', 
    'html'  => 'testing body', 
    'text'  => 'testing body', 
    'from'  => '[email protected]', 
); 


$request = $url.'api/mail.send.json'; 

// Generate curl request 
$session = curl_init($request); 
// Tell curl to use HTTP POST 
curl_setopt ($session, CURLOPT_POST, true); 
// Tell curl that this is the body of the POST 
curl_setopt ($session, CURLOPT_POSTFIELDS, $params); 
// Tell curl not to return headers, but do return the response 
curl_setopt($session, CURLOPT_HEADER, false); 
curl_setopt($session, CURLOPT_RETURNTRANSFER, true); 

// obtain response 
$response = curl_exec($session); 
curl_close($session); 

// print everything out 
print_r($response); 

?> 

回答

0

有一些問題與您正在試圖做什麼:

  • 你不應該發送API調用sendgrid.com,只有api.sendgrid.com
  • 它看起來像你正在嘗試使用帶參數的v2 API。由於您正在設置新腳本,因此您應該使用v3 API,它使用適當的主體JSON參數,並且是RESTful。
  • 而不是設置自己的腳本,你看過SendGrid PHP Library
  • 要在腳本中進行身份驗證,您應該始終使用API Key,而不是您的主用戶名&密碼。

它看起來像你有一個非常舊的示例腳本進行測試。你從哪裏得到那個的?

+0

我不懂PHP,也沒有腳本。你檢查過SendGrid PHP庫嗎?這應該給你一個強大的開頭。 – jacobmovingfwd