def format_email(comment, config): recipient = config['mail_to'] sender_address = config['mail_from'] sender_name = comment['commenter'] sender = _formataddr((sender_name, sender_address)) subject = 'Comment on ' + comment['event']['event_name'] body = "{text}\n\n{url}".format( text=comment['text'], url=comment['event']['event_url'] ) url_as_bytes = comment['event']['event_url'].encode('utf-8') first_part = hashlib.md5(url_as_bytes).hexdigest() host = socket.getfqdn() extra_headers = { 'References': '<{}@{}>'.format(first_part, host) } return get_message( sender, recipient, subject, body, extra_headers=extra_headers )
def get_message(sender, recipient, subject, body, content_type="plain", extra_headers=None): """Generate a `Message` instance. All arguments should be Unicode strings (plain ASCII works as well). Only the real name part of sender and recipient addresses may contain non-ASCII characters. The email will be properly MIME encoded. The charset of the email will be the first one out of the list that can represent all the characters occurring in the email. """ # Split real name (which is optional) and email address parts sender_name, sender_addr = _parseaddr(sender) recipient_name, recipient_addr = _parseaddr(recipient) sender_encoding = guess_encoding(sender_name) recipient_encoding = guess_encoding(recipient_name) subject_encoding = guess_encoding(subject) body_encoding = guess_encoding(body) # We must always pass Unicode strings to Header, otherwise it will # use RFC 2047 encoding even on plain ASCII strings. sender_name = str(_Header(sender_name, sender_encoding).encode()) recipient_name = str(_Header(recipient_name, recipient_encoding).encode()) # Make sure email addresses do not contain non-ASCII characters sender_addr.encode('ascii') recipient_addr.encode('ascii') # Create the message ('plain' stands for Content-Type: text/plain) message = _MIMEText(body, content_type, body_encoding) message['From'] = _formataddr((sender_name, sender_addr)) message['To'] = _formataddr((recipient_name, recipient_addr)) message['Subject'] = _Header(subject, subject_encoding) if extra_headers: for key, value in extra_headers.items(): encoding = guess_encoding(value) message[key] = _Header(value, encoding) return message
def _new_digest(self): digest = _MIMEMultipart('digest') digest['To'] = _formataddr(_parseaddr(self.to)) # Encodes with utf-8 as necessary digest['Subject'] = 'digest for {}'.format(self.name) digest['Message-ID'] = '<{0}@{1}>'.format(_uuid.uuid4(), platform.node()) digest['User-Agent'] = self.user_agent digest['List-ID'] = '<{}.localhost>'.format(self.name) digest['List-Post'] = 'NO (posting not allowed on this list)' digest['X-RSS-Feed'] = self.url return digest
def get_message(sender, recipient, subject, body, content_type, extra_headers=None, config=None, section='DEFAULT'): """Generate a `Message` instance. All arguments should be Unicode strings (plain ASCII works as well). Only the real name part of sender and recipient addresses may contain non-ASCII characters. The email will be properly MIME encoded. The charset of the email will be the first one out of the list that can represent all the characters occurring in the email. >>> message = get_message( ... sender='John <*****@*****.**>', recipient='Ζεύς <*****@*****.**>', ... subject='Testing', ... body='Hello, world!\\n', ... content_type='plain', ... extra_headers={'Approved': '*****@*****.**'}) >>> print(message.as_string()) # doctest: +REPORT_UDIFF MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit From: John <*****@*****.**> To: =?utf-8?b?zpbOtc+Nz4I=?= <*****@*****.**> Subject: Testing Approved: [email protected] <BLANKLINE> Hello, world! <BLANKLINE> """ if config is None: config = _config.CONFIG if section not in config.sections(): section = 'DEFAULT' encodings = [ x.strip() for x in config.get(section, 'encodings').split(',')] # Split real name (which is optional) and email address parts sender_name,sender_addr = _parseaddr(sender) recipient_name,recipient_addr = _parseaddr(recipient) sender_encoding = guess_encoding(sender_name, encodings) recipient_encoding = guess_encoding(recipient_name, encodings) subject_encoding = guess_encoding(subject, encodings) body_encoding = guess_encoding(body, encodings) # We must always pass Unicode strings to Header, otherwise it will # use RFC 2047 encoding even on plain ASCII strings. sender_name = str(_Header(sender_name, sender_encoding).encode()) recipient_name = str(_Header(recipient_name, recipient_encoding).encode()) # Make sure email addresses do not contain non-ASCII characters sender_addr.encode('ascii') recipient_addr.encode('ascii') # Create the message ('plain' stands for Content-Type: text/plain) message = _MIMEText(body, content_type, body_encoding) message['From'] = _formataddr((sender_name, sender_addr)) message['To'] = _formataddr((recipient_name, recipient_addr)) message['Subject'] = _Header(subject, subject_encoding) if config.getboolean(section, 'use-8bit'): del message['Content-Transfer-Encoding'] charset = _Charset(body_encoding) charset.body_encoding = _email_encoders.encode_7or8bit message.set_payload(body, charset=charset) if extra_headers: for key,value in extra_headers.items(): encoding = guess_encoding(value, encodings) message[key] = _Header(value, encoding) return message
def _get_entry_email(self, parsed, entry): """Get the best From email address ('John <*****@*****.**>') """ name = self._get_entry_name(parsed=parsed, entry=entry) address = self._get_entry_address(parsed=parsed, entry=entry) return _formataddr((name, address))
def get_message(sender, recipient, subject, body, content_type, extra_headers=None, config=None, section='DEFAULT'): """Generate a `Message` instance. All arguments should be Unicode strings (plain ASCII works as well). Only the real name part of sender and recipient addresses may contain non-ASCII characters. The email will be properly MIME encoded. The charset of the email will be the first one out of the list that can represent all the characters occurring in the email. >>> message = get_message( ... sender='John <*****@*****.**>', recipient='Ζεύς <*****@*****.**>', ... subject='Testing', ... body='Hello, world!\\n', ... content_type='plain', ... extra_headers={'Approved': '*****@*****.**'}) >>> print(message.as_string()) # doctest: +REPORT_UDIFF MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit From: John <*****@*****.**> To: =?utf-8?b?zpbOtc+Nz4I=?= <*****@*****.**> Subject: Testing Approved: [email protected] <BLANKLINE> Hello, world! <BLANKLINE> """ if config is None: config = _config.CONFIG if section not in config.sections(): section = 'DEFAULT' encodings = [ x.strip() for x in config.get(section, 'encodings').split(',') ] # Split real name (which is optional) and email address parts sender_name, sender_addr = _parseaddr(sender) recipient_list = [] for recipient_name, recipient_addr in _getaddresses([recipient]): recipient_encoding = guess_encoding(recipient_name, encodings) recipient_name = str( _Header(recipient_name, recipient_encoding).encode()) recipient_addr.encode('ascii') recipient_list.append(_formataddr((recipient_name, recipient_addr))) sender_encoding = guess_encoding(sender_name, encodings) recipient_encoding = guess_encoding(recipient_name, encodings) subject_encoding = guess_encoding(subject, encodings) body_encoding = guess_encoding(body, encodings) # We must always pass Unicode strings to Header, otherwise it will # use RFC 2047 encoding even on plain ASCII strings. sender_name = str(_Header(sender_name, sender_encoding).encode()) # Make sure email addresses do not contain non-ASCII characters sender_addr.encode('ascii') # Create the message ('plain' stands for Content-Type: text/plain) message = _MIMEText(body, content_type, body_encoding) message['From'] = _formataddr((sender_name, sender_addr)) message['To'] = ', '.join(recipient_list) message['Subject'] = _Header(subject, subject_encoding) if config.getboolean(section, 'use-8bit'): del message['Content-Transfer-Encoding'] charset = _Charset(body_encoding) charset.body_encoding = _email_encoders.encode_7or8bit message.set_payload(body, charset=charset) if extra_headers: for key, value in extra_headers.items(): encoding = guess_encoding(value, encodings) message[key] = _Header(value, encoding) return message