PSR for Mailer?

285 views
Skip to first unread message

desig...@gmail.com

unread,
Jul 29, 2018, 5:07:49 AM7/29/18
to PHP Framework Interoperability Group
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?

Larry Garfield

unread,
Aug 1, 2018, 6:58:38 PM8/1/18
to php...@googlegroups.com
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
signature.asc

Dyego Fernandes de Sousa

unread,
Aug 1, 2018, 7:07:19 PM8/1/18
to php...@googlegroups.com
I totally agree. 

--
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 php-fig+u...@googlegroups.com.
To post to this group, send email to php...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/php-fig/2006216.Sp8qzAT4IJ%40vulcan.
For more options, visit https://groups.google.com/d/optout.

Niels Braczek

unread,
Aug 1, 2018, 7:13:48 PM8/1/18
to php...@googlegroups.com
Actually, the identical data structure is useful for social media posts
and usenet or forum messages. So a generic interface for handling that
kind of messages could be very useful.

Regards,
Niels

--
| New Stars on the Horizon: GreenCape · nibralab · laJoom |
| http://www.bsds.de · BSDS Braczek Software- und DatenSysteme |
| Webdesign · Webhosting · e-Commerce · Joomla! Content Management |
------------------------------------------------------------------

desig...@gmail.com

unread,
Aug 2, 2018, 1:31:19 AM8/2/18
to PHP Framework Interoperability Group
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, other than that I couldn't find any meaningful implementations.

Hari K T

unread,
Aug 2, 2018, 1:36:19 AM8/2/18
to php...@googlegroups.com
There was some initiatives made to get the common method names .

Sharing the spreadsheet . I did one myself ( https://docs.google.com/spreadsheets/d/1Lh-g2AoT-4ivMPkGv8S_0JLVCZg9q5zk8yC2L6Ql4N4/edit#gid=0 ) , but the one below was copied from someone else ( Don't recall, may be Michael Cullum ? )

https://docs.google.com/spreadsheets/d/1Q5vQMLutP1JQq4q8apt-pTNMBfJYqjVQ2GfqxRAWcHM/edit#gid=0

Hari K T

You can ring me : +91 9388 75 8821

Skype  : kthari85
Twitter : harikt

--
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 php-fig+unsubscribe@googlegroups.com.

To post to this group, send email to php...@googlegroups.com.

Jan Schneider

unread,
Aug 2, 2018, 3:10:57 AM8/2/18
to php...@googlegroups.com

You may want to include https://github.com/horde/Mime/blob/master/lib/Horde/Mime/Mail.php with it, or get me write access to the document.

Zitat von Hari K T <ktha...@gmail.com>:

To unsubscribe from this group and stop receiving emails from it, send an email to php-fig+u...@googlegroups.com.


To post to this group, send email to php...@googlegroups.com.


For more options, visit https://groups.google.com/d/optout.




Jan Schneider
The Horde Project
https://www.horde.org/
Reply all
Reply to author
Forward
0 new messages