2016-12-07 23:29:08 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (C) 2013-2016 Mailgun
|
|
|
|
*
|
|
|
|
* This software may be modified and distributed under the terms
|
|
|
|
* of the MIT license. See the LICENSE file for details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Mailgun\Api;
|
|
|
|
|
|
|
|
use Mailgun\Assert;
|
|
|
|
use Mailgun\Exception\InvalidArgumentException;
|
2017-02-23 19:56:30 +00:00
|
|
|
use Mailgun\Model\Message\SendResponse;
|
|
|
|
use Mailgun\Model\Message\ShowResponse;
|
2016-12-07 23:29:08 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
|
|
|
|
*/
|
|
|
|
class Message extends HttpApi
|
|
|
|
{
|
|
|
|
/**
|
2017-04-14 10:31:13 +02:00
|
|
|
* @param string $domain
|
|
|
|
* @param array $params
|
2016-12-07 23:29:08 +01:00
|
|
|
*
|
|
|
|
* @return SendResponse
|
|
|
|
*/
|
|
|
|
public function send($domain, array $params)
|
|
|
|
{
|
2017-04-14 10:31:13 +02:00
|
|
|
Assert::string($domain);
|
2016-12-07 23:29:08 +01:00
|
|
|
Assert::notEmpty($domain);
|
|
|
|
Assert::notEmpty($params);
|
|
|
|
|
|
|
|
$postDataMultipart = [];
|
2017-04-14 10:31:13 +02:00
|
|
|
$fields = ['attachment', 'inline'];
|
2016-12-07 23:29:08 +01:00
|
|
|
foreach ($fields as $fieldName) {
|
|
|
|
if (!isset($params[$fieldName])) {
|
|
|
|
continue;
|
|
|
|
}
|
2017-04-14 10:31:13 +02:00
|
|
|
|
|
|
|
Assert::isArray($params[$fieldName]);
|
|
|
|
foreach ($params[$fieldName] as $file) {
|
|
|
|
$postDataMultipart[] = $this->prepareFile($fieldName, $file);
|
2016-12-07 23:29:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
unset($params[$fieldName]);
|
|
|
|
}
|
|
|
|
|
2017-04-14 10:31:13 +02:00
|
|
|
$postDataMultipart = array_merge($this->prepareMultipartParameters($params), $postDataMultipart);
|
2016-12-07 23:29:08 +01:00
|
|
|
$response = $this->httpPostRaw(sprintf('/v3/%s/messages', $domain), $postDataMultipart);
|
|
|
|
|
2017-03-26 10:16:36 +02:00
|
|
|
return $this->hydrateResponse($response, SendResponse::class);
|
2016-12-07 23:29:08 +01:00
|
|
|
}
|
|
|
|
|
2017-04-14 10:31:13 +02:00
|
|
|
/**
|
|
|
|
* @param string $domain
|
|
|
|
* @param array $recipients with all you send emails to. Including bcc and cc
|
|
|
|
* @param string $message Message filepath or content
|
|
|
|
* @param array $params
|
|
|
|
*/
|
|
|
|
public function sendMime($domain, array $recipients, $message, array $params)
|
|
|
|
{
|
|
|
|
Assert::string($domain);
|
|
|
|
Assert::notEmpty($domain);
|
|
|
|
Assert::notEmpty($recipients);
|
|
|
|
Assert::notEmpty($message);
|
|
|
|
Assert::nullOrIsArray($params);
|
|
|
|
|
|
|
|
$params['to'] = $recipients;
|
|
|
|
$postDataMultipart = $this->prepareMultipartParameters($params);
|
|
|
|
|
|
|
|
if (is_file($message)) {
|
|
|
|
$fileData = ['filePath' => $message];
|
|
|
|
} else {
|
|
|
|
$fileData = [
|
|
|
|
'fileContent' => $message,
|
|
|
|
'filename' => 'message',
|
|
|
|
];
|
|
|
|
}
|
|
|
|
$postDataMultipart[] = $this->prepareFile('message', $fileData);
|
|
|
|
$response = $this->httpPostRaw(sprintf('/v3/%s/messages.mime', $domain), $postDataMultipart);
|
|
|
|
|
|
|
|
return $this->hydrateResponse($response, SendResponse::class);
|
|
|
|
}
|
|
|
|
|
2016-12-07 23:29:08 +01:00
|
|
|
/**
|
|
|
|
* Get stored message.
|
|
|
|
*
|
|
|
|
* @param string $url
|
2017-04-14 10:31:13 +02:00
|
|
|
* @param bool $rawMessage if true we will use "Accept: message/rfc2822" header
|
2016-12-07 23:29:08 +01:00
|
|
|
*
|
|
|
|
* @return ShowResponse
|
|
|
|
*/
|
|
|
|
public function show($url, $rawMessage = false)
|
|
|
|
{
|
|
|
|
Assert::notEmpty($url);
|
|
|
|
|
|
|
|
$headers = [];
|
|
|
|
if ($rawMessage) {
|
|
|
|
$headers['Accept'] = 'message/rfc2822';
|
|
|
|
}
|
|
|
|
|
|
|
|
$response = $this->httpGet($url, [], $headers);
|
|
|
|
|
2017-03-26 10:16:36 +02:00
|
|
|
return $this->hydrateResponse($response, ShowResponse::class);
|
2016-12-07 23:29:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Prepare a file.
|
|
|
|
*
|
|
|
|
* @param string $fieldName
|
|
|
|
* @param array $filePath array('fileContent' => 'content') or array('filePath' => '/foo/bar')
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*
|
|
|
|
* @throws InvalidArgumentException
|
|
|
|
*/
|
2017-04-08 01:24:37 +02:00
|
|
|
private function prepareFile($fieldName, array $filePath)
|
2016-12-07 23:29:08 +01:00
|
|
|
{
|
|
|
|
$filename = isset($filePath['filename']) ? $filePath['filename'] : null;
|
|
|
|
|
|
|
|
if (isset($filePath['fileContent'])) {
|
|
|
|
// File from memory
|
|
|
|
$resource = fopen('php://temp', 'r+');
|
|
|
|
fwrite($resource, $filePath['fileContent']);
|
|
|
|
rewind($resource);
|
|
|
|
} elseif (isset($filePath['filePath'])) {
|
|
|
|
// File form path
|
|
|
|
$path = $filePath['filePath'];
|
|
|
|
|
|
|
|
// Remove leading @ symbol
|
|
|
|
if (strpos($path, '@') === 0) {
|
|
|
|
$path = substr($path, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
$resource = fopen($path, 'r');
|
|
|
|
} else {
|
|
|
|
throw new InvalidArgumentException('When using a file you need to specify parameter "fileContent" or "filePath"');
|
|
|
|
}
|
|
|
|
|
|
|
|
return [
|
|
|
|
'name' => $fieldName,
|
|
|
|
'content' => $resource,
|
|
|
|
'filename' => $filename,
|
|
|
|
];
|
|
|
|
}
|
2017-04-14 10:31:13 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Prepare multipart parameters. Make sure each POST parameter is splitted into an array with 'name' and 'content' keys.
|
|
|
|
*
|
|
|
|
* @param array $params
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
private function prepareMultipartParameters(array $params)
|
|
|
|
{
|
|
|
|
$postDataMultipart = [];
|
|
|
|
foreach ($params as $key => $value) {
|
|
|
|
// If $value is not an array we cast it to an array
|
|
|
|
foreach ((array) $value as $subValue) {
|
|
|
|
$postDataMultipart[] = [
|
|
|
|
'name' => $key,
|
|
|
|
'content' => $subValue,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $postDataMultipart;
|
|
|
|
}
|
2016-12-07 23:29:08 +01:00
|
|
|
}
|