-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathbulk.php
More file actions
44 lines (37 loc) · 1.8 KB
/
bulk.php
File metadata and controls
44 lines (37 loc) · 1.8 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
<?php
use Mailtrap\Helper\ResponseHelper;
use Mailtrap\MailtrapClient;
use Mailtrap\Mime\MailtrapEmail;
use Symfony\Component\Mime\Address;
require __DIR__ . '/../../vendor/autoload.php';
/**
* Email Batch Sending API (Bulk)
*
* Batch send email (text, html, text&html, templates).
*
* Please note that the endpoint will return a 200-level http status, even when sending for individual messages may fail.
* Users of this endpoint should check the success and errors for each message in the response (the results are ordered the same as the original messages - requests).
* Please note that the endpoint accepts up to 500 messages per API call, and up to 50 MB payload size, including attachments.
*
* For this example, you need to have ready-to-use sending domain or, a Demo domain that allows sending emails to your own account email.
* @see https://help.mailtrap.io/article/69-sending-domain-setup
*/
try {
$mailtrap = MailtrapClient::initSendingEmails(
apiKey: $_ENV['MAILTRAP_API_KEY'], // Your API token from https://mailtrap.io/api-tokens
isBulk: true // Enable bulk sending
);
$baseEmail = (new MailtrapEmail())
->from(new Address('example@YOUR-DOMAIN-HERE.com', 'Mailtrap Test')) // Use your domain installed in Mailtrap
->subject('Batch Email Subject')
->text('Batch email text')
->html('<p>Batch email text</p>');
$recipientEmails = [
(new MailtrapEmail())->to(new Address('recipient1@example.com', 'Recipient 1')),
(new MailtrapEmail())->to(new Address('recipient2@example.com', 'Recipient 2')),
];
$response = $mailtrap->batchSend($recipientEmails, $baseEmail);
var_dump(ResponseHelper::toArray($response)); // Output response body as array
} catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
}