Some more advanced usages of our SDK.
Disclaimer: As the SDK is still in early stages, please refer to tests/
and src/
for more info on possible usages.
- Available Helpers
- Sending and email with CC and BCC
- Sending an email with personalization
- Sending a templated email
- Sending an email with attachment
- Debugging validation errors
use MailerSend\Helpers\Builder\Attachment;
use MailerSend\Helpers\Builder\Recipient;
// This will help you build the recipient array
$recipients = [
new Recipient('[email protected]', 'Your Client'),
];
// This will help you build the attachments array and will encode the contents of attachments
$attachments = [
new Attachment(file_get_contents('attachment.jpg'), 'attachment.jpg')
];
Send an email with CC and BCC.
use MailerSend\MailerSend;
use MailerSend\Helpers\Builder\Recipient;
use MailerSend\Helpers\Builder\EmailParams;
$mailersend = new MailerSend(['api_key' => 'key']);
$recipients = [
new Recipient('[email protected]', 'Your Client'),
];
$cc = [
new Recipient('[email protected]', 'CC'),
];
$bcc = [
new Recipient('[email protected]', 'BCC'),
];
$emailParams = (new EmailParams())
->setFrom('[email protected]')
->setFromName('Your Name')
->setRecipients($recipients)
->setCc($cc)
->setBcc($bcc)
->setSubject('Subject')
->setHtml('This is the HTML content')
->setText('This is the text content');
$mailersend->email->send($emailParams);
use MailerSend\MailerSend;
use MailerSend\Helpers\Builder\Personalization;
use MailerSend\Helpers\Builder\Recipient;
use MailerSend\Helpers\Builder\EmailParams;
$mailersend = new MailerSend(['api_key' => 'key']);
$recipients = [
new Recipient('[email protected]', 'Your Client'),
];
$personalization = [
new Personalization('[email protected]', [
'var' => 'variable',
'number' => 123,
'object' => [
'key' => 'object-value'
],
'objectCollection' => [
[
'name' => 'John'
],
[
'name' => 'Patrick'
]
],
])
];
$emailParams = (new EmailParams())
->setFrom('[email protected]')
->setFromName('Your Name')
->setRecipients($recipients)
->setSubject('Subject {$var}')
->setHtml('This is the html version with a {$var}.')
->setText('This is the text versions with a {$var}.')
->setPersonalization($personalization);
$mailersend->email->send($emailParams);
use MailerSend\MailerSend;
use MailerSend\Helpers\Builder\Variable;
use MailerSend\Helpers\Builder\Recipient;
use MailerSend\Helpers\Builder\EmailParams;
$mailersend = new MailerSend(['api_key' => 'key']);
$recipients = [
new Recipient('[email protected]', 'Your Client'),
];
$tags = ['tag'];
$emailParams = (new EmailParams())
->setFrom('[email protected]')
->setFromName('Your Name')
->setRecipients($recipients)
->setSubject('Subject')
->setTemplateId('ss243wdasd');
$mailersend->email->send($emailParams);
use MailerSend\MailerSend;
use MailerSend\Helpers\Builder\Attachment;
use MailerSend\Helpers\Builder\Recipient;
use MailerSend\Helpers\Builder\EmailParams;
$mailersend = new MailerSend(['api_key' => 'key']);
$recipients = [
new Recipient('[email protected]', 'Your Client'),
];
$attachments = [
new Attachment(file_get_contents('attachment.jpg'), 'attachment.jpg')
];
$emailParams = (new EmailParams())
->setFrom('[email protected]')
->setFromName('Your Name')
->setRecipients($recipients)
->setSubject('Subject')
->setHtml('This is the html version.')
->setText('This is the text version.')
->setAttachments($attachments);
$mailersend->email->send($emailParams);
use MailerSend\MailerSend;
use MailerSend\Helpers\Builder\Recipient;
use MailerSend\Helpers\Builder\EmailParams;
use MailerSend\Exceptions\MailerSendValidationException;
$mailersend = new MailerSend(['api_key' => 'key']);
$recipients = [
new Recipient('[email protected]', 'Your Client'),
];
$emailParams = (new EmailParams())
->setFrom('[email protected]')
->setFromName('Your Name')
->setRecipients($recipients)
->setSubject('Subject {$var}')
->setHtml('This is the html version with a {$var}.')
->setText('This is the text versions with a {$var}.');
try{
$mailersend->email->send($emailParams);
} catch(MailerSendValidationException $e){
// See src/Exceptions/MailerSendValidationException.php for more more info
print_r($e->getResponse()->getBody()->getContents());
}