curl -s --user user:password \
https://api.infobip.com/email/1/send \
-F from='Jane Doe <jane.doe@somecompany.com>' \
-F to='john.smith@somedomain.com' \
-F subject='Mail subject text' \
-F text='Mail body text' \
-F bulkId='cusotmBulkId'
POST /email/1/send HTTP/1.1
Host: api.infobip.com
Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==
Accept: application/json
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW
------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="from"
Jane Doe <jane.doe@somecompany.com>
------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="to"
john.smith@somedomain.com
------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="subject"
Mail subject text
------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="text"
Mail body text
------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="bulkId"
A_CUSTOM_BULK_ID
------WebKitFormBoundary7MA4YWxkTrZu0gW--
<?php
define('MULTIPART_BOUNDARY','-----------------------'.md5(time()));
define('EOL',"\r\n");// PHP_EOL cannot be used for emails we need the CRFL '\r\n'
/*
* Method to convert an associative array of parameters into the HTML body string
*/
function getBody($fields) {
$content = '';
foreach ($fields as $FORM_FIELD => $value) {
$content .= '--' . MULTIPART_BOUNDARY . EOL;
$content .= 'Content-Disposition: form-data; name="' . $FORM_FIELD . '"' . EOL;
$content .= EOL . $value . EOL;
}
return $content . '--' . MULTIPART_BOUNDARY . '--'; // Email body should end with "--"
}
/*
* Method to get the headers for a basic authentication with username and passowrd
*/
function getHeader($username, $password){
// basic Authentication
$auth = base64_encode("$username:$password");
// Define the header
return array("Authorization:Basic $auth", 'Content-Type: multipart/form-data ; boundary=' . MULTIPART_BOUNDARY );
}
// URL to the API that sends the email.
$url = 'https://api.infobip.com/email/1/send';
// Associate Array of the post parameters to be sent to the API
$postData = array(
'from' => 'company@somecompany.com',
'to' => 'john.smith@somedomain.com',
'subject' => 'Mail subject text',
'text' => 'Mail body text',
);
// Create the stream context.
$context = stream_context_create(array(
'http' => array(
'method' => 'POST',
'header' => getHeader('username', 'password'),
'content' => getBody($postData),
)
));
// Read the response using the Stream Context.
$response = file_get_contents($url, false, $context);
?>
/*----Sending single email----*/
var formData = new FormData();
formData.append('from', 'Example <from@example.com>');
formData.append('to', 'recipient@example.com');
formData.append('subject', 'Test subject');
formData.append('text', 'Sample Email Body');
var xhr = new XMLHttpRequest();
xhr.withCredentials = false;
xhr.addEventListener('readystatechange', function () {
if (this.readyState === this.DONE) {
console.log(this.responseText);
}
});
xhr.open('POST', 'https://api.infobip.com/email/1/send', false);
xhr.setRequestHeader('authorization', 'Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==');
xhr.setRequestHeader('accept', 'application/json');
xhr.send(formData);
ContentType PLAIN_UTF8 = ContentType.create("text/plain", StandardCharsets.UTF_8);
HttpClient httpClient = HttpClients.custom()
.setConnectionManager(new PoolingHttpClientConnectionManager())
.build();
HttpPost httpPost = new HttpPost("https://api.infobip.com/email/1/send");
httpPost.setHeader("authorization", "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==");
MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create();
entityBuilder.addPart("from", new StringBody("Jane Doe <jane.doe@somecompany.com>", PLAIN_UTF8));
entityBuilder.addPart("to", new StringBody("john.smith@somedomain.com", PLAIN_UTF8));
entityBuilder.addPart("subject", new StringBody("Mail subject text", PLAIN_UTF8));
entityBuilder.addPart("text", new StringBody("Mail body text", PLAIN_UTF8));
httpPost.setEntity(entityBuilder.build());
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpClient client = new HttpClient();
client.BaseAddress = new Uri("https://api.infobip.com/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", "QWxhZGRpbjpvcGVuIHNlc2FtZQ==");
var request = new MultipartFormDataContent();
request.Add(new StringContent("Jane Doe <jane.doe@somecompany.com>"), "from");
request.Add(new StringContent("john.smith@somedomain.com"), "to");
request.Add(new StringContent("Mail subject text"), "subject");
request.Add(new StringContent("Rich HTML message body."), "text");
var response = client.PostAsync("email/1/send", request).Result;
if (response.IsSuccessStatusCode) {
var responseContent = response.Content;
string responseString = responseContent.ReadAsStringAsync().Result;
Console.WriteLine(responseString);
}
{
"bulkId":"cusotmBulkId",
"messages": [
{
"to": "john.smith@somedomain.com",
"messageCount": 1,
"messageId": "c268350e-c85e-41d1-b5a0-a60771b134bd",
"status": {
"groupId": 1,
"groupName": "PENDING",
"id": 7,
"name": "PENDING_ENROUTE",
"description": "Message sent to next instance"
}
}
]
}