Example #1
0
    def send(self,
             to,
             text_body,
             html_body=None,
             subject=None,
             tags=None,
             metadata=None,
             cc=None,
             bcc=None,
             headers=None,
             important=None):
        """Send the e-mail.

        :param to: Recipient of the e-mail.
        :param text_body: Plain text e-mail body.
        :param html_body: Rich HTML e-mail body.
        :param subject:
            Subject. If not provided, the class instance variable will be used.
        :param tags: list of mandrill tags
        :param metadata: dict of mandrill metadata
        :param cc: list of emails this message should be CC'd to
        :param bcc: list of emails this message should be BCC'd to
        """

        from_combined = '%s <%s>' % (
            self.from_name,
            self.from_email
        ) if self.from_name else self.from_email

        cc = cc if cc is not None else self.cc
        bcc = bcc if bcc is not None else self.bcc
        headers = headers if headers is not None else self.headers

        message = EmailMultiAlternatives(subject or self.subject,
                                         text_body,
                                         from_combined,
                                         [to],
                                         cc=cc,
                                         bcc=bcc,
                                         headers=headers)

        if html_body:
            message.attach_alternative(html_body, "text/html")

        # Attach any files.
        for filename, (data, mime_type) in self.attachments.items():
            message.attach(filename, data, mime_type)

        # Optional Mandrill-specific extensions:
        if tags:
            message.tags = tags
        if metadata:
            message.metadata = metadata

        if important is None:
            important = self.important
        if important is not None:
            message.important = important

        # Send the message.
        message.send()