PHP code example of jdr / mailer

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

    

jdr / mailer example snippets

 php


use JDR\Mailer\EmailType;
use JDR\Mailer\Email\Address;
use JDR\Mailer\Part;

/**
 * The EmailType defines the message.
 */
class WelcomeEmail implements EmailType
{
    /**
     * @var User
     */
    private $user;

    /**
     * Constructor.
     *
     * @param User $user
     */
    public function __construct(User $user)
    {
        $this->user = $user;
    }

    /**
     * {@inheritdoc}
     */
    public function buildEmail(EmailBuilder $builder)
    {
        $builder
            ->add(new Part\Sender(
                new Address('[email protected]', 'Hello Example')
            ))
            ->add(new Part\Recipients(
                new Address($this->user->getEmail(), $this->user->getUsername())
            ))
            ->add(new Part\Subject(
                'Welcome {{ username }}',
                [
                    '{{ username }}' => $this->user->getUsername(),
                ]
            ))
            ->add(new Part\Message(
                'text/html',
                <<<EOT
Welcome {{ username }},
Thank you for choosing jdr/mailer, enjoy your stay.
EOT
                ,
                [
                    '{{ username }}' => $this->user->getUsername(),
                ]
            ))
        ;
    }
}

// Create a new Mailer (i.e. SwiftMailer)
$mailer = new SwiftMailer();
$mailer->sendEmail(new WelcomeEmail($user));