【问题标题】:SendGrid add multiple email addresses to Recipient Lists in phpSendGrid 将多个电子邮件地址添加到 php 中的收件人列表
【发布时间】:2015-08-17 06:26:23
【问题描述】:

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

我正在成功将一个电子邮件地址添加到收件人列表中,请在此处查看代码

  $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

【问题讨论】:

    标签: php sendgrid


    【解决方案1】:

    您需要将每个电子邮件条目添加为自己的“数据”变量,如下所示:

    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);
    

    希望能助你一臂之力!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-23
      相关资源
      最近更新 更多