SendGrid将多个电子邮件地址添加到php中的收件人列表

时间:2015-08-17 06:26:24

标签: php sendgrid

我在向收件人列表添加多个电子邮件地址时遇到了很多困难。

我正在向收件人列表成功添加一个电子邮件地址,请参阅此处的代码

  $email_list = array ( "email" => "srinivas1@addpronetwork.com",'name' => "srinivas");
  $json_email =  json_encode($email_list);

  $url = 'https://api.sendgrid.com/api/newsletter/lists/email/add.json';

  $params = array(
    'list'   => urlencode($list_name),
    'data' =>  $json_email,
    'api_user'  => $this->config->item('api_username'),
    'api_key'   => $this->config->item('api_password'),
  );
    $request =  $url;

    // Generate curl request
    $session = curl_init($request);
    curl_setopt($session, CURLOPT_SSL_VERIFYPEER, false);

    // 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);
    // Tell PHP not to use SSLv3 (instead opting for TLS)
    /*curl_setopt($session, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2);*/
    curl_setopt($session, CURLOPT_RETURNTRANSFER, true);

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

    $obj = json_decode($response);

在上面的代码中,我将单个电子邮件地址添加到列表中..我的数据参数看起来像这样

data => '{ "name": "srinivas", "email": "srinivas@addpronetwork.com" }'

当我尝试添加多个电子邮件地址但我遇到问题时......请参阅数据参数

['{"name":"Srinu","email":"srinivas@addpronetwork.com"}','{"name":"Pallavi","email" :"pallavi@addpronetwork.com"}']

请参阅以下链接

https://sendgrid.com/docs/API_Reference/Marketing_Emails_API/emails.html#-add

1 个答案:

答案 0 :(得分:0)

您需要将每个电子邮件条目添加为自己的数据'像这样的变量:

curl -X POST https://api.sendgrid.com/api/newsletter/lists/email/add.json \
 -d 'api_user=your_sendgrid_username' \
 -d 'api_key=your_sendgrid_password' \
 -d 'list=my_list' \
 -d 'data[]={"email":"address1@domain.com","name":"contactName1"}' \
 -d 'data[]={"email":"address2@domain.com","name":"contactName2"}'

过去我只是通过我的列表循环将它们添加到我的营销电子邮件中,我真的不在php中工作,但这似乎有效:

$email_list = array ( '{"name":"Srinu","email":"srinivas@addpronetwork.com"}','{"name":"Pallavi","email" :"pallavi@addpronetwork.com"}');

$url = 'https://api.sendgrid.com/api/newsletter/lists/email/add.json';

foreach ($email_list as &$email) {
 $params = array(
'list'   => "my_list"),
'data[]' =>  $email,
'api_user'  => "api_username",
'api_key'   => "api_password",
);
$request =  $url;

// Generate curl request
$session = curl_init($request);
curl_setopt($session, CURLOPT_SSL_VERIFYPEER, false);

// 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);
// Tell PHP not to use SSLv3 (instead opting for TLS)
/*curl_setopt($session, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2);*/
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);

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

$obj = json_decode($response);
print_r($response);

希望能让你前进!