【问题标题】:Swift Mailer Dynamic Text AttachementSwift Mailer 动态文本附件
【发布时间】:2014-02-11 02:15:30
【问题描述】:

我正在尝试将字符串作为纯文本文件附加到 swift 邮件消息中,如 doc 中所述:

$debug_data = 'Exception: ' . $e_message . PHP_EOL;
$debug_data .= $file . ': ' . $line . PHP_EOL;
$debug_data .= $trace;

$attach = Swift_Attachment::newInstance($debug_data, 'debug.txt', 'text/plain');

$message->attach($attach);

但这给了我这个错误:

Error in exception handler: fopen(Content-Type: text/plain; name=debug.txt Content-Transfer-Encoding: base64 Content-Disposition: attachment; filename=debug.txt 1ay9wdWJsaWMvaW5kZXgucGhwKDQ5KTog SWxsdW1pbmF0ZVxGb3VuZGF0aW9uXEFwcGxpY2F0aW9uLT5ydW4oKQojMTMge21haW59): failed to open stream: No such file or directory in /vendor/swiftmailer/swiftmailer/lib/classes/Swift/ByteStream/FileByteStream.php:138

有什么想法吗?!

【问题讨论】:

  • 它告诉你...No such file or directory in /vendor/swiftmailer/swiftmailer/lib/classes/Swift/ByteStream/FileByteStream.php:138
  • @SeanWM 这没有意义,因为我没有文件,我正在制作附件。

标签: php email laravel laravel-4 swiftmailer


【解决方案1】:

试试这样的:

$attachment = Swift_Attachment::newInstance()
  ->setFilename('debug.txt')
  ->setContentType('text/plain')
  ->setBody($debug_data);

【讨论】:

    【解决方案2】:

    看来你正试图在 Laravel 中使用它,它有一个 attachData 方法,它不会调用 fopen。您可以像下面的代码一样使用它

    $message->attachData($debug_data, 'debug.txt');

    【讨论】:

      【解决方案3】:

      以上两个答案都与 Symfony 4.0 不兼容。

      对于 Symfony 4.0:

      $attachment = new Swift_Attachment($debug_data, 'debug.txt', 'text/plain');
      $message->attach($attachment);
      

      // 我把它贴在这里,因为它是 'swiftmailer attachment from string' 的最高 google 结果

      【讨论】:

        猜你喜欢
        • 2011-06-07
        • 1970-01-01
        • 1970-01-01
        • 2011-08-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多