Creates a new mime part as a multipart/alternative and assigns the passed
$contentPart as a part below it before returning it.

@param Message $message
@param MessagePart $contentPart
@return MimePart the alternative part
191564
public function createAlternativeContentPart(Message $message, MessagePart $contentPart)
    {
        $altPart = $this->partBuilderFactory->newPartBuilder($this->mimePartFactory)->createMessagePart();
        $this->setMimeHeaderBoundaryOnPart($altPart, 'multipart/alternative');
        $message->removePart($contentPart);
        $message->addChild($altPart, 0);
        $altPart->addChild($contentPart, 0);
        return $altPart;
    }
619270
def from_mime(cls, message, manager):
        
        text, html, attachments = deconstruct_multipart(message)
        subject = prepare_header(message["Subject"])
        sender = prepare_header(message["From"])
        to = prepare_header(message["To"])
        cc = prepare_header(message["Cc"])
        bcc = prepare_header(message["Bcc"])
        reply_to = prepare_header(message["Reply-To"])
        tag = getattr(message, "tag", None)
        return cls(
            manager=manager,
            From=sender,
            To=to,
            TextBody=text,
            HtmlBody=html,
            Subject=subject,
            Cc=cc,
            Bcc=bcc,
            ReplyTo=reply_to,
            Attachments=attachments,
            Tag=tag,
        )
191986
public function removePartByMimeType(Message $message, $mimeType, $index = 0)
    {
        $parts = $message->getAllParts(PartFilter::fromInlineContentType($mimeType));
        $alt = $message->getPart(0, PartFilter::fromInlineContentType('multipart/alternative'));
        if ($parts === null || !isset($parts[$index])) {
            return false;
        } elseif (count($parts) === 1) {
            return $this->removeAllContentPartsByMimeType($message, $mimeType, true);
        }
        $part = $parts[$index];
        $message->removePart($part);
        if ($alt !== null && $alt->getChildCount() === 1) {
            $this->genericHelper->replacePart($message, $alt, $alt->getChild(0));
        }
        return true;
    }
131041
protected static function getMessagePrimaryContentType(Swift_Mime_Message $message)
    {
        $contentType = $message->getContentType();
        if (self::supportsContentType($contentType)) {
            return $contentType;
        }
        // SwiftMailer hides the content type set in the constructor of Swift_Mime_SimpleMessage as soon
        // as you add another part to the message. We need to access the protected property
        // userContentType to get the original type.
        $messageRef = new \ReflectionClass($message);
        if ($messageRef->hasProperty('userContentType')) {
            $propRef = $messageRef->getProperty('userContentType');
            $propRef->setAccessible(true);
            $contentType = $propRef->getValue($message);
        }

        return $contentType;
    }
661381
public function addAttachmentPart($resource, $mimeType, $filename = null, $disposition = 'attachment')
    {
        $this->messageHelperService
            ->getMultipartHelper()
            ->createAndAddPartForAttachment($this, $resource, $mimeType, $disposition, $filename);
    }
306573
public function html($content, $param = [], $config = [])
    {
        if ($param) {
            $content = strtr($content, $this->parseParam($param, $config));
        }
        $this->message->setBody($content, MailerConfig::CONTENT_HTML);

        return $this;
    }
986237
public MultipartContent part(String fieldName, String contentType, String value) {
        return part(fieldName, null, contentType, value);
    }
602372
private function buildPayload(Swift_Mime_Message $message)
    {
        try {
            return $this->payloadBuilder->buildPayload($message);
        } catch (AnyException $exception) {
            throw $this->createAndDispatchTransportException(
                'Failed to build payload for a SparkPost transmission',
                $exception
            );
        }
    }
1009485
public function add_attachment($message, $name =  "", $ctype =  "application/octet-stream", $encode =  "base64", $charset='', $contentid='') {
    $this->parts[] = array ( "ctype" => $ctype,
                             "message" => $message,
                             "encode" => $encode,
                             "name" => $name,
                             "charset" => $charset,
                             "contentid" => $contentid
                            );
  }
1182269
public MimeMessage newMessage( Session session )
    throws MessagingException, EmailException
  {
    MimeMessage message = new MimeMessage( session );

    // populate the addresses and subject.
    populate(message);

    // create the multipart content.
    MimeMultipart containingMultipart = new MimeMultipart(EmailUtil.SUBTYPE_MIXED);

    // create the message multipart content.
    MimeMultipart messageMultipart = new MimeMultipart(EmailUtil.SUBTYPE_ALTERNATIVE);
    containingMultipart.addBodyPart(EmailUtil.newMultipartBodyPart(messageMultipart));

    // create the text part.
    messageMultipart.addBodyPart(EmailUtil.newTextBodyPart(getText()));

    // create the html part.
    MimeMultipart htmlMultipart = new MimeMultipart(EmailUtil.SUBTYPE_RELATED);
    htmlMultipart.addBodyPart(EmailUtil.newHtmlBodyPart(getHtml()));
    messageMultipart.addBodyPart(EmailUtil.newMultipartBodyPart(htmlMultipart));

    /*
    // iterate over the html context, creating new elements for each.
    Iterator htmlAttachmentIterator = htmlAttachmentMap.entrySet().iterator();
    while( htmlAttachmentIterator.hasNext() ) {
      Map.Entry htmlAttachmentEntry = (Map.Entry)htmlAttachmentIterator.next();
      htmlMultipart.addBodyPart(EmailUtil.newHtmlAttachmentPart((URL)htmlAttachmentEntry.getKey(), (String)htmlAttachmentPart.getValue());
    }

    // add the html multipart to the message multipart.
    messageMultipart.addBodyPart(EmailUtil.newMultipartBodyPart(htmlMultipartBodyPart));
    */
  
      
    // create the attachments.
    for( URL attachmentUrl : attachmentUrlSet ) {
      containingMultipart.addBodyPart(EmailUtil.newAttachmentBodyPart(attachmentUrl, null));
    }
    try {
      for( StringAttachment attachment : attachmentStrings ) {
        containingMultipart.addBodyPart(EmailUtil.newAttachmentBodyPart(attachment.getContent(), null, attachment.getMimeType(), attachment.getFilename()));
      }
    } catch (IOException ioe) {
      throw new MessagingException("Unable to create attachment for string.", ioe);
    }

    message.setContent(containingMultipart);

    // save the changes.
    message.saveChanges();

    // return the email.
    return message;
  }
195268
protected function buildMessageParts($message, $references, $offset = 0)
    {
        $messageParts = array();

        // get first reference
        $reference = array_shift($references);

        if (!$reference) {
            return array(
                $this->buildMessagePart($message, 0, mb_strlen($message)),
            );
        }

        $messageParts[] = $this->buildMessagePart(
            $message,
            0,
            $reference->getStartIndice() - $offset
        );

        $messageParts[] = $this->buildMessagePart(
            $message,
            $reference->getStartIndice() - $offset,
            $reference->getEndIndice() - $offset,
            $reference
        );

        $message = mb_substr($message, $reference->getEndIndice() - $offset);

        foreach ($this->buildMessageParts($message, $references, $reference->getEndIndice()) as $messagePart) {
            $messageParts[] = $messagePart;
        };

        return $messageParts;
    }
201343
private function build_message($part) {
    $message = $part[ "message"];
    // #### only base64 encoding supported
    // #### other encodings you have to encode yourself
    if ($part[ "encode"] == "base64"){
      $message = chunk_split(base64_encode($message));
    }else if ($part[ "encode"] == "quoted-printable"){
      $message = $this->encode_qp($message);
    }
    return  "Content-Type: ".$part[ "ctype"].
            ($part[ "charset"]? "; charset = \"".$part[ "charset"]. "\"" :  "").
            ($part[ "name"]? "; name = \"".$part[ "name"]. "\"" :  "").
            (empty($part[ "encode"])? "" : "\nContent-Transfer-Encoding: ".$part[ "encode"] ).
            (empty($part[ "contentid"])? "" : "\nContent-ID: ".$part[ "contentid"] ).
            ((empty($part["contentid"])&&($part["name"]))? "\nContent-Disposition: attachment; filename=\"".$part["name"].'"' : '' ).
            "\n\n$message\n";
  }
644912
public static function createForMessage($recipientOrPhone, Message $message, $notificationType = NotificationType::REGULAR, $messagingType = MessagingType::RESPONSE)
    {
        $options = [];
        $data = [
            'messaging_type' => $messagingType,
            'recipient' => self::createRecipientField($recipientOrPhone),
            'message' => $message,
            'notification_type' => $notificationType,
        ];
        
        if ($message->hasFileToUpload()) {

            // Create a multipart request
            $options[RequestOptions::MULTIPART] = [
                [
                  'name' => 'messaging_type',
                  'contents' => $messagingType
                ],
                [
                    'name' => 'recipient',
                    'contents' => json_encode($data['recipient']),
                ],
                [
                    'name' => 'message',
                    'contents' => json_encode($data['message']),
                ],
                [
                    'name' => 'notification_type',
                    'contents' => $data['notification_type'],
                ],
                [
                    'name' => 'filedata',
                    'contents' => $message->getFileStream(),
                ],
            ];

            // Update timeout if we upload a file
            $options['timeout'] = Client::DEFAULT_FILE_UPLOAD_TIMEOUT;

            return $options;
        }

        $options[RequestOptions::JSON] = $data;

        return $options;
    }
766548
private MimeMessage createMimeMessage() {
		// prepare the message
		Session session = Session.getInstance(properties, authenticator);
		return new MimeMessage(session);
	}
665994
public static MimeMessage newMimeMessage(String mailString) throws MessagingException {
        return newMimeMessage(EncodingUtil.toStream(mailString, EncodingUtil.CHARSET_EIGHT_BIT_ENCODING));
    }
372994
private function failsHeaderFor(MessagePart $part, $type, $name, $header)
    {
        $headerValue = null;
        
        static $map = [
            'content-type' => 'getContentType',
            'content-disposition' => 'getContentDisposition',
            'content-transfer-encoding' => 'getContentTransferEncoding',
            'content-id' => 'getContentId'
        ];
        $lower = strtolower($name);
        if (isset($map[$lower])) {
            $headerValue = call_user_func([$part, $map[$lower]]);
        } elseif (!($part instanceof MimePart)) {
            return ($type === static::FILTER_INCLUDE);
        } else {
            $headerValue = $part->getHeaderValue($name);
        }
        
        return (($type === static::FILTER_EXCLUDE && strcasecmp($headerValue, $header) === 0)
            || ($type === static::FILTER_INCLUDE && strcasecmp($headerValue, $header) !== 0));
    }
1334007
static Map<String,Collection<String>> extractTextParts(Multipart multiPart) throws MessagingException, IOException {
        HashMap<String,Collection<String>> bodies = new HashMap<>();
        for(int i = 0; i < multiPart.getCount(); i++) {
            checkPartForTextType(bodies, multiPart.getBodyPart(i));
        }
        return bodies;
    }