How to use Single Send API in PHP Application Form Submission


#1

I need to send order confirmation details and etc email formats to the customer using the transitional email(Single Send API or others). How I need to parse the custom values to the API.

Already created email template too.

It have only for JSON, Can you provide me any document for PHP


#2

Hi @clubstohire,

I'm not particularly familiar with PHP, but no matter what language you use the data sent to the single send API must be properly formatted JSON. Below is some documentation on the built-in JSON methods, but I'm sure there are also a number of libraries that effectively handle JSON data:

http://php.net/manual/en/book.json.php


#3

Hi Derek,

Thanks for the reply. I created new template on my account and need to send multiple custom values to the api. Can you review below PHP code(whom well know PHP).

$emailId =  "1234567890";
    $to = "test@test.com";
    $orderNumber = "1234";
    $firstName = "Test";    
    $pickupDate = "25/03/2018";    
    $pickupLocation = "UK";
    $locationSummary = "test";
    $forecast = "test";
    $currentYear = date('Y');
    
    $emailValues = array(
            'emailId' => $emailId,
            'message' => array(
                'to'=>$to
            ),
            'customProperties' => array(
                array(
                    'name' => 'ordernumber',
                    'value' => $orderNumber
                )),
                'customProperties' => array(
                array(
                    'name' => 'firstname',
                    'value' => $firstName
                )),
                'customProperties' => array(
                array(
                    'name' => 'pickupdate',
                    'value' => $pickupDate
                )),
                'customProperties' => array(
                array(
                    'name' => 'location',
                    'value' => $pickupLocation
                ))
                ,
                'customProperties' => array(
                array(
                    'name' => 'location_summary',
                    'value' => $locationSummary
                ))
                ,
                'customProperties' => array(
                array(
                    'name' => 'forecast',
                    'value' => $forecast
                ))
                ,
                'customProperties' => array(
                    array(
                    'name' => 'current_year',
                    'value' => $currentYear
                ))
            
        );
    $post_json = json_encode($emailValues);
    $hapiKey = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";
    $endpoint = "http://api.hubapi.com/email/public/v1/singleEmail/send?hapikey=$hapiKey";
    $ch = @curl_init();
    @curl_setopt($ch, CURLOPT_POST, true);
    @curl_setopt($ch, CURLOPT_POSTFIELDS, $post_json);
    @curl_setopt($ch, CURLOPT_URL, $endpoint);
    @curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
    @curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $response = @curl_exec($ch);
    $status_code = @curl_getinfo($ch, CURLINFO_HTTP_CODE);
    $curl_errors = curl_error($ch);
    @curl_close($ch);
    echo "curl Errors: " . $curl_errors;
        echo "\nStatus code: " . $status_code;
        echo "\nResponse: " . $response;   

I am getting below error:
curl Errors: Protocol " https" not supported or disabled in libcurl Status code: 0 Response:

Please help me to solve the above error or guide me the way to go right direction of my issue


#4

Hi @clubstohire,

That error has a space before the protocol (i.e. " https" instead of "https"). Is it possible that you have an extra space in there somewhere? That error commonly appears when the protocol is mistyped:

https://curl.haxx.se/docs/faq.html


#5

Hi @Derek_Gervais,

Please check my code, there is no space in before or after "https" and I tried with "http" also. Same curl execution working for other API, but its not working for the Single Send API. I hope someone will guide me on this, who is well known of PHP.


#6

Hi @clubstohire,

After looking a bit more closely, it looks like the protocol of the URL you specified in $endpoint is http instead of https. That might possibly be the issue, since the HubSpot APIs all require https.


#7

Hi @Derek_Gervais,

Before I tried with https also but now its working. But I am getting below error.

`curl Errors: Status code: 400 Response: {"status":"error","message":"Mismatch in custom properties between template and request.\n - Custom properties in the template, but not in the request: firstname, current_year, location_summary, ordernumber, pickupdate, forecast, location","correlationId":"a21ed56b-7a88-4420-83bd-fcc564dcc3df","sendResult":"MISSING_TEMPLATE_PROPERTIES","requestId":"79f9621e2a0ed45af1e72e411196bf87"}`

But I passed all the values to the API. Please review my above code.


#8

Hi @Derek_Gervais,

I solved the issue and receiving the email also. If any other issues I will posted in Forum.

Thank you for your assistance.


#9

Hi @Derek_Gervais,

Need your help on sending email to multiple CC and BCC.

$cc = "abc@test.com","cde@test.com","xxx@test.com";
$bcc = "abc@test.com","cde@test.com","xxx@test.com";

'message' => array(
                'to'=>$to,
                'cc'=>$cc,
                'bcc'=>$bcc
            )

#10

Hi @clubstohire,

The cc and bcc fields must be arrays, since they can contain multiple email addresses. See the following example:

{
    "emailId": 2853049635,
    "message": {
        "to": "example@hubspot.com",
        "cc": ["example1@hubspot.com"],
        "bcc": ["examplebcc@hubspot.com"]
    },
    "contactProperties": [
        {
            "name": "first_name",
            "value": "John"
        }
    ],
    "customProperties": [
        {
            "name": "item_1",
            "value": "something they bought"
        }
    ]
}