While developing the contact pages for some of my websites, I recently 
thought about this. About how having a wrapper class or standard interfaces 
would allow me to easily switch between mailer libraries. Having a standard 
allows easy switching. I ended up with something like this:

*Mailer Interface:*

   compose(EmailInterface $email): MessageInterface;
   
   withHost(string $host);
   
   withPort(int $port);
   
   withProtocol(string $protocol);
   
   withAuth(string $username, string $password);
   
   
   withDkim(
       string $domain, 
       string $selector = 'default', 
       ?string $identity = null, ?string 
       $passphrase = null,
       ?string $privateKeyFilePath = null, 
       ?string $privateString = null
   );
   
   /**
     * Send one email message to a single 'to' recipient.
     *
     * If email has multiple recipients (as specified in 'to' field), 
     * the method delegates the request to $this::sendBatch() if the
     * message body is not a callable, otherwise the request is 
     * delegated to $this::sendMultiple() instead.
     */
   send();
   

*EmailInterface:*

   setTo(string|array $to);
   
   setFrom(string $from);
   
   setReplyTo(string|array $replyTo);
   
   setCc(string|array $cc);
   
   setBcc(string|array $bcc);
   
   setMainRecipient(string $mainRecipient);
   
   getMainRecipient(): string;
   
   getTo();
   
   getFrom(): string;
   
   getReplyTo();
   
   getCc();
   
   getBcc();
   
   getMessage(): MessageInterface;
   

*MessageInterface:*

write(string|callable $body);

setSubject(string $subject);

setAltBody(string $altBody);

setCharset(string $charset);

withAttachment(string $file, string $contentType = '');

clearAttachments();

getSubject(): string;

getBody(): string|callable;

getAltBody(): string;

getCharset(): string;

getAttachments(): array;


*The way this works is kind of like this:*

$mail = new PhpMailer(*$host, $port, $type*); // PhpMailer implements 
MailerInterface
$mail->setDebug(*false*);

$mail->withDkim('designcise.com', 'default');

try {
    $eml = new Email(*$to, $from*); // Email implements EmailInterface
    $eml->setReplyTo(*$reply_to*);

    $mail
         // compose method returns Message Object 
         // (which is instantiated in *$eml* Obj Constructor)
         ->compose(*$eml*)
         // so now we can set Message Object properties:
         ->setSubject(*$subject*)
         ->write(*$msg*);

    // and send email
    $mail->send();    
} catch(Exception $e) {
    // ...
}

I took some inspiration from the Yii2 Framework 
<https://github.com/yiisoft/yii2-framework/tree/master/mail>, other than 
that I couldn't find any meaningful implementations.


On Thursday, August 2, 2018 at 3:58:38 AM UTC+5, Larry Garfield wrote:
>
> On Sunday, July 29, 2018 4:07:49 AM CDT [email protected] <javascript:> 
> wrote: 
> > I think it would be a good idea to have a set of interfaces to create a 
> > standard for email libraries (such as PHPMailer, SwiftMailer, etc.); 
> anyone 
> > agree? 
>
> In concept it sounds similar to PSR-7/PSR-17.  You'd have a value object 
> for 
> the email message, and a fronting interface for a thing that sends a 
> message. 
>
> Whether there's value to that or not would depend mainly on if PHPMailer, 
> SwiftMailer, etc. were interested in standardizing on such an interface. 
>  If 
> so, yes.  If they're not interested, then there wouldn't be much point to 
> it. 
>
> --Larry Garfield

-- 
You received this message because you are subscribed to the Google Groups "PHP 
Framework Interoperability Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/php-fig/c1e50c83-d173-490a-89cc-d952f5270af9%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to