def get_reporter_domains(self): "a set of domains which have reported domains" self.domains = set() for addr in self.db.issues.distinct("fields.reporter.emailAddress"): try: parsed_addr = address.EmailAddress(addr) self.domains.add(parsed_addr.hostname) except IndexError: logging.debug("Invalid Address", addr) except AssertionError: logging.debug("Invalid Address", addr)
def create_email(from_name, from_email, reply_to, nylas_uid, to_addr, cc_addr, bcc_addr, subject, html, in_reply_to, references, attachments): """ Creates a MIME email message (both body and sets the needed headers). Parameters ---------- from_name: string The name aka phrase of the sender. from_email: string The sender's email address. to_addr, cc_addr, bcc_addr: list of pairs (name, email_address), or None Message recipients. reply_to: tuple or None Indicates the mailbox in (name, email_address) format to which the author of the message suggests that replies be sent. subject : string a utf-8 encoded string html : string a utf-8 encoded string in_reply_to: string or None If this message is a reply, the Message-Id of the message being replied to. references: list or None If this message is a reply, the Message-Ids of prior messages in the thread. attachments: list of dicts, optional a list of dicts(filename, data, content_type) """ html = html if html else '' plaintext = html2text(html) # Create a multipart/alternative message msg = mime.create.multipart('alternative') msg.append(mime.create.text('plain', plaintext), mime.create.text('html', html)) # Create an outer multipart/mixed message if attachments: text_msg = msg msg = mime.create.multipart('mixed') # The first part is the multipart/alternative text part msg.append(text_msg) # The subsequent parts are the attachment parts for a in attachments: # Disposition should be inline if we add Content-ID msg.append( mime.create.attachment(a['content_type'], a['data'], filename=a['filename'], disposition='attachment')) msg.headers['Subject'] = subject if subject else '' # Gmail sets the From: header to the default sending account. We can # however set our own custom phrase i.e. the name that appears next to the # email address (useful if the user has multiple aliases and wants to # specify which to send as), see: http://lee-phillips.org/gmailRewriting/ # For other providers, we simply use name = '' from_addr = address.EmailAddress(from_name, from_email) msg.headers['From'] = from_addr.full_spec() # Need to set these headers so recipients know we sent the email to them # TODO(emfree): should these really be unicode? if to_addr: full_to_specs = [ _get_full_spec_without_validation(name, spec) for name, spec in to_addr ] msg.headers['To'] = u', '.join(full_to_specs) if cc_addr: full_cc_specs = [ _get_full_spec_without_validation(name, spec) for name, spec in cc_addr ] msg.headers['Cc'] = u', '.join(full_cc_specs) if bcc_addr: full_bcc_specs = [ _get_full_spec_without_validation(name, spec) for name, spec in bcc_addr ] msg.headers['Bcc'] = u', '.join(full_bcc_specs) if reply_to: # reply_to is only ever a list with one element msg.headers['Reply-To'] = _get_full_spec_without_validation( reply_to[0][0], reply_to[0][1]) add_nylas_headers(msg, nylas_uid) if in_reply_to: msg.headers['In-Reply-To'] = in_reply_to if references: msg.headers['References'] = '\t'.join(references) # Most ISPs set date automatically, but we need to set it here for those # which do not. The Date header is required and omitting it causes issues # with scoring in many spam systems like SpamAssassin # Set dates in UTC since we don't know the timezone of the sending user # +0000 means UTC, whereas -0000 means unsure of timezone utc_datetime = datetime.utcnow() day = utc_datetime.strftime('%a') date = utc_datetime.strftime('%d %b %Y %X') date_header = '{day}, {date} +0000\r\n'.format(day=day, date=date) msg.headers['Date'] = date_header rfcmsg = _rfc_transform(msg) return rfcmsg
def create_email(sender_name, sender_email, inbox_uid, to_addr, cc_addr, bcc_addr, subject, html, in_reply_to, references, attachments): """ Creates a MIME email message (both body and sets the needed headers). Parameters ---------- sender_name: string The name aka phrase of the sender. sender_email: string The sender's email address. to_addr, cc_addr, bcc_addr: list of pairs (name, email_address), or None Message recipients. subject : string a utf-8 encoded string html : string a utf-8 encoded string in_reply_to: string or None If this message is a reply, the Message-Id of the message being replied to. references: list or None If this message is a reply, the Message-Ids of prior messages in the thread. attachments: list of dicts, optional a list of dicts(filename, data, content_type) """ html = html if html else '' plaintext = html2text(html) # Create a multipart/alternative message msg = mime.create.multipart('alternative') msg.append(mime.create.text('text', plaintext), mime.create.text('html', html)) # Create an outer multipart/mixed message if attachments: text_msg = msg msg = mime.create.multipart('mixed') # The first part is the multipart/alternative text part msg.append(text_msg) # The subsequent parts are the attachment parts for a in attachments: # Disposition should be inline if we add Content-ID msg.append( mime.create.attachment(a['content_type'], a['data'], filename=a['filename'], disposition='attachment')) msg.headers['Subject'] = subject if subject else '' # Gmail sets the From: header to the default sending account. We can # however set our own custom phrase i.e. the name that appears next to the # email address (useful if the user has multiple aliases and wants to # specify which to send as), see: http://lee-phillips.org/gmailRewriting/ # For other providers, we simply use name = '' from_addr = address.EmailAddress(sender_name, sender_email) msg.headers['From'] = from_addr.full_spec() # Need to set these headers so recipients know we sent the email to them # TODO(emfree): should these really be unicode? if to_addr: full_to_specs = [ address.EmailAddress(name, spec).full_spec() for name, spec in to_addr ] msg.headers['To'] = u', '.join(full_to_specs) if cc_addr: full_cc_specs = [ address.EmailAddress(name, spec).full_spec() for name, spec in cc_addr ] msg.headers['Cc'] = u', '.join(full_cc_specs) if bcc_addr: full_bcc_specs = [ address.EmailAddress(name, spec).full_spec() for name, spec in cc_addr ] msg.headers['Bcc'] = u', '.join(full_bcc_specs) add_inbox_headers(msg, inbox_uid) if in_reply_to: msg.headers['In-Reply-To'] = in_reply_to if references: msg.headers['References'] = '\t'.join(references) rfcmsg = _rfc_transform(msg) return rfcmsg
def create_email(sender_name, sender_email, inbox_uid, recipients, subject, html, attachments): """ Creates a MIME email message (both body and sets the needed headers). Parameters ---------- sender_name: string The name aka phrase of the sender. sender_email: string The sender's email address. recipients: Recipients(to, cc, bcc) namedtuple to, cc, bcc are a lists of utf-8 encoded strings or None. subject : string a utf-8 encoded string html : string a utf-8 encoded string attachments: list of dicts, optional a list of dicts(filename, data, content_type) """ to = [address.EmailAddress(*addr) for addr in recipients.to] \ if recipients.to else [] cc = [address.EmailAddress(*addr) for addr in recipients.cc] \ if recipients.cc else [] bcc = [address.EmailAddress(*addr) for addr in recipients.bcc] \ if recipients.bcc else [] html = html if html else '' plaintext = html2text(html) # Create a multipart/alternative message msg = mime.create.multipart('alternative') msg.append(mime.create.text('text', plaintext), mime.create.text('html', html)) # Create an outer multipart/mixed message if attachments: text_msg = msg msg = mime.create.multipart('mixed') # The first part is the multipart/alternative text part msg.append(text_msg) # The subsequent parts are the attachment parts for a in attachments: # Disposition should be inline if we add Content-ID msg.append( mime.create.attachment(a['content_type'], a['data'], filename=a['filename'], disposition='attachment')) msg.headers['Subject'] = subject if subject else '' # Gmail sets the From: header to the default sending account. We can # however set our own custom phrase i.e. the name that appears next to the # email address (useful if the user has multiple aliases and wants to # specify which to send as), see: http://lee-phillips.org/gmailRewriting/ # For other providers, we simply use name = '' from_addr = address.EmailAddress(sender_name, sender_email) msg.headers['From'] = from_addr.full_spec() # Need to set these headers so recipients know we sent the email to them: # Note also that the To: header has different semantics than the envelope # recipient. For example, you can use '"Tony Meyer" <*****@*****.**>' # as an address in the To: header, but the envelope recipient must be only # '*****@*****.**'. msg.headers['To'] = u', '.join([addr.full_spec() for addr in to]) msg.headers['Cc'] = u', '.join([addr.full_spec() for addr in cc]) msg.headers['Bcc'] = u', '.join([addr.full_spec() for addr in bcc]) add_inbox_headers(msg, inbox_uid) return msg