def send(message, email_to, test=None): """ Send message if message non-empty and email-parms are set """ # we should not use CFG if we are testing. we should use values # from UI instead. if test: email_server = test.get('email_server') email_from = test.get('email_from') email_account = test.get('email_account') email_pwd = test.get('email_pwd') if email_pwd and not email_pwd.replace('*', ''): # If all stars, get stored password instead email_pwd = cfg.email_pwd() else: email_server = cfg.email_server() email_from = cfg.email_from() email_account = cfg.email_account() email_pwd = cfg.email_pwd() # email_to is replaced at send_with_template, since it can be an array if not message.strip('\n\r\t '): return "Skipped empty message" if email_server and email_to and email_from: message = _prepare_message(message) server, port = split_host(email_server) if not port: port = 25 logging.debug("Connecting to server %s:%s", server, port) try: mailconn = ssmtplib.SMTP_SSL(server, port) mailconn.ehlo() logging.debug("Connected to server %s:%s", server, port) except Exception, errorcode: if errorcode[0]: # Non SSL mail server logging.debug("Non-SSL mail server detected " \ "reconnecting to server %s:%s", server, port) try: mailconn = smtplib.SMTP(server, port) mailconn.ehlo() except: return errormsg(T('Failed to connect to mail server')) else: return errormsg(T('Failed to connect to mail server')) # TLS support if mailconn.ehlo_resp: m = re.search('STARTTLS', mailconn.ehlo_resp, re.IGNORECASE) if m: logging.debug("TLS mail server detected") try: mailconn.starttls() mailconn.ehlo() except: return errormsg(T('Failed to initiate TLS connection')) # Authentication if (email_account != "") and (email_pwd != ""): try: mailconn.login(email_account, email_pwd) except smtplib.SMTPHeloError: return errormsg( T("The server didn't reply properly to the helo greeting")) except smtplib.SMTPAuthenticationError: return errormsg(T("Failed to authenticate to mail server")) except smtplib.SMTPException: return errormsg( T("No suitable authentication method was found")) except: return errormsg( T("Unknown authentication failure in mail server")) try: mailconn.sendmail(email_from, email_to, message) msg = None except smtplib.SMTPHeloError: msg = errormsg( 'The server didn\'t reply properly to the helo greeting.') except smtplib.SMTPRecipientsRefused: msg = errormsg( 'The server rejected ALL recipients (no mail was sent).') except smtplib.SMTPSenderRefused: msg = errormsg('The server didn\'t accept the from_addr.') except smtplib.SMTPDataError: msg = errormsg( 'The server replied with an unexpected error code (other than a refusal of a recipient).' ) except: msg = errormsg(T('Failed to send e-mail')) try: mailconn.close() except: errormsg(T('Failed to close mail connection')) if msg: return msg else: logging.info("Notification e-mail succesfully sent") return T('Email succeeded')
def send(message, recipient): """ Send message if message non-empty and email-parms are set """ if not message.strip('\n\r\t '): return "Skipped empty message" if cfg.email_server() and recipient and cfg.email_from(): message = _prepare_message(message) server, port = split_host(cfg.email_server()) if not port: port = 25 logging.debug("Connecting to server %s:%s", server, port) try: mailconn = ssmtplib.SMTP_SSL(server, port) mailconn.ehlo() logging.debug("Connected to server %s:%s", server, port) except Exception, errorcode: if errorcode[0]: # Non SSL mail server logging.debug("Non-SSL mail server detected " \ "reconnecting to server %s:%s", server, port) try: mailconn = smtplib.SMTP(server, port) mailconn.ehlo() except: return errormsg(T('Failed to connect to mail server')) else: return errormsg(T('Failed to connect to mail server')) # TLS support if mailconn.ehlo_resp: m = re.search('STARTTLS', mailconn.ehlo_resp, re.IGNORECASE) if m: logging.debug("TLS mail server detected") try: mailconn.starttls() mailconn.ehlo() except: return errormsg(T('Failed to initiate TLS connection')) # Authentication if (cfg.email_account() != "") and (cfg.email_pwd() != ""): try: mailconn.login(cfg.email_account(), cfg.email_pwd()) except: return errormsg(T('Failed to authenticate to mail server')) try: mailconn.sendmail(cfg.email_from(), recipient, message) msg = None except smtplib.SMTPHeloError: msg = errormsg( 'The server didn\'t reply properly to the helo greeting.') except smtplib.SMTPRecipientsRefused: msg = errormsg( 'The server rejected ALL recipients (no mail was sent).') except smtplib.SMTPSenderRefused: msg = errormsg('The server didn\'t accept the from_addr.') except smtplib.SMTPDataError: msg = errormsg( 'The server replied with an unexpected error code (other than a refusal of a recipient).' ) except: msg = errormsg(T('Failed to send e-mail')) try: mailconn.close() except: errormsg(T('Failed to close mail connection')) if msg: return msg else: logging.info("Notification e-mail succesfully sent") return T('Email succeeded')