def mail_message_to_mime_message(protocol_message): """Generate a MIMEMultitype message from protocol buffer. Generates a complete MIME multi-part email object from a MailMessage protocol buffer. The body fields are sent as individual alternatives if they are both present, otherwise, only one body part is sent. Multiple entry email fields such as 'To', 'Cc' and 'Bcc' are converted to a list of comma separated email addresses. Args: protocol_message: Message PB to convert to MIMEMultitype. Returns: MIMEMultitype representing the provided MailMessage. Raises: InvalidAttachmentTypeError when the file name of an attachment """ parts = [] if protocol_message.has_textbody(): parts.append(MIMEText.MIMEText(protocol_message.textbody())) if protocol_message.has_htmlbody(): parts.append(MIMEText.MIMEText(protocol_message.htmlbody(), _subtype='html')) if len(parts) == 1: payload = parts else: payload = [MIMEMultipart.MIMEMultipart('alternative', _subparts=parts)] result = MIMEMultipart.MIMEMultipart(_subparts=payload) for attachment in protocol_message.attachment_list(): file_name = attachment.filename() mime_type = _GetMimeType(file_name) maintype, subtype = mime_type.split('/') mime_attachment = MIMEBase.MIMEBase(maintype, subtype) mime_attachment.add_header('Content-Disposition', 'attachment', filename=attachment.filename()) mime_attachment.set_payload(attachment.data()) result.attach(mime_attachment) if protocol_message.to_size(): result['To'] = ', '.join(protocol_message.to_list()) if protocol_message.cc_size(): result['Cc'] = ', '.join(protocol_message.cc_list()) if protocol_message.bcc_size(): result['Bcc'] = ', '.join(protocol_message.bcc_list()) result['From'] = protocol_message.sender() result['Reply-To'] = protocol_message.replyto() result['Subject'] = protocol_message.subject() for header in protocol_message.header_list(): result[header.name()] = header.value() return result
def mail_message_to_mime_message(protocol_message): """Generate a MIMEMultitype message from protocol buffer. Generates a complete MIME multi-part email object from a MailMessage protocol buffer. The body fields are sent as individual alternatives if they are both present, otherwise, only one body part is sent. Multiple entry email fields such as 'To', 'Cc' and 'Bcc' are converted to a list of comma separated email addresses. Args: protocol_message: Message PB to convert to MIMEMultitype. Returns: MIMEMultitype representing the provided MailMessage. Raises: InvalidAttachmentTypeError when the file name of an attachment """ parts = [] if protocol_message.has_textbody(): parts.append( MIMEText.MIMEText(protocol_message.textbody(), _charset=_GuessCharset( protocol_message.textbody()))) if protocol_message.has_htmlbody(): parts.append( MIMEText.MIMEText(protocol_message.htmlbody(), _subtype='html', _charset=_GuessCharset( protocol_message.htmlbody()))) if len(parts) == 1: payload = parts else: payload = [MIMEMultipart.MIMEMultipart('alternative', _subparts=parts)] result = MIMEMultipart.MIMEMultipart(_subparts=payload) for attachment in protocol_message.attachment_list(): file_name = attachment.filename() mime_type = _GetMimeType(file_name) maintype, subtype = mime_type.split('/') mime_attachment = MIMEBase.MIMEBase(maintype, subtype) mime_attachment.add_header('Content-Disposition', 'attachment', filename=attachment.filename()) mime_attachment.set_payload(attachment.data()) if attachment.has_contentid(): mime_attachment['content-id'] = attachment.contentid() result.attach(mime_attachment) if protocol_message.to_size(): result['To'] = _I18nHeader(', '.join(protocol_message.to_list())) if protocol_message.cc_size(): result['Cc'] = _I18nHeader(', '.join(protocol_message.cc_list())) if protocol_message.bcc_size(): result['Bcc'] = _I18nHeader(', '.join(protocol_message.bcc_list())) result['From'] = _I18nHeader(protocol_message.sender()) result['Reply-To'] = _I18nHeader(protocol_message.replyto()) result['Subject'] = _I18nHeader(protocol_message.subject()) for header in protocol_message.header_list(): result[header.name()] = _I18nHeader(header.value()) return result
def mail_message_to_mime_message(protocol_message): """Generates a `MIMEMultipart` message from a `MailMessage` protocol buffer. This function generates a complete `MIMEMultipart` email object from a `MailMessage` protocol buffer. The body fields are sent as individual alternatives if they are both present; otherwise, only one body part is sent. Multiple entry email fields, such as 'To', 'Cc', and 'Bcc' are converted to a list of comma-separated email addresses. Args: protocol_message: Message protocol buffer to convert to a `MIMEMultipart` message. Returns: A `MIMEMultipart` message that represents the provided `MailMessage`. Raises: InvalidAttachmentTypeError: If the file type of the attachment is invalid. """ parts = [] if protocol_message.has_textbody(): parts.append(MIMEText.MIMEText( protocol_message.textbody(), _charset=_GuessCharset(protocol_message.textbody()))) if protocol_message.has_htmlbody(): parts.append(MIMEText.MIMEText( protocol_message.htmlbody(), _subtype='html', _charset=_GuessCharset(protocol_message.htmlbody()))) if len(parts) == 1: payload = parts else: payload = [MIMEMultipart.MIMEMultipart('alternative', _subparts=parts)] result = MIMEMultipart.MIMEMultipart(_subparts=payload) for attachment in protocol_message.attachment_list(): file_name = attachment.filename() mime_type = _GetMimeType(file_name) maintype, subtype = mime_type.split('/') mime_attachment = MIMEBase.MIMEBase(maintype, subtype) mime_attachment.add_header('Content-Disposition', 'attachment', filename=attachment.filename()) mime_attachment.set_payload(attachment.data()) if attachment.has_contentid(): mime_attachment['content-id'] = attachment.contentid() result.attach(mime_attachment) if protocol_message.to_size(): result['To'] = _I18nHeader(', '.join(protocol_message.to_list())) if protocol_message.cc_size(): result['Cc'] = _I18nHeader(', '.join(protocol_message.cc_list())) if protocol_message.bcc_size(): result['Bcc'] = _I18nHeader(', '.join(protocol_message.bcc_list())) result['From'] = _I18nHeader(protocol_message.sender()) result['Reply-To'] = _I18nHeader(protocol_message.replyto()) result['Subject'] = _I18nHeader(protocol_message.subject()) for header in protocol_message.header_list(): result[header.name()] = _I18nHeader(header.value()) return result