PHP code example of mperonnet / symfony-react-email
1. Go to this page and download the library: Download mperonnet/symfony-react-email 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/ */
use Mperonnet\ReactEmail\ReactEmailRenderer;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mime\Email;
class UserController
{
private ReactEmailRenderer $emailRenderer;
private MailerInterface $mailer;
public function __construct(
ReactEmailRenderer $emailRenderer,
MailerInterface $mailer
) {
$this->emailRenderer = $emailRenderer;
$this->mailer = $mailer;
}
public function createUser()
{
// Create a user...
$user = [
'name' => 'John Doe',
'email' => '[email protected]'
];
// Render the email template
$content = $this->emailRenderer->render('new-user', [
'user' => $user
]);
// Create a Symfony Email object
$email = (new Email())
->html($content->html)
->text($content->text)
->subject('Welcome to our platform!')
->from('[email protected]')
->to($user['email']);
// Send the email
$this->mailer->send($email);
// ...
}
}
class ReactEmailContent
{
public readonly string $html; // The rendered HTML version of the email
public readonly string $text; // The rendered plain text version of the email
}
// Render the email template
$content = $this->emailRenderer->render('welcome-email', [
'user' => $user,
'resetLink' => $resetLink
]);
// Create and configure a Symfony Email object
$email = (new Email())
->html($content->html)
->text($content->text)
->subject('Welcome to our Application')
->from('[email protected]')
->to($user->getEmail());
// Send the email
$this->mailer->send($email);
// In your controller/service
$this->messageBus->dispatch(new SendEmailMessage(
'welcome-email',
['user' => ['name' => $user->getName(), 'email' => $user->getEmail()]],
'Welcome to our Platform!'
));
// In your message handler
class SendEmailMessageHandler
{
public function __invoke(SendEmailMessage $message)
{
$content = $this->emailRenderer->render($message->template, $message->data);
$email = (new Email())
->html($content->html)
->text($content->text)
->subject($message->subject)
->from('[email protected]')
->to($message->data['user']['email']);
$this->mailer->send($email);
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.