/** * Creates file MIME part. * @param string * @param string * @param string * @return MailMimePart */ public function createFilePart($file, $contentType, $encoding) { if (!is_file($file)) { throw new FileNotFoundException("File '{$file}' not found."); } if (!$contentType) { $info = getimagesize($file); $contentType = $info ? image_type_to_mime_type($info[2]) : 'application/octet-stream'; } $part = new MailMimePart(); $part->setContentType($contentType); $part->setEncoding($encoding); $part->setBody(file_get_contents($file)); return $part; }
/** * 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; }