-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathmessages.php
More file actions
106 lines (86 loc) · 2.91 KB
/
Copy pathmessages.php
File metadata and controls
106 lines (86 loc) · 2.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<?php
use Mailtrap\Config;
use Mailtrap\Helper\ResponseHelper;
use Mailtrap\MailtrapInboundClient;
use Symfony\Component\Mime\Address;
use Symfony\Component\Mime\Email;
require __DIR__ . '/../../vendor/autoload.php';
$config = new Config($_ENV['MAILTRAP_API_KEY']); #your API token from here https://mailtrap.io/api-tokens
$inboxId = (int) ($_ENV['MAILTRAP_INBOUND_INBOX_ID'] ?? 0);
$messageId = $_ENV['MAILTRAP_INBOUND_MESSAGE_ID'] ?? '';
// Messages are scoped to an inbox.
$messages = (new MailtrapInboundClient($config))->messages($inboxId);
/**
* List received messages. Pass the previous page's last id to paginate.
*
* GET https://mailtrap.io/api/inbound/inboxes/{inbox_id}/messages
*/
try {
$response = $messages->getList();
var_dump(ResponseHelper::toArray($response));
} catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
}
/**
* Get a single message with its body and attachment download URLs.
*
* GET https://mailtrap.io/api/inbound/inboxes/{inbox_id}/messages/{message_id}
*/
try {
$response = $messages->getById($messageId);
var_dump(ResponseHelper::toArray($response));
} catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
}
/**
* Reply to a message (sends a real email to the original sender).
* reply/replyAll/forward accept a Symfony Mime Email, just like the send API.
*
* POST https://mailtrap.io/api/inbound/inboxes/{inbox_id}/messages/{message_id}/reply
*/
try {
$email = (new Email())
->text('Thanks for reaching out!')
->html('<p>Thanks for reaching out!</p>');
$response = $messages->reply($messageId, $email);
var_dump(ResponseHelper::toArray($response));
} catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
}
/**
* Reply to a message and copy the original's other recipients.
*
* POST https://mailtrap.io/api/inbound/inboxes/{inbox_id}/messages/{message_id}/reply_all
*/
try {
$email = (new Email())->text('Looping everyone in.');
$response = $messages->replyAll($messageId, $email);
var_dump(ResponseHelper::toArray($response));
} catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
}
/**
* Forward a message to new recipients (at least one is required).
*
* POST https://mailtrap.io/api/inbound/inboxes/{inbox_id}/messages/{message_id}/forward
*/
try {
$email = (new Email())
->to(new Address('colleague@example.com'))
->text('Please take a look.');
$response = $messages->forward($messageId, $email);
var_dump(ResponseHelper::toArray($response));
} catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
}
/**
* Delete a message.
*
* DELETE https://mailtrap.io/api/inbound/inboxes/{inbox_id}/messages/{message_id}
*/
try {
$response = $messages->delete($messageId);
var_dump($response->getStatusCode());
} catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
}