[PHP] Register / cancel e-mail newsletter with blastmail API

Sep 7, 2020 PHP api

#Overview I had the opportunity to implement e-mail newsletter registration / cancellation using blastmail. It is a memo about the procedure at that time and how to use the API.

Preparing #blustmail If you register as a member of blastmail,

Will be issued.

You will use this information to use the blastmail API. The API Key is included in the email when you registered.

You can also check it at the bottom of ** “Contract Information> Account Information” ** on the management screen.

#API integration As a procedure for registering and canceling e-mail newsletters using the API,

  1. Log in via API and issue an access token
  2. Include the issued access token in the parameter and hit the API
  3. You can check new registrations to e-mail newsletters and user registration information.

It’s like that.

API reference for blastmail

Login (issue access token)

Click here for detailed parameters

First, log in to blustmail and issue an access token. Login is valid only for POST, and this time I used Curl.

$ url ='https://api.bme.jp/rest/1.0/authenticate/login';
$ login_data = [
    'username' =>'Login ID',
    'password' =>'login password',
    'api_key' =>'API Key',
    'f' =>'json' // Specify response in json format
];;

// curl settings
$ ch = curl_init ();
curl_setopt ($ ch, CURLOPT_URL, $ url); // Specify the URL to get
curl_setopt ($ ch, CURLOPT_CUSTOMREQUEST,'POST'); // Send POST
curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, true); // Return the execution result as a string
curl_setopt ($ ch, CURLOPT_POSTFIELDS, http_build_query ($ login_data)); // Value to POST
$ response = curl_exec ($ ch); // Hit the API
$ token = json_decode ($ response, true); // Convert response from json to array

// Login failed
if (! isset ($ token ['accessToken'])) {
    echo'Login failed';
}

// Response result ($ token)
// {'accessToken':'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'}

If you log in successfully, you will receive an accessToken.

Log out (destroy access token)

Click here for detailed parameters

Log out and destroy the access token. I used file_get_contents () because I can use GET to log out.

$ url ='https://api.bme.jp/rest/1.0/authenticate/logout?';
$ query = ['access_token' => $ token ['accessToken']];
$ response = file_get_contents ($ url. Http_build_query ($ query));

$ response can be 0 (false) or 1 (true). If the logout is successful, true will be returned.

#Individual registration (new registration) This is the case when you newly subscribe to the e-mail newsletter.

Item setting

This time, the only information required for new registration is the “email address”.

You can decide the items to be registered from “Item settings”. You can also register your name and phone number.

qiita_blast002.png

You can check the parameter name of the item to be registered below. ** “Text mail (new mail)> Merge code” **

If it is an email address, it will be “c15”.

qiita_blast003.png

##Implementation Click here for detailed parameters

Send registration data by POST using Curl.

// $ token ['accessToken'] was obtained by logging in
// Assume that the access token has been assigned

$ url ='https://api.bme.jp/rest/1.0/contact/detail/create';
$ regist_data = [
    'access_token' => $ token ['accessToken'],
    'c15' =>'Email address you want to register' // Parameters of the item you want to register
];;

// curl settings
$ ch = curl_init ();
curl_setopt ($ ch, CURLOPT_URL, $ url);
curl_setopt ($ ch, CURLOPT_CUSTOMREQUEST,'POST');
curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt ($ ch, CURLOPT_POSTFIELDS, http_build_query ($ register_data));
$ response = curl_exec ($ ch);

// Registration failed
if (! (bool) $ response) {
    echo'Registration failed';
}

$ response can be 0 (false) or 1 (true). If registration is successful, true will be returned.

It is successful if the registered users are displayed in the “Reader List” on the management screen. qiita_blast001.png

Could not register new because the information remains in the database. Therefore, you need to change the delivery status described below.

As an implementation, check if there is a registration history and

  1. If not registered, new registration
  2. If there is a registration history, change the status to “delivering”

New registration will be implemented in the form of. Finally, the source code that summarizes these is described.

#Change delivery status If you want to delete the registration or unsubscribe, change the status. You need the registrant’s ID to change it, so first search for the user and get the ID.

Click here for detailed parameters

Search for registration information by email address or user ID.

// $ token ['accessToken'] was obtained by logging in
// Assume that the access token has been assigned

$ url ='https://api.bme.jp/rest/1.0/contact/detail/search?';
$ query = [
    'access_token' => $ token ['accessToken'],
    'email' =>'email address',
    'f' =>'json'
];;
$ response = file_get_contents ($ url. Http_build_query ($ query));
$ user = json_decode ($ response, true);

// USER ID
echo $ user ['contactID'];

Individual edit (status change)

Click here for detailed parameters

Change the status based on the user ID (contactID) obtained by the search.

// $ token ['accessToken'] was obtained by logging in
// Assume that the access token has been assigned

// $ user ['contactID'] was obtained by search
// Assume that the user ID has been assigned

$ url ='https://api.bme.jp/rest/1.0/contact/detail/update';
$ change_data = [
    'access_token' => $ token ['accessToken'],
    'contactID' => $ user ['contactID'],
    'status' =>'Unsubscribe' // Status after change
];;

// curl settings
$ ch = curl_init ();
curl_setopt ($ ch, CURLOPT_URL, $ url);
curl_setopt ($ ch, CURLOPT_CUSTOMREQUEST,'POST');
curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt ($ ch, CURLOPT_POSTFIELDS, http_build_query ($ change_data));
$ response = curl_exec ($ ch);

// Change failed
if (! (bool) $ response) {
    echo'Failed to change status';
}

$ response can be 0 (false) or 1 (true). If the logout is successful, true will be returned.

#Summary of the flow up to registration I will summarize the flow until registering an e-mail newsletter subscription. In the case of cancellation, it can be implemented with almost the same source.

As a flow1. Log in to blastmail and get an access token 2. Search for registered user’s email address 3. If not registered, new registration 4. If registered, change the status to “Delivery” 5. Log out of blastmail and discard the access token

It looks like this.

// blastmail login
$ url ='https://api.bme.jp/rest/1.0/authenticate/login';
$ login_data = [
    'username' =>'Login ID',
    'password' =>'login password',
    'api_key' =>'API Key',
    'f' =>'json' // Specify response in json format
];;

// curl settings
$ ch = curl_init ();
curl_setopt ($ ch, CURLOPT_URL, $ url); // Specify the URL to get
curl_setopt ($ ch, CURLOPT_CUSTOMREQUEST,'POST'); // Send POST
curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, true); // Return the execution result as a string
curl_setopt ($ ch, CURLOPT_POSTFIELDS, http_build_query ($ login_data)); // Value to POST
$ response = curl_exec ($ ch);
$ token = json_decode ($ response, true); // Convert response from json to array

if (! isset ($ token ['accessToken'])) {
    // Login failure error
}

// User search
$ url ='https://api.bme.jp/rest/1.0/contact/detail/search?';
$ query = [
    'access_token' => $ token ['accessToken'],
    'email' =>'Email address you want to register',
    'f' =>'json'
];;
$ response = file_get_contents ($ url. Http_build_query ($ query));
$ user = json_decode ($ response, true);

if (empty ($ user)) {
    // New registration if not registered
    $ url ='https://api.bme.jp/rest/1.0/contact/detail/create';
    $ regist_data = [
        'access_token' => $ token ['accessToken'],
        'c15' =>'Email address you want to register'
    ];;
    curl_setopt ($ ch, CURLOPT_URL, $ url);
    curl_setopt ($ ch, CURLOPT_POSTFIELDS, http_build_query ($ register_data));
    $ response = curl_exec ($ ch);
} else {
    // Change status if registered in the past
    $ url ='https://api.bme.jp/rest/1.0/contact/detail/update';
    $ regist_data = [
        'access_token' => $ token ['accessToken'],
        'contactID' => $ user ['contactID'],
        'status' =>'Delivery'
    ];;
    curl_setopt ($ ch, CURLOPT_URL, $ url);
    curl_setopt ($ ch, CURLOPT_POSTFIELDS, http_build_query ($ register_data));
    $ response = curl_exec ($ ch);
}
if (! (bool) $ response) {
    // Registration failure error
}

// Log out (destroy access token)
$ url ='https://api.bme.jp/rest/1.0/authenticate/logout?';
$ query = ['access_token' => $ token ['accessToken'],];
$ response = file_get_contents ($ url. Http_build_query ($ query));

Note

If you set the status to “Delete”, the user will be deleted from the administration screen, but it will probably remain in the database due to logical deletion. (Therefore, deleted users cannot be newly registered, so it is necessary to change the status)

that’s all!