def send_evaluation_email(self, etask): """Sends evaluation email appending issues to message log """ # clear message log fix for issue #99 self._msg_log = None if etask is None: logger.error('Task passed in is None') self._append_to_message_log('\nTask passed in is None\n') return if etask.is_external_submission() is False: logger.debug('Not an external submission, just returning') self._append_to_message_log('\nNot an external submission\n') return if self._participantdatabase is None: logger.error('Participant Database is None') self._append_to_message_log('\nParticipant database is None ' 'cannot send' ' docking evaluation email!!!\n') return try: emailer = self._get_smtp_emailer() to_list = self._get_external_submitter_email(etask) if to_list is None: logger.debug('No external submitter email, just returning') return subject, msg = self\ ._generate_external_submission_email_body(etask) from_addr = emailer.generate_from_address_using_login_and_host() reply_to = self._get_reply_to_address(from_addr) rmsd = Attachment(etask.get_rmsd_txt(), 'rmsd.txt') emailer.send_email(from_addr, to_list, subject, msg, reply_to=reply_to, attachments=[rmsd]) self._append_to_message_log('\nSent evaluation email to: ' + ", ".join(to_list) + '\n') except Exception as e: logger.exception('Caught exception') self._append_to_message_log('\nCaught exception trying to email ' 'participant : ' + str(e) + '\n')
def send_postevaluation_email(self, petask): """Sends evaluation email appending issues to message log """ # clear message log self._msg_log = None if petask is None: logger.error('Task passed in is None') self._append_to_message_log('\nTask passed in is None\n') return try: emailer = self._get_smtp_emailer() if self._to_list is None: logger.debug('No email addresses in to list') self._append_to_message_log('\nNo email addresses ' + 'in to list\n') return subject, msg = self._generate_post_evaluation_email_body(petask) from_addr = emailer.generate_from_address_using_login_and_host() reply_to = self._get_reply_to_address(from_addr) attach_list = None for csv_file in petask.get_all_csv_files_in_dir(): if attach_list is None: attach_list = [] attach_list.append( Attachment(csv_file, os.path.basename(csv_file))) emailer.send_email(from_addr, self._to_list, subject, msg, reply_to=reply_to, attachments=attach_list) self._append_to_message_log('\nSent post evaluation email to: ' + ", ".join(self._to_list) + '\n') except Exception as e: logger.exception('Caught exception') self._append_to_message_log('\nCaught exception trying to email ' ': ' + ', '.join(self._to_list) + ' : ' + str(e) + '\n')
def test_append_attachments(self): temp_dir = tempfile.mkdtemp() try: emailer = SmtpEmailer() # try no attachments msg_root = MIMEMultipart("alternative") res = emailer._append_attachments(msg_root, None) self.assertEqual(msg_root, res) # try 1 text attachment txtfile = os.path.join(temp_dir, 'my.txt') f = open(txtfile, 'w') f.write('hello') f.flush() f.close() msg_root = MIMEMultipart("alternative") a = Attachment(txtfile, 'renamed.txt') res = emailer._append_attachments(msg_root, [a]) res.as_string().index('Content-Type: text/plain; ' 'charset="us-ascii"') res.as_string().index('Content-Disposition: attachment; ' 'filename="renamed.txt"') res.as_string().index('hello') # try 1 image attachment img = os.path.join(temp_dir, 'yo.ppm') f = open(img, 'w') f.write('P6\n1 1\n255\n') f.write('%c' % 255) f.flush() f.close() msg_root = MIMEMultipart("alternative") b = Attachment(img, None) res = emailer._append_attachments(msg_root, [b]) res.as_string().index('Content-Type: image/x-portable-pixmap') res.as_string().index('Content-Disposition: attachment; ' 'filename="yo.ppm"') # try 1 gzip attachment mygz = os.path.join(temp_dir, 'foo.gz') f = gzip.open(mygz, 'wb') f.write('some compressed data') f.flush() f.close() c = Attachment(mygz, 'well.gz') res = emailer._append_attachments(msg_root, [c]) res.as_string().index('Content-Type: application/octet-stream') res.as_string().index('Content-Disposition: attachment; ' 'filename="well.gz"') # try 3 attachments above together msg_root = MIMEMultipart("alternative") res = emailer._append_attachments(msg_root, [a, b, c]) res.as_string().index('Content-Type: text/plain; ' 'charset="us-ascii"') res.as_string().index('Content-Disposition: attachment; ' 'filename="renamed.txt"') res.as_string().index('hello') res.as_string().index('Content-Type: image/x-portable-pixmap') res.as_string().index('Content-Disposition: attachment; ' 'filename="yo.ppm"') res.as_string().index('Content-Type: application/octet-stream') res.as_string().index('Content-Disposition: attachment; ' 'filename="well.gz"') finally: shutil.rmtree(temp_dir)
def test_attachment_class(self): a = Attachment('/foo', 'hi') self.assertEqual(a.get_desired_name(), 'hi') self.assertEqual(a.get_file_to_attach(), '/foo')