Identifiers
These are boxes in e-mail’s header allowing for e-mail message identification, e.g. its origin, date of send out etc. However these types of boxes can be created and used alone for specific data sending. Message-id is one of these boxes. In the example below we will show how you can use this box for your own needs and include data on e.g. what transaction in clearing system did a given message apply to.
What is Message-Id box?
It is a box holding a unique message number from a given server <message_id@domain.com> determined by sender’s server. Thanks to this we can see what is happening to our message during specific stages of delivery to the end user.
How to build custom Message-Id?
First of all we must take care of making our message id always unique. Moreover similar to how an e-mail address had a assigned domain: message_id@domain.com, additionally our unique number must be added between <> symbols, like here: <message_id@domain.com>. In order to learn more read about RFC standards.
A little bit of practice
Message-Id box apart form identification can also server other purposes. Think about what would we like to communicate in Message-Id box. Let’s assume that we will be sending a message to an online shop’s client who just purchased something. Basis for our unique client will be: transaction number (oid), user number (uid) and message’s send out time (time).
1 2 3 4 5 |
{ "oid":"1234567", "uid":"87654321", "time":1417176584 } |
Naturally sending messages in such form is pointless, because it is highly possible that such identifier will be rejected by a server. The messages can be encoded using Base64. As a result we will receive the following character string:
1 |
eyJvaWQiOiIxMjM0NTY3IiwidWlkIjoiODc2NTQzMjEiLCJ0aW1lIjoxNDE3NTA0NTEwfQ== |
Having such character string we can start sending messages. Let’s write an easy script that will verify a send out with our custom message id number:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
//PHPMailer libraries for easier send out require_once( dirname(__FILE__).'/../protected/commands/PHPMailer/class.phpmailer.php' ); require_once( dirname(__FILE__).'/../protected/commands/PHPMailer/class.smtp.php' ); //Data for Message-Id creation $data = array( 'oid' => '1234567', 'uid' => '87654321', 'date' => date('Y-m-d'), 'sent' => time(), ); //Converting a board to JSON $json = json_encode( $data ); //Converting JSON to Base64 $messageId = base64_encode( $json ); //PHPMailer library initialization $mail = new PHPMailer(); //Setting SMTP connection $mail->IsSMTP(); $mail->SMTPAuth = true; $mail->Host = 'smtp.emaillabs.net.pl'; $mail->Port = '587'; $mail->Username = 'login.to.smtp.account'; $mail->Password = 'password.to.smtp.account'; $mail->SMTPSecure = 'tls'; //Setting coding and sender $mail->CharSet = 'utf-8'; $mail->From = 'email-address@sender.com'; //Setting receiver $mail->AddAddress('email-address@receiver.com'); //Setting Message-Id $mail->MessageID = '<'.$messageId.'@emaillabs.pl>'; //After @ we are adding sender’s server. We also have to add < > symbols as show in the example //Setting message subject and content $mail->Subject = 'Message-Id test message'; $mail->Body = 'Hey, this is a message-id test message'; //Message send out $mail->Send(); |
After a correct send out we are checking an inbox:
As can be seen Message-Id was accepted by a server (Gmail in this case). We can find out whether Message-Id already exists in a system, e.g. by using API. In order to start working with API read this article. Let’s write a short script that will do it for us and display results:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
//CURL library initialization $curl = curl_init(); //Setting an address from which data will be retreived $url = "https://api.emaillabs.net.pl/api/emails"; //Setting App Key $appkey = 'your-app-key'; //Setting Secret Key $secret = 'your-secret-key'; //Creating retriving criteria $data = array( //Here we are retriving messages with their message-is 'filter' => '"message_id::eyJvaWQiOiIxMjM0NTY3IiwidWlkIjoiODc2NTQzMjEiLCJ0aW1lIjoxNDE3NTA0NTEwfQ==@emaillabs.pl"', 'offset' => 0, 'limit' => 1, 'sort' => '-created_at', ); //Passing retreving criteria to URL address $url = sprintf("%s?%s", $url, http_build_query($data)); //Setting authorization type curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); //Passing login data curl_setopt($curl, CURLOPT_USERPWD , "$appkey:$secret"); //Passing URL address with action curl_setopt($curl, CURLOPT_URL, $url); //Server return settings curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); //Retriving result $result = curl_exec($curl); //Processing data from API $json = json_decode( $result ); echo '<pre>'; print_r( $json ); |
And here is the result:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
stdClass Object ( [code] => 200 [status] => success [message] => [data] => Array ( [0] => stdClass Object ( [vps] => smtp2-03 [account] => 1.test.smtp [injected_time] => 2014-12-02 08:15:12 [message_id] => eyJvaWQiOiIxMjM0NTY3IiwidWlkIjoiODc2NTQzMjEiLCJ0aW1lIjoxNDE3NTA0NTEwfQ==@emaillabs.pl [ok_desc] => 250 2.0.0 OK 1417504512 kn5si33697578wjb.116 - gsmtp [ok_time] => 2014-12-02 08:15:12 [open_desc] => [open_time] => [postfix_id] => Array ( [0] => 3jsF266HBKz9f50k [1] => 3jsF2650sWz8jd4K ) [tracking] => Array ( ) [tags] => Array ( ) [to] => receiver_email@gmail.com [uid] => 8ba341fe29d5dad8bd683a4cef2dc7d9 [from] => Root User [subject] => Generated Message-Id 08:15:10 [created_at] => [updated_at] => [id] => 547d67aae1e68a470e43bbe8 ) ) ) |
Next by using only one line of code we can decode out Message-Id to its original form:
1 |
json_decode( base64_decode( str_replace('@emaillabs.pl', '', $json->data[0]->message_id) ) ) |
As a result we get the following:
1 2 3 4 5 6 |
stdClass Object ( [oid] => 1234567 [uid] => 87654321 [time] => 1417504510 ) |