def create_draft(self, subject="", to="", cc="", bcc="", content="", draft_folder=None): """Create a draft message and save it to user's draft folder Args: subject (string): the subject line of the draft message to (a single instance|list of string|Contact): addresses that go in to field cc (a single instance|list of string|Contact): addresses that go in cc field bcc (a single instance|list of string|Contact): addresses that go in bcc field content (string): content of the draft message draft_folder (string): a name of draft folder """ new_message = MIMEMultipart('alternative') new_message["Subject"] = subject to = format_email_address(to) cc = format_email_address(cc) bcc = format_email_address(bcc) new_message["To"] = to new_message["Cc"] = cc new_message["Bcc"] = bcc # new_message.set_payload(content.encode('utf-8')) if "text" in content and "html" in content: part1 = MIMEText(content["text"].encode('utf-8'), 'plain') part2 = MIMEText(content["html"].encode('utf-8'), 'html') new_message.attach(part1) new_message.attach(part2) else: part1 = MIMEText(content.encode('utf-8'), 'plain') new_message.attach(part1) if not self.is_simulate: try: if draft_folder is not None: self._imap_client.append(draft_folder, str(new_message)) elif self._imap_account.is_gmail: self._imap_client.append('[Gmail]/Drafts', str(new_message)) else: import imapclient drafts = self._imap_client.find_special_folder( imapclient.DRAFTS) if drafts is not None: self._imap_client.append(drafts, str(new_message)) except IMAPClient.Error, e: logger.critical('create_draft() failed') return logger.debug("create_draft(): Your draft %s has been created" % subject)
def forward(self, to=[], cc=[], bcc=[], content=""): if not self.is_simulate: to = format_email_address(to) cc = format_email_address(cc) bcc = format_email_address(bcc) new_message_wrapper = self._create_message_instance("Fwd: " + self.subject, to, cc, bcc, content) if new_message_wrapper: self._send_message(new_message_wrapper)
def forward(self, to=[], cc=[], bcc=[], content=""): to = format_email_address(to) cc = format_email_address(cc) bcc = format_email_address(bcc) new_message_wrapper = self._create_message_instance( "Fwd: " + self.subject, to, cc, bcc, content) if not self._is_simulate: if new_message_wrapper: from engine.models.mailbox import MailBox # noqa: F401 ignore unused we use it for typing mailbox = MailBox(self._schema.imap_account, self._imap_client) mailbox._send_message( new_message_wrapper )
def _create_message_wrapper(self, subject="", to="", cc="", bcc="", content="", content_html=""): new_message = MIMEMultipart('alternative') new_message["Subject"] = subject to = format_email_address(to) cc = format_email_address(cc) bcc = format_email_address(bcc) new_message["To"] = to new_message["Cc"] = cc new_message["Bcc"] = bcc header_charset = 'ISO-8859-1' # We must choose the body charset manually for body_charset in 'US-ASCII', 'ISO-8859-1', 'UTF-8': try: content.encode(body_charset) except UnicodeError: pass else: break # We must choose the body charset manually for body_charset2 in 'US-ASCII', 'ISO-8859-1', 'UTF-8': try: content_html.encode(body_charset2) except UnicodeError: pass else: break part1 = MIMEText(content.encode(body_charset), 'plain', body_charset) new_message.attach(part1) if content_html: part2 = MIMEText(content_html.encode(body_charset2), 'html', body_charset2) new_message.attach(part2) return new_message
def reply(self, to=[], cc=[], bcc=[], content=""): # type: (t.Iterable[t.AnyStr], t.Iterable[t.AnyStr], t.Iterable[t.AnyStr], t.AnyStr) -> None """Reply to the sender of this message """ if not self.is_simulate: to_addr = "" if isinstance(to, list): to_addr = to.append(self._schema.from_) to = format_email_address(to_addr) else: to = format_email_address([self._schema.from_, to]) cc = format_email_address(cc) bcc = format_email_address(bcc) new_message_wrapper = self._create_message_instance("Re: " + self.subject, to, cc, bcc, content) if new_message_wrapper: self._send_message(new_message_wrapper)
def reply(self, to=[], cc=[], bcc=[], content=""): # type: (t.Iterable[t.AnyStr], t.Iterable[t.AnyStr], t.Iterable[t.AnyStr], t.AnyStr) -> None """Reply to the sender of this message """ if not self._is_simulate: to_addr = "" if isinstance(to, list): to_addr = to.append(self._schema.from_) to = format_email_address(to_addr) else: to = format_email_address([self._schema.from_, to]) cc = format_email_address(cc) bcc = format_email_address(bcc) new_message_wrapper = self._create_message_instance( "Re: " + self.subject, to, cc, bcc, content) if new_message_wrapper: from engine.models.mailbox import MailBox # noqa: F401 ignore unused we use it for typing mailbox = MailBox(self._schema.imap_account, self._imap_client) mailbox._send_message( new_message_wrapper )