Problem
How do I send an email using the outMail API in PHP?
How do I send an email in PHP with a RESTful API?
Solution
The following example of code assumes you have already got a fully functional webserver and you have working knowledge of PHP and the cURL Libraries.
Example code outmail-send.php
<?php
/**
* outmail-send.php
*
* This example we will send an email using the outMail API.
*/
$url = 'https://api.smtp-engine.com/outmail/v1/email/send';
$data = array(
"api_key" => "YOUR_SECRET_API_KEY",
"to" => array("someone@example.com", "someone-else@example.com"),
"sender" => "me@example.com",
"subject" => "Sending an email via Outmail API",
"text_body" => "This is the text message body",
"html_body" => "<body><h1>This is the message</h1></body>"
);
$data_string = json_encode($data);
$ch=curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json', 'Content-Type: application/json'));
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_HEADER, true);
$result = curl_exec($ch);
curl_close($ch);
Summary of server details
Outgoing server |
mxXXXXXX.smtp-engine.com As provided in your signup email. |
Outgoing server protocol |
SMTP |
Outgoing server port |
25, 465, 587, 2525 or 8025 |
Authentication Type |
Basic Authentication, SSL and TLS supported |
Username |
As provided |
Password |
As provided |