Swift mailer default sender

php, swiftmailer

Solution

You can't.

As far as I know, you can't omit this parameter.

A `From:` address is required and is set with the `setFrom()` method of the message. `From:` addresses specify who actually wrote the email, and usually who sent it.

This is maybe the best solution:

->setFrom(MyClass::FROM_EMAIL);

Once thing you can try, is to create an instance once in your application, and clone it when you want to send a new mail without re-defining the from part:

// somewhere early in your app
$message = Swift_Message::newInstance()
            ->setFrom(array('sender@domain.com' => 'Sender'));

And then, somewhere else:

$newMessage = clone $message;
$newMessage
    ->setSubject('subject')
    ->setTo(array('recipient@domain.com'))
    ->setBody('Message')
    ->send();

Problem

I'm trying to set a default sender to all messages in Swift Mailer 4.3.0, but couldn't find a proper solution. I want to avoid using ->setFrom() in every message, since it'll be a future headache. I could use a constant for that, but I was looking for a more elegant solution. ``` $message = Swift_Message::newInstance() ->setSubject('subject') ->setFrom(array('sender@domain.com' => 'Sender')) ->setTo(array('recipient@domain.com')) ->setBody('Message'); ``` Thanks!

Original source