def send_response(recipient, from_address, subject, body_text, body_html, replyid, attachments): ''' Send email back to the user that sent feedback. On error, raises exception. `replyid` may be None. `attachments` may be None. ''' # TODO: Use SMTP rather than SES if we have attachments SES or SMTP (get@ style). # DISABLING ATTACHMENTS if attachments: return extra_headers = {'Reply-To': from_address} if replyid: extra_headers['In-Reply-To'] = replyid extra_headers['References'] = replyid body = [] if body_text: body.append(('plain', body_text)) if body_html: body.append(('html', body_html)) raw_email = sendmail.create_raw_email(recipient, from_address, subject, body, attachments, extra_headers) if not raw_email: return try: sendmail.send_raw_email_amazonses(raw_email, from_address) except: raise
def send_response(recipient, from_address, subject, body_text, body_html, replyid, attachments): ''' Send email back to the user that sent feedback. On error, raises exception. `replyid` may be None. `attachments` may be None. ''' # TODO: Use SMTP rather than SES if we have attachments SES or SMTP (get@ style). # DISABLING ATTACHMENTS if attachments: return extra_headers = {'Reply-To': from_address} if replyid: extra_headers['In-Reply-To'] = replyid extra_headers['References'] = replyid body = [] if body_text: body.append(('plain', body_text)) if body_html: body.append(('html', body_html)) raw_email = sendmail.create_raw_email(recipient, from_address, subject, body, attachments, extra_headers) if not raw_email: return # SES has a send size limit, above which it'll reject the email. If our raw_email is # larger than that, we'll discard the HTML version. if len(raw_email) > _SES_EMAIL_SIZE_LIMIT: body.pop() # discard HTML raw_email = sendmail.create_raw_email(recipient, from_address, subject, body, attachments, extra_headers) if not raw_email: return try: sendmail.send_raw_email_amazonses(raw_email, from_address, recipient) except: raise
def process_email(self, email_string): ''' Processes the given email and sends a response. Returns True if successful, False or exception otherwise. ''' self._email_string = email_string if not self._parse_email(email_string): return False # Look up all config entries matching the requested address. request_conf = [ item for item in self._conf if item['email_addr'] == self.requested_addr ] # If we didn't find anything for that address, exit. if not request_conf: logger.info('fail: invalid requested address: %s', self.requested_addr) return False # Check if the user is (or should be) blacklisted if not self._check_blacklist(): logger.info('fail: blacklist') return False # Process each config entry found the for the requested address separately. # Don't fail out early, since the other email send method has a chance # to succeed even if one fails. (I.e., SMTP will succeed even if there's # a SES service problem.) full_success = True exception_to_raise = None for conf in request_conf: attachments = None if conf['attachments']: attachments = [] for attachment_info in conf['attachments']: bucketname, bucket_filename, attachment_filename = attachment_info attachments.append((aws_helpers.get_s3_attachment( settings.ATTACHMENT_CACHE_DIR, bucketname, bucket_filename), attachment_filename)) extra_headers = { 'Reply-To': self.requested_addr, 'Auto-Submitted': 'auto-replied' } if self._requester_msgid: extra_headers['In-Reply-To'] = self._requester_msgid extra_headers['References'] = self._requester_msgid raw_response = sendmail.create_raw_email(self._requester_addr, self._response_from_addr, self._subject, conf['body'], attachments, extra_headers) if not raw_response: full_success = False continue if conf.get('send_method', '').upper() == 'SES': # If sending via SES, we'll use its DKIM facility -- so don't do it here. try: if not sendmail.send_raw_email_amazonses( raw_response, self._response_from_addr): return False except BotoServerError as ex: if ex.error_message == 'Address blacklisted.': logger.critical( 'fail: requester address blacklisted by SES') else: exception_to_raise = ex full_success = False continue else: raw_response = _dkim_sign_email(raw_response) if not sendmail.send_raw_email_smtp( raw_response, settings.COMPLAINTS_ADDRESS, # will be Return-Path self._requester_addr): full_success = False continue if exception_to_raise: raise exception_to_raise return full_success
def process_email(self, email_string): ''' Processes the given email and sends a response. Returns True if successful, False or exception otherwise. ''' self._email_string = email_string if not self._parse_email(email_string): return False # Look up all config entries matching the requested address. request_conf = [item for item in self._conf if item['email_addr'] == self.requested_addr] # If we didn't find anything for that address, exit. if not request_conf: logger.info('fail: invalid requested address: %s', self.requested_addr) return False # Check if the user is (or should be) blacklisted if not self._check_blacklist(): logger.info('fail: blacklist') return False # Process each config entry found the for the requested address separately. # Don't fail out early, since the other email send method has a chance # to succeed even if one fails. (I.e., SMTP will succeed even if there's # a SES service problem.) full_success = True exception_to_raise = None for conf in request_conf: attachments = None if conf['attachments']: attachments = [] for attachment_info in conf['attachments']: bucketname, bucket_filename, attachment_filename = attachment_info attachments.append((aws_helpers.get_s3_attachment(settings.ATTACHMENT_CACHE_DIR, bucketname, bucket_filename), attachment_filename)) extra_headers = {'Reply-To': self.requested_addr} if self._requester_msgid: extra_headers['In-Reply-To'] = self._requester_msgid extra_headers['References'] = self._requester_msgid raw_response = sendmail.create_raw_email(self._requester_addr, self._response_from_addr, self._subject, conf['body'], attachments, extra_headers) if not raw_response: full_success = False continue if conf.get('send_method', '').upper() == 'SES': # If sending via SES, we'll use its DKIM facility -- so don't do it here. try: if not sendmail.send_raw_email_amazonses(raw_response, self._response_from_addr): return False except BotoServerError as ex: if ex.error_message == 'Address blacklisted.': logger.critical('fail: requester address blacklisted by SES') else: exception_to_raise = ex full_success = False continue else: raw_response = _dkim_sign_email(raw_response) if not sendmail.send_raw_email_smtp(raw_response, settings.COMPLAINTS_ADDRESS, # will be Return-Path self._requester_addr): full_success = False continue if exception_to_raise: raise exception_to_raise return full_success