Beispiel #1
0
def EmailResult(rcptto, content, mailfrom, mailhost):
    """Email results to the colletor.

  Args:
    rcptto: a string, email destination.
    content: a list, the content to send out to the collection point.
    mailfrom: a string, the from address for the mail.
    mailhost: a string, the destination to send mail through.
  """
    # Create a 'file' object type to Mime-ify.
    s = StringIO()
    s.write('\n'.join(content))

    # Mime-ify the message.
    msg = MIMEText(s.getvalue())
    s.close()

    msg['Subject'] = 'YT Diags (%s)' % datetime.strftime(
        datetime.utcnow(), '%Y/%m/%d %H:%M')
    msg['From'] = mailfrom
    msg['To'] = rcptto

    # Attempt to send the message.
    try:
        s = smtplib.SMTP(mailhost)
        s.sendmail(mailfrom, rcptto, msg.as_string())
        s.quit()
    except smtplib.SMTPConnectError as e:
        print('Failed to send mail, connect error: %s' % e)
        sys.exit(1)
    except smtplib.SMTPDataError as e:
        print('Failed to send mail, data error: %s' % e)
        sys.exit(1)
    except smtplib.SMTPException as e:
        print('Failed to send mail, Exception: %s' % e)
        sys.exit(1)
    except smtplib.SMTPHeloError as e:
        print('Failed to send mail, Error in Helo: %s' % e)
        sys.exit(1)
    except socket.error as e:
        print('Failed to connect to the smtp/mailhost: %s' % mailhost)
        print(msg.as_string())
        sys.exit(1)