-
Notifications
You must be signed in to change notification settings - Fork 8
[3.4.0] Add Batch sending functionality (transactional, bulk and sandbox) #42
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
gaalferov
wants to merge
11
commits into
main
Choose a base branch
from
feature/batch-send
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+934
−12
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
3a215db
Update Readme with functionality and prerequisites
yanchuk 5bcd1b3
fix type
yanchuk 5643337
add batch sending functionality for emails
gaalferov 67408f9
add tests for batch sending
gaalferov a621276
add examples
gaalferov 1b53702
add examples for Symfony & Laravel
gaalferov c85bc90
Merge pull request #40 from railsware/readme-update
gaalferov 1ec7d61
fix examples
gaalferov 87f139a
fix laravel example
gaalferov 28ea71a
refactor batchSend method to accept recipientEmails first and make ba…
gaalferov 071b08c
update release date for version 3.4.0 in CHANGELOG.md
gaalferov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,41 @@ | ||
Official Mailtrap PHP client | ||
=============== | ||
# Mailtrap PHP client - Official | ||
|
||
 | ||
 | ||
|
||
[](https://packagist.org/packages/railsware/mailtrap-php) | ||
[](https://packagist.org/packages/railsware/mailtrap-php) | ||
[](https://packagist.org/packages/railsware/mailtrap-php) | ||
|
||
## Prerequisites | ||
|
||
To get the most of this official Mailtrap.io PHP SDK: | ||
- [Create a Mailtrap account](https://mailtrap.io/signup) | ||
- [Verify your domain](https://mailtrap.io/sending/domains) | ||
|
||
## Supported functionality | ||
|
||
It supports Symphony and Laravel integrations. | ||
|
||
Currently with this SDK you can: | ||
- Email API/SMTP | ||
- Send an email (Transactional and Bulk streams) | ||
- Send an email with a Template | ||
- Send a batch of emails (Transactional and Bulk streams) | ||
- Email Sandbox | ||
- Send an email | ||
- Send an email with a template | ||
- Send a batch of emails | ||
- Message management | ||
- Inbox management | ||
- Project management | ||
- Contact management | ||
- Contacts CRUD | ||
- Lists CRUD | ||
- General | ||
- Templates CRUD | ||
- Suppressions management (find and delete) | ||
|
||
|
||
## Installation | ||
You can install the package via [composer](http://getcomposer.org/) | ||
|
@@ -56,13 +85,13 @@ $email = (new MailtrapEmail()) | |
->addCc('[email protected]') | ||
->bcc('[email protected]') | ||
->subject('Best practices of building HTML emails') | ||
->text('Hey! Learn the best practices of building HTML emails and play with ready-to-go templates. Mailtrap’s Guide on How to Build HTML Email is live on our blog') | ||
->text('Hey! Learn the best practices of building HTML emails and play with ready-to-go templates. Mailtrap's Guide on How to Build HTML Email is live on our blog') | ||
->html( | ||
'<html> | ||
<body> | ||
<p><br>Hey</br> | ||
Learn the best practices of building HTML emails and play with ready-to-go templates.</p> | ||
<p><a href="https://mailtrap.io/blog/build-html-email/">Mailtrap’s Guide on How to Build HTML Email</a> is live on our blog</p> | ||
<p><a href="https://mailtrap.io/blog/build-html-email/">Mailtrap's Guide on How to Build HTML Email</a> is live on our blog</p> | ||
<img src="cid:logo"> | ||
</body> | ||
</html>' | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -226,3 +226,111 @@ | |
} catch (Exception $e) { | ||
echo 'Caught exception: ', $e->getMessage(), "\n"; | ||
} | ||
|
||
|
||
/********************************************************************************************************************** | ||
******************************************* EMAIL BATCH SENDING ******************************************************* | ||
********************************************************************************************************************** | ||
*/ | ||
|
||
/** | ||
* Email Batch Sending API (Transactional OR 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. | ||
*/ | ||
try { | ||
// Choose either Transactional API or Bulk API | ||
// For Transactional API | ||
$mailtrap = MailtrapClient::initSendingEmails( | ||
apiKey: getenv('MAILTRAP_API_KEY'), // Your API token from https://mailtrap.io/api-tokens | ||
); | ||
|
||
// OR for Bulk API (uncomment the line below and comment out the transactional initialization) | ||
// $mailtrap = MailtrapClient::initSendingEmails( | ||
// apiKey: getenv('MAILTRAP_API_KEY'), // Your API token from https://mailtrap.io/api-tokens | ||
// isBulk: true // Enable bulk sending | ||
//); | ||
|
||
$baseEmail = (new MailtrapEmail()) | ||
->from(new Address('[email protected]', '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('[email protected]', 'Recipient 1')), | ||
(new MailtrapEmail())->to(new Address('[email protected]', '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"; | ||
} | ||
|
||
|
||
/** | ||
* Email Batch Sending WITH TEMPLATE (Transactional OR Bulk) | ||
* | ||
* WARNING! If a template is provided, then subject, text, html, category and other params are forbidden. | ||
* | ||
* UUID of email template. Subject, text and html will be generated from template using optional template_variables. | ||
* Optional template variables that will be used to generate actual subject, text and html from email template | ||
*/ | ||
try { | ||
// Choose either Transactional API or Bulk API | ||
// For Transactional API | ||
$mailtrap = MailtrapClient::initSendingEmails( | ||
apiKey: getenv('MAILTRAP_API_KEY'), // Your API token from https://mailtrap.io/api-tokens | ||
); | ||
|
||
// OR for Bulk API (uncomment the line below and comment out the transactional initialization) | ||
// $mailtrap = MailtrapClient::initSendingEmails( | ||
// apiKey: getenv('MAILTRAP_API_KEY'), // Your API token from https://mailtrap.io/api-tokens | ||
// isBulk: true // Enable bulk sending | ||
//); | ||
|
||
$baseEmail = (new MailtrapEmail()) | ||
->from(new Address('[email protected]', 'Mailtrap Test')) // Use your domain installed in Mailtrap | ||
->templateUuid('bfa432fd-0000-0000-0000-8493da283a69') // Template UUID | ||
->templateVariables([ | ||
'user_name' => 'Jon Bush', | ||
'next_step_link' => 'https://mailtrap.io/', | ||
'get_started_link' => 'https://mailtrap.io/', | ||
'company' => [ | ||
'name' => 'Best Company', | ||
'address' => 'Its Address', | ||
], | ||
'products' => [ | ||
[ | ||
'name' => 'Product 1', | ||
'price' => 100, | ||
], | ||
[ | ||
'name' => 'Product 2', | ||
'price' => 200, | ||
], | ||
], | ||
]); | ||
|
||
$recipientEmails = [ | ||
(new MailtrapEmail()) | ||
->to(new Address('[email protected]', 'Recipient 1')) | ||
// Optional: Override template variables for this recipient | ||
->templateVariables([ | ||
'user_name' => 'Custom User 1', | ||
]), | ||
(new MailtrapEmail()) | ||
->to(new Address('[email protected]', '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"; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -124,3 +124,98 @@ | |
} catch (Exception $e) { | ||
echo 'Caught exception: ', $e->getMessage(), "\n"; | ||
} | ||
|
||
/********************************************************************************************************************** | ||
******************************************* EMAIL BATCH SENDING ******************************************************* | ||
********************************************************************************************************************** | ||
*/ | ||
|
||
/** | ||
* Test Email Batch | ||
* | ||
* 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. | ||
*/ | ||
try { | ||
$mailtrap = MailtrapClient::initSendingEmails( | ||
apiKey: getenv('MAILTRAP_API_KEY'), #your API token from here https://mailtrap.io/api-tokens | ||
isSandbox: true, # Sandbox sending (@see https://help.mailtrap.io/article/109-getting-started-with-mailtrap-email-testing) | ||
inboxId: getenv('MAILTRAP_INBOX_ID') # required param for sandbox sending | ||
); | ||
|
||
$baseEmail = (new MailtrapEmail()) | ||
->from(new Address('[email protected]', '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('[email protected]', 'Recipient 1')), | ||
(new MailtrapEmail())->to(new Address('[email protected]', '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"; | ||
} | ||
|
||
|
||
/** | ||
* Test Email Batch WITH TEMPLATE | ||
* | ||
* WARNING! If a template is provided, then subject, text, html, category and other params are forbidden. | ||
* | ||
* UUID of email template. Subject, text and html will be generated from template using optional template_variables. | ||
* Optional template variables that will be used to generate actual subject, text and html from email template | ||
*/ | ||
try { | ||
$mailtrap = MailtrapClient::initSendingEmails( | ||
apiKey: getenv('MAILTRAP_API_KEY'), #your API token from here https://mailtrap.io/api-tokens | ||
isSandbox: true, # Sandbox sending (@see https://help.mailtrap.io/article/109-getting-started-with-mailtrap-email-testing) | ||
inboxId: getenv('MAILTRAP_INBOX_ID') # required param for sandbox sending | ||
); | ||
|
||
$baseEmail = (new MailtrapEmail()) | ||
->from(new Address('[email protected]', 'Mailtrap Test')) // Use your domain installed in Mailtrap | ||
->templateUuid('bfa432fd-0000-0000-0000-8493da283a69') // Template UUID | ||
->templateVariables([ | ||
'user_name' => 'Jon Bush', | ||
'next_step_link' => 'https://mailtrap.io/', | ||
'get_started_link' => 'https://mailtrap.io/', | ||
'company' => [ | ||
'name' => 'Best Company', | ||
'address' => 'Its Address', | ||
], | ||
'products' => [ | ||
[ | ||
'name' => 'Product 1', | ||
'price' => 100, | ||
], | ||
[ | ||
'name' => 'Product 2', | ||
'price' => 200, | ||
], | ||
], | ||
]); | ||
|
||
$recipientEmails = [ | ||
(new MailtrapEmail()) | ||
->to(new Address('[email protected]', 'Recipient 1')) | ||
// Optional: Override template variables for this recipient | ||
->templateVariables([ | ||
'user_name' => 'Custom User 1', | ||
]), | ||
(new MailtrapEmail()) | ||
->to(new Address('[email protected]', '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"; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.