zend framework 2 mail set default sender

485 Views Asked by At

ZF1 Zend_Mail has a static method setDefaultFrom($email, $name = null). Value specified here is used as default wherever it's not given for a concrete object.

ZF2 has no more facade component Zend\Mail. Instead you should explicitly use

Zend\Mail\Transport\TransportInterface::send(Mail\Message $message) 

My proposal is to store default sender in the config and set it every time when Zend\Message object is created. We can even go further and create factory in Module for mail message where instantiate new object every time and inject default sender (make sure you don't share the same message object)

'service_manager' => array(
    'shared' => array(
        'default_mail_message' => false,
    ),
),

Any other suggestions?

1

There are 1 best solutions below

1
On

I'm not sure what you are asking. There's certainly nothing wrong with your suggested approach.

For myself, I've stored the default sender in config and set it each time in my apps, the sender is different for each email that the app sends.