Skip to content

Commit

Permalink
Adding deleteMessage method to delete single messages
Browse files Browse the repository at this point in the history
  • Loading branch information
David Schneider committed Jun 28, 2016
1 parent 5b864c4 commit 09bbafb
Showing 1 changed file with 95 additions and 25 deletions.
120 changes: 95 additions & 25 deletions src/Mailtrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,29 @@

class Mailtrapper {

/**
* Token to authorize access
* @var string
*/
protected $token;

/**
* URL of the Mailtrap API
* @var string
*/
protected $url = 'https://mailtrap.io/api/v1/';

/**
* Desired format of response, accepted values are 'json' and 'xml'
* @var string
*/
protected $format = '';

/**
* Mailtrapper constructor
* @param $api_token
* @param bool $format
*/
public function __construct($api_token, $format = false)
{
$this->token=$api_token;
Expand All @@ -16,45 +35,96 @@ public function __construct($api_token, $format = false)
}
}

/**
* Get list of inboxes
* @return string
*/
public function getInboxes()
{
$url = $this->buildUrl('inboxes');
return $this->process($url);
}

/**
* Get mails by inbox ID
* @param $inbox_id
* @return string
*/
public function getMails($inbox_id)
{
$path = 'inboxes/'.$inbox_id.'/messages';
$url = $this->buildUrl($path);
return $this->process($url);
}

public function clearInbox($inbox_id){
$path = 'inboxes/'.$inbox_id.'/clean';
$url = $this->buildUrl($path);
$context = stream_context_create(array('http'=>array(
'method'=>"PATCH",
'header' => 'Api-Token: '.$this->token)));
return $this->process($url, $context);
}

/**
* Delete all mails by inbox ID
* @param $inbox_id
* @return string
*/
public function clearInbox($inbox_id)
{
$path = 'inboxes/'.$inbox_id.'/clean';
$url = $this->buildUrl($path);
$context = stream_context_create([
'http'=>[
'method'=>"PATCH",
'header' => 'Api-Token: '.$this->token
]
]);
return $this->process($url, $context);
}

/**
* Delete mail by inbox ID and mail ID
* @param $inbox_id
* @param $message_id
* @return string
*/
public function deleteMessage($inbox_id, $message_id)
{
$path = 'inboxes/'.$inbox_id.'/messages/'.$message_id;
$url = $this->buildUrl($path);
$context = stream_context_create([
'http'=>[
'method'=>"DELETE",
'header' => 'Api-Token: '.$this->token
]
]);
return $this->process($url, $context);
}

/**
* Build an URL to send request to
* @param $path
* @return string
*/
protected function buildUrl($path)
{
return $this->url
. $path
. $this->format;
}
{
return $this->url
. $path
. $this->format;
}

/**
* Send request to Mailtrap and return response
* @param $url
* @param null $context
* @return string
*/
protected function process($url, $context = null)
{
if ($context==null)
$context = stream_context_create(array('http'=>array(
'method'=>"GET",
'header' => 'Api-Token: '.$this->token)));
return json_decode(
file_get_contents($url, null, $context),
true
);
}

{
if ($context==null)
{
$context = stream_context_create([
'http'=>[
'method'=>"GET",
'header' => 'Api-Token: '.$this->token
]
]);
}
return file_get_contents($url, null, $context);
}

}

1 comment on commit 09bbafb

@crazyinventor
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This also breaks stuff, because the return value from the process method is not an array anymore but plain text. Otherwise it would break when choosing xml as response format in the constructor.

Please sign in to comment.