Esempio n. 1
0
def send_admin_credentials():
    config = current_app.config
    users = Users.get()
    username = users.username
    access_key = users.access_key

    smtp_server = config["SMTP_SERVER"]
    port = config["SMTP_PORT"]
    sender_email = config["SENDER_EMAIL"]
    receiver_email = config["RECEIVER_EMAIL"]
    password = config["EMAIL_PASSWORD"]

    subject = "[Speedtest Servers] Admin credentials"
    body = f"""
    Your credentials:

    Username: {username}
    Access Key: {access_key}
    """

    message = MIMEMultipart()
    message["From"] = "Speedtest Servers"
    message["To"] = receiver_email
    message["Subject"] = subject
    message.attach(MIMEText(body, "plain"))

    text = message.as_string()

    context = ssl.create_default_context()
    with SMTP(smtp_server, port) as server:
        server.starttls(context=context)
        server.login(sender_email, password)
        server.sendmail(sender_email, receiver_email, text)
Esempio n. 2
0
    def authorize_smtp(
        self,
        account: str = None,
        password: str = None,
        smtp_server: str = None,
        smtp_port: int = None,
    ) -> None:
        """Authorize to SMTP server.

        Can be called without giving any parameters if library
        has been initialized with necessary information and/or
        keyword ``set_credentials`` has been used.

        :param account: SMTP account name, defaults to None
        :param password: SMTP account password, defaults to None
        :param smtp_server: SMTP server address, defaults to None
        :param smtp_port: SMTP server port, defaults to None (587 for SMTP)

        Example:

        .. code-block:: robotframework

            Authorize SMTP    ${username}   ${password}  smtp.gmail.com  587

        """
        if account is None and password is None:
            account = self.account
            password = self.password
        if smtp_server is None:
            smtp_server = self.smtp_server
        if smtp_port is None:
            smtp_port = self.smtp_port
        else:
            smtp_port = int(smtp_port)
        if smtp_server and account and password:
            try:
                self.smtp_conn = SMTP(smtp_server, smtp_port)
                self.send_smtp_hello()
                try:
                    self.smtp_conn.starttls()
                except SMTPNotSupportedError:
                    self.logger.warning("TLS not supported by the server")
            except SMTPConnectError:
                context = ssl.create_default_context()
                self.smtp_conn = SMTP_SSL(smtp_server,
                                          smtp_port,
                                          context=context)
            self.smtp_conn.login(account, password)
        else:
            self.logger.warning(
                "Server address, account and password are needed for "
                "authentication with SMTP")
        if self.smtp_conn is None:
            self.logger.warning("Not able to establish SMTP connection")
Esempio n. 3
0
def send_email(messages, subject, password):

    message = MIMEMultipart()
    message['From'] = SENDER_EMAIL
    message['To'] = RECIVER_EMAIL
    message['Subject'] = subject

    part = MIMEText(messages, "plain")

    message.attach(part)

    context = ssl.create_default_context()

    try:
        with SMTP_SSL(SMTP_SERVER, EMAIL_PORT, context=context) as server:
            server.login(SENDER_EMAIL, password)
            server.sendmail(SENDER_EMAIL, RECIVER_EMAIL, message.as_string())
    except SMTPAuthenticationError as smt:
        logging.error("ERROR E-Mail not sent, logging error " +
                      str(smt.__repr__))
Esempio n. 4
0
def send_via_smtp(smtp_settings, to_emails, problems, verbose=False):
    """
    Send email via passed credentials

    """
    # Create a secure SSL context
    context = ssl.create_default_context()

    with SMTP_SSL(host=smtp_settings['host'],
                  port=smtp_settings['port'],
                  context=context) as smtp_server:
        smtp_server.login(smtp_settings['email'], smtp_settings['password'])
        if verbose:
            logger.info(
                f"EasyMon Alerts logged into {smtp_settings['host']} successfully!"
            )
            smtp_server.set_debuglevel(1)

        for to_email in to_emails:
            msg = get_email_message(smtp_settings['email'], to_email, problems)
            logger.info(
                f'EasyMon Alerts is going to send the email with details to {to_email}...'
            )
            smtp_server.send_message(msg)
Esempio n. 5
0
    def sendmail(self, args, smtphost, smtpport, smtptls, to_addr, sender_addr,
                 sender_password, clientid, access_token):

        if (len(to_addr) > 3 and '@' in to_addr):

            m = EmailMessage()

            # Header vars
            m['from'] = "Backup Routine <" + sender_addr + ">"
            m['to'] = to_addr
            m.set_default_type('text/plain')

            # Backup run mode detection
            subject = "Unknown operation"
            if args.backup:
                subject = "Backup"
            if args.verify:
                subject = "Verify"
            if args.checkpoint:
                subject = "Checkpoint"

            # SUCCESS or FAILURE including server hostname
            if (self.verr or self.cerr or self.terr):
                subject += " result for " + os.uname()[1] + ": FAILURE"
            else:
                subject += " result for " + os.uname()[1] + ": SUCCESS"

            # Set subject only once
            m['subject'] = subject

            # Message body
            m.set_content(self.vmsg + self.cmsg + self.tmsg)

            # Send the email
            if len(access_token) > 0:
                # GSuite or Google Accounts
                o = oauth2()
                access_token = o.get_access_token()

                auth_string = 'user=%s\1auth=Bearer %s\1\1' % (sender_addr,
                                                               access_token)
                auth_string = base64.b64encode(
                    auth_string.encode('ascii')).decode('ascii')

                smtp = SMTP(smtphost, 587)
                smtp.ehlo(clientid)
                smtp.starttls()
                smtp.docmd('AUTH', 'XOAUTH2 ' + auth_string)

            elif (smtptls):
                # Sending as default + TLS
                context = ssl.create_default_context()
                smtp = SMTP(smtphost, smtpport)
                smtp.ehlo()
                smtp.starttls(context=context)
                smtp.login(sender_addr, sender_password)
            else:
                # Sending as default unencrypted
                context = ssl.create_default_context()
                smtp = SMTP_SSL(smtphost, smtpport, context=context)
                smtp.ehlo()
                smtp.login(sender_addr, sender_password)

            smtp.send_message(m, sender_addr, to_addr)
            smtp.quit()
        else:
            print(
                "destination e-mail is malformed. Check config.\n e-mail supplied was:"
                + to_addr)