How to use _process_to_string method in autotest

Best Python code snippet using autotest_python

mail.py

Source:mail.py Github

copy

Full Screen

...15except ImportError:16 import common17from autotest.client.shared.settings import settings18DEFAULT_FROM_EMAIL = "autotest-grid@no-reply.com"19def _process_to_string(to_string):20 """21 Process a string containing email addresses. Separators: ',' ';' ':'22 :param to_string: String containing email addresses.23 :return: List with email addresses.24 """25 return [x for x in re.split('\s|,|;|:', to_string) if x]26def send(from_address, to_addresses, cc_addresses, subject, body,27 smtp_info, html=None):28 """29 Send out an email.30 Args:31 from_address: The email address to put in the "From:" field.32 to_addresses: Either a single string or an iterable of33 strings to put in the "To:" field of the email.34 cc_addresses: Either a single string of an iterable of35 strings to put in the "Cc:" field of the email.36 subject: The email subject.37 body: The body of the email. there's no special38 handling of encoding here, so it's safest to39 stick to 7-bit ASCII text.40 smtp_info: Dictionary with SMTP info.41 html: Optional HTML content of the message.42 """43 # addresses can be a tuple or a single string, so make them tuples44 if isinstance(to_addresses, str):45 to_addresses = [to_addresses]46 else:47 to_addresses = list(to_addresses)48 if isinstance(cc_addresses, str):49 cc_addresses = [cc_addresses]50 else:51 cc_addresses = list(cc_addresses)52 if html:53 message = email.mime.multipart.MIMEMultipart('alternative')54 message["To"] = ", ".join(to_addresses)55 message["Cc"] = ", ".join(cc_addresses)56 message["From"] = from_address57 message["Subject"] = subject58 message.attach(email.mime.text.MIMEText(body, 'plain'))59 message.attach(email.mime.text.MIMEText(html, 'html'))60 else:61 message = email.Message.Message()62 message["To"] = ", ".join(to_addresses)63 message["Cc"] = ", ".join(cc_addresses)64 message["From"] = from_address65 message["Subject"] = subject66 message.set_payload(body)67 if not smtp_info['port']:68 smtp_info['port'] = 2569 try:70 if smtp_info['server']:71 mailer = smtplib.SMTP(smtp_info['server'], smtp_info['port'])72 else:73 logging.info("No SMTP server specified, will try to connect to "74 "the local MTA")75 mailer = smtplib.SMTP()76 try:77 if smtp_info.get('user'):78 mailer.login(smtp_info['user'], smtp_info['password'])79 mailer.sendmail(from_address, to_addresses + cc_addresses,80 message.as_string())81 finally:82 try:83 mailer.quit()84 except:85 logging.exception('mailer.quit() failed:')86 except Exception:87 logging.exception('Sending email failed:')88class EmailNotificationManager(object):89 """90 Email notification facility, for use in things like the autotest scheduler.91 This facility can use values defined in the autotest settings92 (global_config.ini) to conveniently send notification emails to the admin93 of an autotest module.94 """95 def __init__(self, module="scheduler"):96 """97 Initialize an email notification manager.98 :param subsystem: String describing the module this manager is99 handling. Example: 'scheduler'.100 """101 self.module = module102 self.email_queue = []103 self.html_email = settings.get_value("NOTIFICATION",104 "html_notify_email",105 type=bool,106 default=False)107 self.from_email = settings.get_value("NOTIFICATION",108 "notify_email_from",109 default=DEFAULT_FROM_EMAIL)110 self.grid_admin_email = settings.get_value("NOTIFICATION",111 "grid_admin_email",112 default='')113 server = settings.get_value("EMAIL", "smtp_server", default='localhost')114 port = settings.get_value("EMAIL", "smtp_port", default=None)115 user = settings.get_value("EMAIL", "smtp_user", default=None)116 password = settings.get_value("EMAIL", "smtp_password", default=None)117 self.smtp_info = {'server': server,118 'port': port,119 'user': user,120 'password': password}121 def set_module(self, module):122 """123 Change the name of the module we're notifying for.124 """125 self.module = module126 def send(self, to_string, subject, body):127 """128 Send emails to the addresses listed in to_string.129 to_string is split into a list which can be delimited by any of:130 ';', ',', ':' or any whitespace131 """132 to_list = _process_to_string(to_string)133 if not to_list:134 return135 send(from_address=self.from_email, to_addresses=to_list, cc_addresses="",136 subject=subject, body=body, smtp_info=self.smtp_info,137 html=self.html_email)138 def send_admin(self, subject, body):139 """140 Send an email to this grid admin.141 """142 self.send(self.grid_admin_email, subject, body)143 def enqueue_admin(self, subject, message):144 """145 Enqueue an email to the test grid admin.146 """...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run autotest automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful