Swiftmailer附件无法打开(Laravel 3)

时间:2014-06-16 10:27:47

标签: file-upload email-attachments swiftmailer laravel-3

我在Laravel 3中使用Swiftmailer软件包并尝试将上传的文件附加到电子邮件中:

$message = \Message::bcc(preg_split('/,/', preg_replace('/\s+/', '', $to)))
    ->from(Input::get('from'))
    ->subject(Input::get('subject')
    ->body(Input::get('message'));

$attachment_path = null;

if (Input::has_file('attachment')) {
    $filename = Input::file('attachment.name')
    $attachment_path = path('storage') . "files/attachments/";
    if (Input::upload('attachment', $attachment_path, $filename)) {
        $attachment_path = $attachment_path . $filename;
        $attachment = Swift_Attachment::fromPath($attachment_path);
        $message->attach($attachment, $filename, Input::file('attachment.type'));
    }
}

$message->send();

// clear up attachments
if ($attachment_path) {
    File::delete($attachment_path);
}

我已检查过该文件是否正确上传,我可以在磁盘上打开该文件。但是,当我收到电子邮件并尝试打开附件时,文件已损坏并且无法打开。它有数据但文件大小比原始大。

1 个答案:

答案 0 :(得分:0)

我的错误:$message->attach()生成一个Swift_Attachment附件,它不接受一个作为参数。这是更正:

<击> $attachment = Swift_Attachment::fromPath($attachment_path);

$message->attach($attachment_path, $filename, Input::file('attachment.type'));