Laravel发送可变附件邮件:解决Attachment方法变量未定义问题
Laravel邮件附件传递变量报错的解决方法
你遇到的“meta未定义”错误,原因是在attachments()方法里直接调用了$meta变量,但这个方法内并没有定义该局部变量——类的公共属性$meta需要通过$this->meta来访问,就像你在envelope()方法里使用$this->meta['title']那样。
修改SendMail类的attachments()方法即可解决问题:
class SendMail extends Mailable { use Queueable, SerializesModels; public $meta; public function __construct($meta) { $this->meta = $meta; } public function envelope() { return new Envelope( from: new Address('from@from.com', 'From From'), subject: $this->meta['title'], ); } public function content() { return new Content( view: 'email', ); } public function attachments() { return [ // 这里要加上$this->访问类属性 Attachment::fromPath($this->meta['fileName']), ]; } }
另外需要确认$meta['fileName']是完整且正确的文件路径,比如如果文件存放在项目的storage/app目录下,可能需要用storage_path($this->meta['fileName'])来拼接完整路径;如果是public目录下的文件,用public_path()拼接,确保路径无误后就能正常加载附件了。
内容的提问来源于stack exchange,提问作者bart2puck
相关产品推荐
相关产品推荐

