"""
usernames_string = ",".join(usernames)
log.info("Sending emails to %s", usernames_string)
- server = smtplib.SMTP('localhost')
- hostname = socket.gethostname()
- users = [username + "@" + hostname for username in usernames]
+ with smtplib.SMTP('localhost') as server:
+ hostname = socket.gethostname()
+ users = [username + "@" + hostname for username in usernames]
- for email in emails:
- log.info("Sending email %s", email)
- with open(os.path.join(self.source_path, email), 'rb') as f:
- email_content = f.read()
- server.sendmail(self.smtp_sender, users, email_content)
- server.quit()
+ for email in emails:
+ log.info("Sending email %s", email)
+ with open(os.path.join(self.source_path, email), 'rb') as f:
+ email_content = f.read()
+ server.sendmail(self.smtp_sender, users, email_content)
# Wait till SMTP queue is processed
arnied_wrapper.wait_for_email_transfer()
"""
target_paths = self._extract_email_paths(self.target_path, emails, timeout)
for email_path in target_paths:
- email_file = open(email_path, "r")
- verified_email = Parser().parse(email_file, headersonly=True)
- log.debug("Extracted email headers:\n%s", verified_email)
- email_file.close()
+ with open(email_path, "r") as email_file:
+ verified_email = Parser().parse(email_file, headersonly=True)
+ log.debug("Extracted email headers:\n%s", verified_email)
log.info("Checking header '%s' in %s", header, email_path)
if not present_values:
"""
target_paths = self._extract_email_paths(self.target_path, emails, timeout)
for email_path in target_paths:
- email_file = open(email_path, "r")
- verified_email = Parser().parse(email_file)
+ with open(email_path, "r") as email_file:
+ verified_email = Parser().parse(email_file)
log.debug("Extracted email content:\n%s", verified_email)
content = ""
for part in verified_email.walk():
content = content.decode()
# NOTE: only one such element is expected
break
- email_file.close()
log.info("Checking content '%s' in %s", content_type, email_path)
if not present_values:
"""
text = 'This is an autogenerated email.\n'
- server = smtplib.SMTP('localhost')
hostname = socket.gethostname()
user = username + "@" + hostname
log.debug("Sending message from %s to %s" %
(self.smtp_sender, user))
- server.sendmail(self.smtp_sender, user, msg.as_string())
- server.close()
+ with smtplib.SMTP('localhost') as server:
+ server.sendmail(self.smtp_sender, user, msg.as_string())
if isinstance(wait_for_transfer, int):
arnied_wrapper.wait_for_email_transfer(timeout=wait_for_transfer)
def _default_compare_emails(self, source_email_path, target_email_path, tolerance=1):
"""Use python provided diff functionality to compare target emails with source ones."""
- source_email_file = open(source_email_path, "r")
- target_email_file = open(target_email_path, "r")
- source_email = source_email_file.read()
- target_email = target_email_file.read()
+ with open(source_email_path, "r") as source_email_file:
+ source_email = source_email_file.read()
+ with open(target_email_path, "r") as target_email_file:
+ target_email = target_email_file.read()
s = difflib.SequenceMatcher(None, source_email, target_email)
diffratio = s.ratio()
log.debug("Target message comparison ratio is %s.", diffratio)