PHP code example of wieni / wmmailable

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

    

wieni / wmmailable example snippets




namespace Drupal\wmcustom\Mail;

/**
 * @Mailable(
 *     id = "contact_form_submission",
*      template = "mail.contact-submission"
 * )
 */
class ContactFormSubmission extends MailableBase
{
    public function build(array $parameters): MailableInterface;
}



namespace Drupal\wmcustom\Mail;

/**
 * @Mailable(
 *     id = "register_no_approval_tConfirmationMail extends MailableBase
{
    public function build(array $parameters): MailableInterface
    {
        $parameters['oneTimeLoginUrl'] = user_pass_reset_url($parameters['account']);
        
        {...}
    }
}



namespace Drupal\wmcustom\Form;

use Drupal\wmmailable\Mailer\MailerInterface;

class ContactForm extends FormBase
{
    /** @var MailerInterface */
    protected $mailer;

    public function __construct(
        MailerInterface $mailer
    ) {
        $this->mailer = $mailer;
    }

    public function submitForm(array &$form, FormStateInterface $formState)
    {
        $mail = $this->mailer->create('contact_form_submission')
            ->setRecepients(['Wieni <[email protected]>', '[email protected]'])
            ->addBcc('[email protected]')
            ->setParameters(
                compact('domain', 'firstName', 'lastName', 'email', 'question')
            );
        
        $this->mailer->send($mail);
	}

	public static function create(ContainerInterface $container)
	{
	    return new static(
	        $container->get('mailable.mailer')
        );
    }
}



function wmcustom_mailable_alter(MailableInterface $mail)
{
    $mail->setHeader('X-SES-SOURCE-ARN', '<...>');
}



use Drupal\wmmailable\Event\MailableAlterEvent;
use Drupal\wmmailable\WmmailableEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class MailableAlterSubscriber implements EventSubscriberInterface
{
    public static function getSubscribedEvents()
    {
        $events[WmmailableEvents::MAILABLE_ALTER][] = 'onMailAlter';

        return $events;
    }

    public function onMailAlter(MailableAlterEvent $event)
    {
        $mailable = $event->getMailable();
        $mailable->setSubject("Here's a better subject.");
    }
}