def send_email_data(mailserver, subject, message, mail_from, recipient, username=None, password=None, port=25, email_tls=False): """ Send an email via the given email configuration data. :param mailserver: The mailserver :param subject: The subject of the email :param message: The body of the email :param mail_from: The sender of the email :param recipient: The recipient of the email :param username: The mailuser, if the SMTP server requires authentication :param password: The password, if the SMTP server requires authentication :param port: The mail server port :param email_tls: If the mailserver requires TLS :type email_tls: bool :return: True or False """ dbserver = SMTPServerDB(identifier="emailtoken", server=mailserver, sender=mail_from, username=username, password=password, port=port, tls=email_tls) smtpserver = SMTPServer(dbserver) return smtpserver.send_email(recipient, subject, message)
def add_smtpserver(identifier, server, port=25, username="", password="", sender="", description="", tls=False): """ This adds an smtp server to the smtp server database table. If the "identifier" already exists, the database entry is updated. :param identifier: The identifier or the name of the SMTPServer definition. As the identifier is unique, providing an identifier will return a list with either one or no smtpserver :type identifier: basestring :param server: The FQDN or IP address of the smtpserver :type server: basestring :return: The Id of the database object """ cryptedPassword = encryptPassword(password) r = SMTPServerDB(identifier=identifier, server=server, port=port, username=username, password=cryptedPassword, sender=sender, description=description, tls=tls).save() return r
def test(): """ Test the email configuration :return: """ param = request.all_data identifier = getParam(param, "identifier", required) server = getParam(param, "server", required) port = int(getParam(param, "port", default=25)) username = getParam(param, "username", default="") password = getParam(param, "password", default="") sender = getParam(param, "sender", default="") tls = getParam(param, "tls", default=False) tls = tls in [True, "True", "true", "1"] recipient = getParam(param, "recipient", required) s = SMTPServerDB(identifier=identifier, server=server, port=port, username=username, password=password, sender=sender, tls=tls) r = SMTPServer.test_email( s, recipient, "Test Email from privacyIDEA", "This is a test email from privacyIDEA. " "The configuration %s is working." % identifier) g.audit_object.log({'success': r > 0, 'info': r}) return send_result(r > 0)
def test_05_test_email(self): smtpmock.setdata(response={"*****@*****.**": (200, "OK")}, support_tls=True) identifier = "newConfig" server = "mailsever" port = 25 username = "******" password = "******" sender = "*****@*****.**" tls = True recipient = "*****@*****.**" s = SMTPServerDB(identifier=identifier, server=server, port=port, username=username, password=password, sender=sender, tls=tls) r = SMTPServer.test_email( s, recipient, "Test Email from privacyIDEA", "This is a test email from privacyIDEA. " "The configuration %s is working." % identifier)