def test_string_empty(): """Method to test whether an empty string is correctly identified.""" assert string_empty('') is True assert string_empty(' ') is True assert string_empty('\t') is True assert string_empty('\n') is True assert string_empty(None) is False assert string_empty('some value') is False assert string_empty(' some value ') is False assert string_empty(0) is False assert string_empty([]) is False assert string_empty(False) is False
def test_string_empty(self): """Method to test whether an empty string is correctly identified.""" self.assertTrue(string_empty('')) self.assertTrue(string_empty(' ')) self.assertTrue(string_empty('\t')) self.assertTrue(string_empty('\n')) self.assertFalse(string_empty(None)) self.assertFalse(string_empty('some value')) self.assertFalse(string_empty(' some value ')) self.assertFalse(string_empty(0)) self.assertFalse(string_empty([])) self.assertFalse(string_empty(False))
def test_string_empty(self): """Method to test whether an empty string is correctly identified.""" with app.test_request_context(): self.assertTrue(string_empty('')) self.assertTrue(string_empty(' ')) self.assertFalse(string_empty('some value')) self.assertFalse(string_empty(' some value ')) self.assertFalse(string_empty(str)) self.assertFalse(string_empty(int)) self.assertFalse(string_empty(None))
def test_string_empty(self): """Method to test whether an empty string is correctly identified.""" with app.test_request_context(): self.assertTrue(string_empty('')) self.assertTrue(string_empty(' ')) self.assertFalse(string_empty('some value')) self.assertFalse(string_empty(' some value ')) self.assertFalse(string_empty(str)) self.assertFalse(string_empty(int)) self.assertFalse(string_empty(None))
def send_email(to, action, subject, html, attachments=None): """ Sends email and records it in DB """ from .tasks import send_email_task_sendgrid, send_email_task_smtp if not string_empty(to): email_service = get_settings()['email_service'] email_from_name = get_settings()['email_from_name'] if email_service == 'smtp': email_from = email_from_name + '<' + get_settings( )['email_from'] + '>' else: email_from = get_settings()['email_from'] payload = { 'to': to, 'from': email_from, 'subject': subject, 'html': html, 'attachments': attachments, } if not current_app.config['TESTING']: smtp_encryption = get_settings()['smtp_encryption'] if smtp_encryption == 'tls': smtp_encryption = 'required' elif smtp_encryption == 'ssl': smtp_encryption = 'ssl' elif smtp_encryption == 'tls_optional': smtp_encryption = 'optional' else: smtp_encryption = 'none' smtp_config = { 'host': get_settings()['smtp_host'], 'username': get_settings()['smtp_username'], 'password': get_settings()['smtp_password'], 'encryption': smtp_encryption, 'port': get_settings()['smtp_port'], } smtp_status = check_smtp_config(smtp_encryption) if smtp_status: if email_service == 'smtp': send_email_task_smtp.delay(payload=payload, headers=None, smtp_config=smtp_config) else: key = get_settings().get('sendgrid_key') if key: headers = { "Authorization": ("Bearer " + key), "Content-Type": "application/json", } payload['fromname'] = email_from_name send_email_task_sendgrid.delay(payload=payload, headers=headers, smtp_config=smtp_config) else: logging.exception( 'SMTP & sendgrid have not been configured properly' ) else: logging.exception( 'SMTP is not configured properly. Cannot send email.') # record_mail(to, action, subject, html) mail = Mail( recipient=to, action=action, subject=subject, message=html, time=datetime.utcnow(), ) save_to_db(mail, 'Mail Recorded') record_activity('mail_event', email=to, action=action, subject=subject) return True
def send_email(to, action, subject, html, attachments=None, bcc=None, reply_to=None): """ Sends email and records it in DB """ from .tasks import get_smtp_config, send_email_task_sendgrid, send_email_task_smtp if not MessageSettings.is_enabled(action): logger.info("Mail of type %s is not enabled. Hence, skipping...", action) return if isinstance(to, User): logger.warning('to argument should be an email string, not a User object') to = to.email if string_empty(to): logger.warning('Recipient cannot be empty') return False email_service = get_settings()['email_service'] email_from_name = get_settings()['email_from_name'] if email_service == 'smtp': email_from = email_from_name + '<' + get_settings()['email_from'] + '>' else: email_from = get_settings()['email_from'] payload = { 'to': to, 'from': email_from, 'subject': subject, 'html': html, 'attachments': attachments, 'bcc': bcc, 'reply_to': reply_to, } if not (current_app.config['TESTING'] or email_service == 'disable'): if email_service == 'smtp': smtp_status = check_smtp_config(get_smtp_config()) if smtp_status: send_email_task_smtp.delay(payload) else: logger.error('SMTP is not configured properly. Cannot send email.') elif email_service == 'sendgrid': key = get_settings().get('sendgrid_key') if key: payload['fromname'] = email_from_name send_email_task_sendgrid.delay(payload) else: logger.error('SMTP & sendgrid have not been configured properly') else: logger.error( 'Invalid Email Service Setting: %s. Skipping email', email_service ) else: logger.warning('Email Service is disabled in settings, so skipping email') mail_recorder = current_app.config['MAIL_RECORDER'] mail_recorder.record(payload) mail = Mail( recipient=to, action=action, subject=subject, message=html, ) save_to_db(mail, 'Mail Recorded') record_activity('mail_event', email=to, action=action, subject=subject) return True
def send_email(to, action, subject, html): """ Sends email and records it in DB """ if not string_empty(to): email_service = get_settings()['email_service'] email_from_name = get_settings()['email_from_name'] if email_service == 'smtp': email_from = email_from_name + '<' + get_settings( )['email_from'] + '>' else: email_from = get_settings()['email_from'] payload = { 'to': to, 'from': email_from, 'subject': subject, 'html': html } if not current_app.config['TESTING']: if email_service == 'smtp': smtp_encryption = get_settings()['smtp_encryption'] if smtp_encryption == 'tls': smtp_encryption = 'required' elif smtp_encryption == 'ssl': smtp_encryption = 'ssl' elif smtp_encryption == 'tls_optional': smtp_encryption = 'optional' else: smtp_encryption = 'none' config = { 'host': get_settings()['smtp_host'], 'username': get_settings()['smtp_username'], 'password': get_settings()['smtp_password'], 'encryption': smtp_encryption, 'port': get_settings()['smtp_port'], } from .tasks import send_mail_via_smtp_task send_mail_via_smtp_task.delay(config, payload) else: payload['fromname'] = email_from_name key = get_settings()['sendgrid_key'] if not key: print('Sendgrid key not defined') return headers = { "Authorization": ("Bearer " + key), "Content-Type": "application/json" } from .tasks import send_email_task send_email_task.delay(payload, headers) # record_mail(to, action, subject, html) mail = Mail(recipient=to, action=action, subject=subject, message=html, time=datetime.utcnow()) save_to_db(mail, 'Mail Recorded') record_activity('mail_event', email=to, action=action, subject=subject) return True
def send_email(to, action, subject, html): """ Sends email and records it in DB """ if not string_empty(to): email_service = get_settings()['email_service'] email_from_name = get_settings()['email_from_name'] if email_service == 'smtp': email_from = email_from_name + '<' + get_settings()['email_from'] + '>' else: email_from = get_settings()['email_from'] payload = { 'to': to, 'from': email_from, 'subject': subject, 'html': html } if not current_app.config['TESTING']: if email_service == 'smtp': smtp_encryption = get_settings()['smtp_encryption'] if smtp_encryption == 'tls': smtp_encryption = 'required' elif smtp_encryption == 'ssl': smtp_encryption = 'ssl' elif smtp_encryption == 'tls_optional': smtp_encryption = 'optional' else: smtp_encryption = 'none' config = { 'host': get_settings()['smtp_host'], 'username': get_settings()['smtp_username'], 'password': get_settings()['smtp_password'], 'encryption': smtp_encryption, 'port': get_settings()['smtp_port'], } from .tasks import send_mail_via_smtp_task send_mail_via_smtp_task.delay(config, payload) else: payload['fromname'] = email_from_name key = get_settings()['sendgrid_key'] if not key: print('Sendgrid key not defined') return headers = { "Authorization": ("Bearer " + key), "Content-Type": "application/json" } from .tasks import send_email_task send_email_task.delay(payload, headers) # record_mail(to, action, subject, html) mail = Mail( recipient=to, action=action, subject=subject, message=html, time=datetime.utcnow() ) save_to_db(mail, 'Mail Recorded') record_activity('mail_event', email=to, action=action, subject=subject) return True