PHP code example of glpzzz / form-mailer

1. Go to this page and download the library: Download glpzzz/form-mailer 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/ */

    

glpzzz / form-mailer example snippets


use Glpzzz\FormMailer\MailableForm;
use Glpzzz\FormMailer\RendersFieldTable;
use Glpzzz\FormMailer\Captcha\CaptchaRule;
use Symfony\Component\Mime\Address;

final class ContactForm extends FormModel implements RulesProviderInterface, MailableForm
{
    use RendersFieldTable;

    public function getRules(): iterable
    {
        return [
            // ...
            'captcha' => [new Required(), new CaptchaRule('contact')],
        ];
    }

    public function mailSubject(): string   => "Quote request — {$this->name}";
    public function mailReplyTo(): ?Address  => new Address($this->email, $this->name ?? '');
    public function mailTemplate(): string   => '@form-mailer/table.html.twig';

    // exclude the captcha field from the email
    protected function mailFields(): array   => ['name', 'phone', 'email', 'comment'];
    protected function fieldType(string $n): string
        => match ($n) { 'email' => 'email', 'phone' => 'phone', default => 'text' };
}

use Glpzzz\FormMailer\FormMailer;
use Glpzzz\FormMailer\MailSettings;
use Glpzzz\FormMailer\Captcha\FigletCaptcha;
use Glpzzz\FormMailer\Captcha\SessionCaptchaStore;

$captcha = new FigletCaptcha(new SessionCaptchaStore());
$mailer  = FormMailer::create(
    dsn: $config['dsn'],
    settings: MailSettings::fromArray($config), // from, to, bcc, replyTo
);

if ($form->isValid()) {
    $mailer->send($form);
}

$captcha->issue('contact');          // fresh code for the next render
echo $captcha->banner('contact');    // figlet art for the template

FormMailer::create(dsn: ..., settings: ..., templateDirs: [__DIR__ . '/emails' => 'app']);