带有file_get_contents的附件?

Ili*_*tko 1 email attachment swiftmailer laravel laravel-4

我花了很多时间试图找出使用Lavarel 4发送电子邮件并包含邮件附件的任何情况。甚至没有人谈论它,每个人都在附加文件系统中的文件。

有没有人在没有实际将其下载到磁盘的情况下发送过电子邮件以附加图像(例如:http : //www.w3schools.com/html/html5.gif)?

jsz*_*ody 6

Swift Mailer绝对可以让您使用Swift_Attachment::newInstance()以下方式附加数据:

http://swiftmailer.org/docs/messages.html#attaching-dynamic-content

看来Laravel为此提供了一个包装方法,但主要文档中并未提及。看这里:

https://github.com/laravel/framework/blob/4.2/src/Illuminate/Mail/Message.php#L196

http://laravel.com/api/4.2/Illuminate/Mail/Message.html#method_attachData

因此,您将像这样使用它:

$data = file_get_contents(...);
$message->attachData($data, $filename);
Run Code Online (Sandbox Code Playgroud)

这对于您自己动态生成数据(例如PDF)的情况特别有用。

如果您只是从URL提取一个已有文件,则可以attach()对URL 使用标准方法,然后让Swift Mailer提取它:

$message->attach("http://www.w3schools.com/html/html5.gif");
Run Code Online (Sandbox Code Playgroud)