Suppose I have an assets field in my orders fields, how could I attach those files in the confirmation email?
1 Answer
I suppose you could use the \craft\commerce\services\Emails::EVENT_BEFORE_SEND_MAIL
event for that.
use craft\commerce\events\MailEvent;
use craft\commerce\services\Emails;
use yii\base\Event;
Event::on(
Emails::class,
Emails::EVENT_BEFORE_SEND_MAIL,
function (MailEvent $e) {
// You would be able to access the `\craft\mail\Message` like so
// $e->craftEmail;
// And the `\craft\commerce\elements\Order` like so
// $e->order
// So adding an asset could be done by accessing the `attach` method.
// https://www.yiiframework.com/extension/yiisoft/yii2-swiftmailer/doc/api/2.1/yii-swiftmailer-message#attach()-detail
$e->craftEmail->attach(...);
}
);