编程语言
首页 > 编程语言> > php – Swift邮件程序默认发件人

php – Swift邮件程序默认发件人

作者:互联网

我正在尝试为Swift Mailer 4.3.0中的所有邮件设置默认发件人,但找不到合适的解决方案.我想避免在每条消息中使用 – > setFrom(),因为这将是一个未来的头痛.我可以使用常量,但我正在寻找更优雅的解决方案.

$message = Swift_Message::newInstance()
                ->setSubject('subject')
                ->setFrom(array('sender@domain.com' => 'Sender'))
                ->setTo(array('recipient@domain.com'))
                ->setBody('Message');

谢谢!

解决方法:

你不能.

据我所知,你不能省略这个参数.

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.

这可能是最好的解决方案:

->setFrom(MyClass::FROM_EMAIL);

一旦你可以尝试,就是在你的应用程序中创建一个实例,并在你想要发送新邮件时克隆它而不重新定义from部分:

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

然后,在其他地方:

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

标签:swiftmailer,php
来源: https://codeday.me/bug/20190901/1780951.html