darvinstudio/darvin-webmail-linker-bundle
Install the Bundle
Add the bundle to your composer.json:
composer require darvinstudio/darvin-webmail-linker-bundle
Enable it in config/bundles.php:
return [
// ...
DarvinStudio\DarvinWebmailLinkerBundle\DarvinWebmailLinkerBundle::class => ['all' => true],
];
Configure the Bundle Publish the default configuration:
php bin/console darvin-webmail-linker:install
Update config/packages/darvin_webmail_linker.yaml with your preferred webmail providers (e.g., Gmail, Outlook, Yahoo) and their API keys.
First Use Case: Generate a Webmail Link
Inject the WebmailLinker service in a controller or service:
use DarvinStudio\WebmailLinker\WebmailLinker;
class EmailController extends AbstractController
{
public function sendEmail(WebmailLinker $webmailLinker): Response
{
$email = 'user@example.com';
$subject = 'Test Email';
$body = 'Click here to open in webmail: ';
$link = $webmailLinker->generateLink($email, $subject, $body);
return $this->render('emails/send.html.twig', ['link' => $link]);
}
}
Generating Links in Email Templates
Use the WebmailLinker service to dynamically generate webmail links in Twig templates:
<a href="{{ app.service('DarvinStudio\WebmailLinker\WebmailLinker').generateLink(email, subject, body) }}">
Open in Webmail
</a>
Integrating with Mailers
Extend Laravel's Mailable or Symfony's Swiftmailer to include webmail links:
use DarvinStudio\WebmailLinker\WebmailLinker;
class OrderConfirmationMailer extends Mailable
{
public function build(WebmailLinker $webmailLinker)
{
$link = $webmailLinker->generateLink(
$this->email,
'Order Confirmation',
'View your order details'
);
return $this->markdown('emails.order_confirmation')
->with(['webmailLink' => $link]);
}
}
Customizing Provider Logic Override the default provider configurations via DI:
# config/packages/darvin_webmail_linker.yaml
darvin_webmail_linker:
providers:
gmail:
api_key: '%env(GMAIL_API_KEY)%'
custom_domain: 'mail.yourcompany.com'
Batch Processing Generate links for multiple recipients in bulk:
$recipients = ['user1@example.com', 'user2@example.com'];
$links = array_map(
fn($email) => $webmailLinker->generateLink($email, 'Batch Email', 'Content'),
$recipients
);
Deprecated Library
The underlying webmail-linker library (last updated in 2019) may not support modern email providers (e.g., Outlook's new API). Test thoroughly with your target providers.
Configuration Overrides
Avoid hardcoding API keys in config/packages/. Use Symfony's %env() or Laravel's .env:
darvin_webmail_linker:
providers:
outlook:
api_key: '%env(OUTLOOK_API_KEY)%'
Link Expiry Generated links may expire after a short period (e.g., 24 hours). Handle failures gracefully:
try {
$link = $webmailLinker->generateLink($email, $subject, $body);
} catch (\RuntimeException $e) {
// Fallback to a generic "Open Webmail" link
$link = "https://mail.google.com/mail/?view=cm&fs=1&to=$email&su=$subject";
}
CSRF and Security
Sanitize user-provided $email, $subject, and $body to prevent injection in generated links. Use Symfony's Validator or Laravel's Validator:
$validator = $this->container->get('validator');
$errors = $validator->validate($email, [
new Email(),
new Length(['max' => 255]),
]);
Extending Providers
Add custom providers by implementing DarvinStudio\WebmailLinker\ProviderInterface:
class CustomProvider implements ProviderInterface
{
public function generateLink(string $email, string $subject, string $body): string
{
return "https://custommail.com/?email=$email&subject=$subject";
}
}
Register it in services.yaml:
services:
DarvinStudio\WebmailLinker\ProviderInterface custom_provider:
class: App\Service\CustomProvider
tags: ['darvin_webmail_linker.provider']
Logging Failures Enable Symfony's monolog to log failed link generations:
# config/packages/monolog.yaml
handlers:
main:
type: stream
path: "%kernel.logs_dir%/%kernel.environment%.log"
level: debug
channels: ["!event"]
Testing
Mock the WebmailLinker service in PHPUnit:
$this->mockBuilder()
->disableOriginalConstructor()
->getMock()
->method('generateLink')
->willReturn('https://test-webmail-link.com');
Fallback Links Provide a static fallback link in templates:
<a href="{{ webmailLink|default('https://mail.google.com') }}">
Open in Webmail
</a>
How can I help you explore Laravel packages today?