/** * Adds attachment. * @param string * @param string * @param string * @return MailMimePart */ public function addAttachment($file, $content = NULL, $contentType = NULL) { $part = new MailMimePart(); $part->setBody($content === NULL ? $this->readFile($file, $contentType) : (string) $content); $part->setContentType($contentType ? $contentType : 'application/octet-stream'); $part->setEncoding(self::ENCODING_BASE64); $part->setHeader('Content-Disposition', 'attachment; filename="' . basename($file) . '"'); return $this->attachments[] = $part; }
/** * Creates file MIME part. * @return MailMimePart */ private function createAttachment($file, $content, $contentType, $disposition) { $part = new MailMimePart(); if ($content === NULL) { $content = file_get_contents($file); if ($content === FALSE) { throw new FileNotFoundException("Unable to read file '{$file}'."); } } else { $content = (string) $content; } $part->setBody($content); $part->setContentType($contentType ? $contentType : MimeTypeDetector::fromString($content)); $part->setEncoding(preg_match('#(multipart|message)/#A', $contentType) ? self::ENCODING_8BIT : self::ENCODING_BASE64); $part->setHeader('Content-Disposition', $disposition . '; filename="' . Strings::fixEncoding(basename($file)) . '"'); return $part; }