/** * 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="' . String::fixEncoding(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) { if (!is_file($file)) { throw new \FileNotFoundException("File '$file' not found."); } if (!$contentType && $info = getimagesize($file)) { $contentType = $info['mime']; } $part->setBody(file_get_contents($file)); } else { $part->setBody((string) $content); } $part->setContentType($contentType ? $contentType : 'application/octet-stream'); $part->setEncoding(preg_match('#(multipart|message)/#A', $contentType) ? self::ENCODING_8BIT : self::ENCODING_BASE64); $part->setHeader('Content-Disposition', $disposition . '; filename="' . String::fixEncoding(basename($file)) . '"'); return $part; }
/** * Returns encoded message. * @return string */ public function generateMessage() { if ($this->getHeader('Message-ID')) { return parent::generateMessage(); } else { return $this->build()->generateMessage(); } }