0

I would like to send email list to sparkpost API using curl here is the curl as in sparkpost docs

    curl -v \
-H "Content-Type: application/json" \
-H "Authorization: $API_KEY" \
-X GET "https://api.sparkpost.com/api/v1/metrics/deliverability/aggregate?campaigns=testjob&from=2014-01-23T14:00&metrics=count_targeted,count_sent,count_accepted&timezone=America%2FNew_York&to=2014-06-23T15:50"

How do i do this with PHP curl

gokcand
  • 6,694
  • 2
  • 22
  • 38
  • I'd start with the [PHP curl docs](http://php.net/manual/en/curl.examples-basic.php). – Alex Howansky Jan 04 '18 at 15:12
  • The first BIG tip is do NOT share your API Key if that is indeed what that is. – Yepher Jan 04 '18 at 15:55
  • I am not clear on your question. What you have above is a metrics API call. But your question seems to indicate you want to send an email to a list. Can you please clarify which you are looking for? – Yepher Jan 04 '18 at 16:00
  • Sorry, one more question about, your question. If you do indeed want to do a transmission to a list then do you want to provide the list at the time of injection or do you want to use a list you have stored in SparkPost? – Yepher Jan 04 '18 at 16:04

3 Answers3

1

If you want to create an email list on the SparkPost server you can use this API: https://developers.sparkpost.com/api/recipient-lists.html#recipient-lists-create-post here is an example in PHP:

<?php

$request = new HttpRequest();
$request->setUrl('https://api.sparkpost.com/api/v1/recipient-lists');
$request->setMethod(HTTP_METH_POST);

$request->setQueryData(array(
  'num_rcpt_errors' => '3'
));

$request->setHeaders(array(
  'Cache-Control' => 'no-cache',
  'Authorization' => $YOUR_API_KEY_HERE // put your API key here
));

// You will need to put PHP code that reads from your DB and puts Your recipients in that array below. You only need the address->name and address-email bit if you don't have additional metadata
$request->setBody('{
  "id": "unique_id_4_graduate_students_list",
  "name": "graduate_students",
  "description": "An email list of graduate students at UMBC",
  "attributes": {
    "internal_id": 112,
    "list_group_id": 12321
  },
  "recipients": [
    {
      "address": {
        "email": "wilmaflin@example.com",
        "name": "Wilma"
      },
      "tags": [
        "greeting",
        "prehistoric",
        "fred",
        "flintstone"
      ],
      "metadata": {
        "age": "24",
        "place": "Bedrock"
      },
      "substitution_data": {
        "favorite_color": "SparkPost Orange",
        "job": "Software Engineer"
      }
    },
    {
      "address": {
        "email": "abc@example.com",
        "name": "ABC"
      },
      "tags": [
        "driver",
        "flintstone"
      ],
      "metadata": {
        "age": "52",
        "place": "MD"
      },
      "substitution_data": {
        "favorite_color": "Sky Blue",
        "job": "Driver"
      }
    },
    {
      "address": {
        "email": "fred.jones@example.com",
        "name": "Grad Student Office",
        "header_to": "grad-student-office@example.com"
      },
      "tags": [
        "driver",
        "fred",
        "flintstone"
      ],
      "metadata": {
        "age": "33",
        "place": "NY"
      },
      "substitution_data": {
        "favorite_color": "Bright Green",
        "job": "Firefighter"
      }
    }
  ]
}');

try {
  $response = $request->send();

  echo $response->getBody();
} catch (HttpException $ex) {
  echo $ex;
}

Assuming you want to send an email to a list of recipients you have stored in SparkPost below is one way to do it in PHP.

For this to work you will need to create a "recipient list" in SparkPost here https://app.sparkpost.com/lists/recipients and provide the ID of the list in the request below. You will also need an API key that allows REST Injection which can be created here https://app.sparkpost.com/account/credentials You will also need a valid sending domain which I assume you have already setup in SparkPost.

<?php

$request = new HttpRequest();
$request->setUrl('https://api.sparkpost.com/api/v1/transmissions');
$request->setMethod(HTTP_METH_POST);

$request->setHeaders(array(
  'Cache-Control' => 'no-cache',
  'Authorization' => $YOUR_SPARKPOST_API_KEY . // Put your "real" API key here or better set a variable and use that
));

$request->setBody('{
    "options": {
        "open_tracking": true,
        "click_tracking": true
    },
  "campaign_id": "test_campaign",
  "recipients": {
    "list_id": "unique_id_4_graduate_students_list" // Put your List ID here from here https://app.sparkpost.com/lists/recipients
  },
  "content": {
    "from": {
      "email": "test@test.example.com", // Change this to your proper from address
      "name": "John Doe"
    },
    "subject": "simple subject",
    "text": "simple text content",
    "html": "<b>Your HTML Content</b>"
  }
}

');

try {
  $response = $request->send();

  echo $response->getBody();
} catch (HttpException $ex) {
  echo $ex;
}

Here are a few more tips that may get your going quicker. There is a SparkPost PHP library here: https://github.com/SparkPost/php-sparkpost

Also, for more "real-time" questions and answers you can join the community Slack here: http://slack.sparkpost.com/ Once you are in check out the #php channel.

If you want to get familiar with the API you can try out the PostMan resources. Here is a good blog post to get you going: https://www.sparkpost.com/blog/how-to-run-sparkpost-postman/ Once you have that going PostMan can produce code in most common languages to help you get going quickly.

Yepher
  • 1,465
  • 12
  • 25
  • Thanks alot , I'm not new to PHP , i have a list of emails from database that I would like to send to sparkpost and that's all for now. your code will come handy. Any suggestions Many thanks ? – user2560659 Jan 04 '18 at 16:33
  • I have a few suggestions. This is the API you will need: https://developers.sparkpost.com/api/recipient-lists.html I made this CLI a while back that will allow you to import your recipient list here https://github.com/SparkPost/sparkpost-cli/releases or you can import a CSV into the recipient list in the WebUI here https://app.sparkpost.com/lists/recipients Is it a requirement to do it in PHP if so I can make an example for you. – Yepher Jan 04 '18 at 16:37
  • I added the PHP command to upload a list of recipients to the SparkPost server. I hope that answers your question. I also changed the transmission to use that "list id" – Yepher Jan 04 '18 at 16:51
0
$ch = curl_init("https://api.sparkpost.com/api/v1/metrics/deliverability/aggregate?campaigns=testjob&from=2014-01-23T14:00&metrics=count_targeted,count_sent,count_accepted&timezone=America%2FNew_York&to=2014-06-23T15:50");
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Authorization: $API_KEY',
'Content-Type: application/json'
));

$data = curl_exec($ch);
curl_close($ch);

More info: Docs

Yepher
  • 1,465
  • 12
  • 25
gokcand
  • 6,694
  • 2
  • 22
  • 38
0

If you have access to shell_exec() you could invoke it as a shell command line:

<?php
$output = shell_exec('curl -H "Content-Type: application/json" -H "Authorization: '.$API_KEY.'" -X GET "https://api.sparkpost.com/api/v1/metrics/deliverability/aggregate?campaigns=testjob&from=2014-01-23T14:00&metrics=count_targeted,count_sent,count_accepted&timezone=America%2FNew_York&to=2014-06-23T15:50"');

echo $output;
?>

Assuming that you're interested in the server response only, I've removed the -v argument from the command line which stands for verbose mode, verbose mode prints extra debugging information about the curl request which will mess with the output, for more on how to use curl in command line use man curl

Shadi Shaaban
  • 1,670
  • 1
  • 10
  • 17