2016-12-07 23:29:08 +01:00
|
|
|
<?php
|
|
|
|
|
2019-01-09 20:32:09 +01:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2016-12-07 23:29:08 +01:00
|
|
|
/*
|
2017-11-22 00:37:04 -08:00
|
|
|
* Copyright (C) 2013 Mailgun
|
2016-12-07 23:29:08 +01:00
|
|
|
*
|
|
|
|
* 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;
|
2018-08-04 21:30:15 +02:00
|
|
|
use Mailgun\Message\BatchMessage;
|
2017-02-23 19:56:30 +00:00
|
|
|
use Mailgun\Model\Message\SendResponse;
|
|
|
|
use Mailgun\Model\Message\ShowResponse;
|
2019-01-09 20:18:58 +01:00
|
|
|
use Psr\Http\Message\ResponseInterface;
|
2016-12-07 23:29:08 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
|
|
|
|
*/
|
|
|
|
class Message extends HttpApi
|
|
|
|
{
|
2019-01-09 20:18:58 +01:00
|
|
|
public function getBatchMessage(string $domain, bool $autoSend = true): BatchMessage
|
2018-08-04 21:30:15 +02:00
|
|
|
{
|
|
|
|
return new BatchMessage($this, $domain, $autoSend);
|
|
|
|
}
|
|
|
|
|
2016-12-07 23:29:08 +01:00
|
|
|
/**
|
2019-01-09 20:18:58 +01:00
|
|
|
* @return SendResponse|ResponseInterface
|
2016-12-07 23:29:08 +01:00
|
|
|
*/
|
2019-01-09 20:18:58 +01:00
|
|
|
public function send(string $domain, array $params)
|
2016-12-07 23:29:08 +01:00
|
|
|
{
|
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);
|
2019-01-05 08:23:41 +01:00
|
|
|
$this->closeResources($postDataMultipart);
|
2016-12-07 23:29:08 +01:00
|
|
|
|
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 array $recipients with all you send emails to. Including bcc and cc
|
|
|
|
* @param string $message Message filepath or content
|
2019-01-09 20:18:58 +01:00
|
|
|
*
|
|
|
|
* @return SendResponse|ResponseInterface
|
2017-04-14 10:31:13 +02:00
|
|
|
*/
|
2019-01-09 20:18:58 +01:00
|
|
|
public function sendMime(string $domain, array $recipients, string $message, array $params)
|
2017-04-14 10:31:13 +02:00
|
|
|
{
|
|
|
|
Assert::string($domain);
|
|
|
|
Assert::notEmpty($domain);
|
|
|
|
Assert::notEmpty($recipients);
|
|
|
|
Assert::notEmpty($message);
|
|
|
|
Assert::nullOrIsArray($params);
|
|
|
|
|
|
|
|
$params['to'] = $recipients;
|
|
|
|
$postDataMultipart = $this->prepareMultipartParameters($params);
|
|
|
|
|
2019-01-05 08:44:50 -03:00
|
|
|
if (strlen($message) < PHP_MAXPATHLEN && is_file($message)) {
|
2017-04-14 10:31:13 +02:00
|
|
|
$fileData = ['filePath' => $message];
|
|
|
|
} else {
|
|
|
|
$fileData = [
|
|
|
|
'fileContent' => $message,
|
|
|
|
'filename' => 'message',
|
|
|
|
];
|
|
|
|
}
|
|
|
|
$postDataMultipart[] = $this->prepareFile('message', $fileData);
|
|
|
|
$response = $this->httpPostRaw(sprintf('/v3/%s/messages.mime', $domain), $postDataMultipart);
|
2019-01-05 08:23:41 +01:00
|
|
|
$this->closeResources($postDataMultipart);
|
2017-04-14 10:31:13 +02:00
|
|
|
|
|
|
|
return $this->hydrateResponse($response, SendResponse::class);
|
|
|
|
}
|
|
|
|
|
2016-12-07 23:29:08 +01:00
|
|
|
/**
|
|
|
|
* Get stored message.
|
|
|
|
*
|
2019-01-09 20:32:09 +01:00
|
|
|
* @param bool $rawMessage if true we will use "Accept: message/rfc2822" header
|
2016-12-07 23:29:08 +01:00
|
|
|
*
|
2019-01-09 20:18:58 +01:00
|
|
|
* @return ShowResponse|ResponseInterface
|
2016-12-07 23:29:08 +01:00
|
|
|
*/
|
2019-01-09 20:18:58 +01:00
|
|
|
public function show(string $url, bool $rawMessage = false)
|
2016-12-07 23:29:08 +01:00
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-01-09 20:18:58 +01:00
|
|
|
* @param array $filePath array('fileContent' => 'content') or array('filePath' => '/foo/bar')
|
2016-12-07 23:29:08 +01:00
|
|
|
*
|
|
|
|
* @throws InvalidArgumentException
|
|
|
|
*/
|
2019-01-09 20:18:58 +01:00
|
|
|
private function prepareFile(string $fieldName, array $filePath): array
|
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
|
2017-11-22 00:37:04 -08:00
|
|
|
if (0 === strpos($path, '@')) {
|
2016-12-07 23:29:08 +01:00
|
|
|
$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
|
|
|
|
|
|
|
/**
|
2017-11-22 03:37:38 -05:00
|
|
|
* Prepare multipart parameters. Make sure each POST parameter is split into an array with 'name' and 'content' keys.
|
2017-04-14 10:31:13 +02:00
|
|
|
*/
|
2019-01-09 20:18:58 +01:00
|
|
|
private function prepareMultipartParameters(array $params): array
|
2017-04-14 10:31:13 +02:00
|
|
|
{
|
|
|
|
$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;
|
|
|
|
}
|
2019-01-05 08:23:41 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Close open resources.
|
|
|
|
*/
|
2019-01-09 20:18:58 +01:00
|
|
|
private function closeResources(array $params): void
|
2019-01-05 08:23:41 +01:00
|
|
|
{
|
|
|
|
foreach ($params as $param) {
|
|
|
|
if (is_array($param) && array_key_exists('content', $param) && is_resource($param['content'])) {
|
|
|
|
fclose($param['content']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-12-07 23:29:08 +01:00
|
|
|
}
|