【问题标题】:Attachment with file_get_contents?带有 file_get_contents 的附件?
【发布时间】:2015-03-31 05:41:13
【问题描述】:

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

有没有人有过发送电子邮件附加图片(例如:http://www.w3schools.com/html/html5.gif)而不实际下载到磁盘上的经验?

【问题讨论】:

    标签: email laravel laravel-4 attachment swiftmailer


    【解决方案1】:

    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);
    

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

    如果您只是从 URL 中提取预先存在的文件,您可以使用标准的 attach() 方法与 URL,然后让 Swift Mailer 获取它:

    $message->attach("http://www.w3schools.com/html/html5.gif");
    

    【讨论】:

    • 我尝试了几次,但我可能遇到了另一个问题。非常感谢您的提示! =)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-09-17
    • 1970-01-01
    • 1970-01-01
    • 2022-08-19
    • 2019-10-16
    • 2010-12-31
    • 1970-01-01
    相关资源
    最近更新 更多