PHP code example of letmesendemail / letmesendemail-php
1. Go to this page and download the library: Download letmesendemail/letmesendemail-php library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
letmesendemail / letmesendemail-php example snippets
use LetMeSendEmail\LetMeSendEmail;
$apiKey = getenv('LETMESENDEMAIL_API_KEY');
if ($apiKey === false || $apiKey === '') {
throw new \RuntimeException('LETMESENDEMAIL_API_KEY is not set.');
}
$client = new LetMeSendEmail(apiKey: $apiKey);
$email = $client->emails()->send(
from: 'Acme <[email protected]>',
to: ['[email protected]'],
subject: 'Welcome',
html: '<p>Hello from letmesend.email</p>',
);
echo $email->getId(); // "01kvv5a6xk9qd6y2egeae8w76e"
use LetMeSendEmail\LetMeSendEmail;
use LetMeSendEmail\Configuration;
// Simple setup with just an API key:
$client = new LetMeSendEmail(apiKey: 'lms_live_...');
// With custom configuration:
$config = new Configuration(
apiKey: 'lms_live_...',
baseUrl: 'https://letmesend.email/api/v1', // default
timeout: 60, // default: 30 seconds
);
$client = new LetMeSendEmail(configuration: $config);
$email = $client->emails()->send(
from: 'Acme <[email protected]>',
to: ['[email protected]', 'Jane <[email protected]>'],
subject: 'Welcome to letmesend.email',
html: '<h1>Welcome!</h1><p>Thanks for signing up.</p>',
text: 'Welcome! Thanks for signing up.',
type: 'transactional', // or 'broadcast'
eventName: 'user.created',
replyTo: ['[email protected]'],
cc: ['[email protected]'],
bcc: ['[email protected]'],
headers: ['X-Custom-Header' => 'value'],
attachments: [
[
'name' => 'report.pdf',
'mime' => 'application/pdf',
'path' => 'https://storage.example.com/report.pdf',
],
],
);
echo $email->getId(); // "01kvv5a6xk9qd6y2egeae8w76e"
echo $email->getStatus(); // "pending_scan" or "accepted"
print_r($email->getEmails()); // list of all recipient addresses
use LetMeSendEmail\Requests\Attachment;
// From a URL (external file)
$attachment = Attachment::fromPath(
name: 'report.pdf',
path: 'https://example.com/report.pdf',
contentId: 'optional-cid',
contentDisposition: 'attachment', // or 'inline'
);
// From base64 content
$attachment = Attachment::fromContent(
name: 'data.txt',
content: base64_encode('Hello World'),
contentId: 'optional-cid',
contentDisposition: 'inline',
);
$email = $client->emails()->send(
from: 'Acme <[email protected]>',
to: ['[email protected]'],
subject: 'With attachment',
html: '<p>See attached</p>',
attachments: [$attachment],
);
use LetMeSendEmail\Requests\TemplateVariable;
$email = $client->emails()->sendWithTemplate(
from: 'Acme <[email protected]>',
to: ['[email protected]'],
templateId: '01ARZ3NDEKTSV4RRFFQ69G5FAV',
templateVariables: [
new TemplateVariable('USER_NAME', 'string', 'John'),
new TemplateVariable('ORDER_NUMBER', 'number', 12345),
],
);
$email = $client->emails()->send(
from: 'Acme <[email protected]>',
to: ['[email protected]'],
subject: 'Your invoice',
html: '<p>Invoice attached</p>',
idempotencyKey: 'my-unique-key-abc123',
);
if ($email->isDuplicate()) {
// This send was a duplicate — the original send was not re-attempted.
}
use LetMeSendEmail\Exceptions\ValidationError;
use LetMeSendEmail\Exceptions\AuthenticationError;
use LetMeSendEmail\Exceptions\RateLimitError;
use LetMeSendEmail\Exceptions\ApiException;
try {
$client->emails()->send(/* ... */);
} catch (ValidationError $e) {
echo $e->getMessage();
print_r($e->getValidationErrors()); // field-level errors
} catch (AuthenticationError $e) {
echo 'Check your API key.';
} catch (RateLimitError $e) {
echo 'Retry after ' . $e->getRetryAfter() . ' seconds.';
} catch (ApiException $e) {
echo 'HTTP ' . $e->getHttpStatus() . ': ' . $e->getMessage();
}
$list = $client->emails()->list(perPage: 10);
// Items on this page
$emails = $list->items();
// Pagination metadata
$pag = $list->pagination();
$pag->hasMore(); // bool
$pag->getTotal(); // int
$pag->getPerPage(); // int
$pag->getFetched(); // int
// Next page: use the last item's ID
if ($list->pagination()->hasMore() && count($list->items()) > 0) {
$items = $list->items();
$nextList = $client->emails()->list(after: $items[count($items) - 1]->getId());
}
// Previous page: use the first item's ID (from a page other than the first)
if (count($list->items()) > 0) {
$items = $list->items();
$prevList = $client->emails()->list(before: $items[0]->getId());
}
$config = new Configuration(
apiKey: 'lms_live_...',
retries: 3,
);
$client = new LetMeSendEmail(configuration: $config);
use LetMeSendEmail\Support\WebhookSignature;
use LetMeSendEmail\Exceptions\WebhookVerificationException;
use LetMeSendEmail\Exceptions\WebhookSigningException;
$payload = file_get_contents('php://input');
if ($payload === false) {
http_response_code(400);
echo 'Failed to read request body.';
exit;
}
$headers = getallheaders();
if (!is_array($headers)) {
$headers = [];
}
$secret = getenv('LETMESENDEMAIL_WEBHOOK_SECRET');
if ($secret === false || $secret === '') {
http_response_code(500);
echo 'Webhook secret is not configured.';
exit;
}
try {
$event = WebhookSignature::verify(
payload: $payload,
headers: $headers,
secret: $secret,
tolerance: 300,
);
// $event contains the verified payload as an associative array
} catch (WebhookVerificationException $e) {
http_response_code(400);
echo 'Webhook verification failed: ' . $e->getMessage();
} catch (WebhookSigningException $e) {
http_response_code(500);
echo 'Webhook configuration error: ' . $e->getMessage();
}
bash
composer
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.