0

I'm using the Swiftmailer bundle for Laravel 3 and trying to attach an uploaded file to an email message:

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

I've checked that the file is uploaded correctly and I can open the file on disk. However, when I receive the email and try to open the attachment the file is corrupt and won't open. It has data but the file size is larger than the original.

benedict_w
  • 3,543
  • 1
  • 32
  • 49

1 Answers1

0

My bad: $message->attach() generates a Swift_Attachment attachment, it does not accept one as a parameter. This is the correction:

$attachment = Swift_Attachment::fromPath($attachment_path);

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

benedict_w
  • 3,543
  • 1
  • 32
  • 49