def __connect(self): try: self.server = smtplib.LMTP(self.conf['host'], self.conf['port']) except smtplib.SMTPException as err: raise getmailConfigurationError( 'Failed to connect to server' )
def notify(subject, whom, what): """Send email notification. Try to notify the addressee (``whom``) by e-mail, with Subject: defined by ``subject`` and message body by ``what``. """ msg = email.message_from_string(what) msg.add_header("From", "Certbot renewal agent <root>") msg.add_header("To", whom) msg.add_header("Subject", subject) msg = msg.as_string() try: lmtp = smtplib.LMTP() lmtp.connect() lmtp.sendmail("root", [whom], msg) except (smtplib.SMTPHeloError, smtplib.SMTPRecipientsRefused, smtplib.SMTPSenderRefused, smtplib.SMTPDataError, socket.error): # We should try using /usr/sbin/sendmail in this case try: proc = subprocess.Popen(["/usr/sbin/sendmail", "-t"], stdin=subprocess.PIPE) proc.communicate(msg) except OSError: return False return True
def __configure_smtp(self, conffilename, host, port, proto="SMTP", user=None, password=None): assert self.SMTPConnection is None if host == '0': self.SMTPConnection = -1 else: if proto == 'SMTP': self.SMTPConnection = (smtplib.SMTP, (host, port)) elif proto == 'SMTP_SSL': self.SMTPConnection = (smtplib.SMTP_SSL, (host, port)) elif proto == 'LMTP': self.SMTPConnection = (smtplib.LMTP(host, port)) else: raise striga.core.exception.StrigaConfigurationError( "Unknown SMTP protocol (use one SMTP, SMTP_SSL or LMTP") self.User = user self.Password = password if self.User is not None: assert self.Password is not None
def connect(self) -> None: """ Connect to server Returns: None """ if self.connection_type.lower() == "ssl": self.server = smtplib.SMTP_SSL( host=self.host, port=self.port, local_hostname=self.local_hostname, timeout=self.timeout, source_address=self.source_address, ) elif self.connection_type.lower() == "lmtp": self.server = smtplib.LMTP( host=self.host, port=self.port, local_hostname=self.local_hostname, source_address=self.source_address, ) else: self.server = smtplib.SMTP( host=self.host, port=self.port, local_hostname=self.local_hostname, timeout=self.timeout, source_address=self.source_address, ) self.server.login(self.username, self.password)
def lmtp(user_mailbox): lmtp = smtplib.LMTP() lmtp.set_debuglevel(1) lmtp.connect('/run/dovecot/lmtp') try: yield lmtp finally: lmtp.quit()
def _get_server(self): try: self._server.rset() except (smtplib.SMTPException, AttributeError): if settings.SALMON_SERVER["type"] == "smtp": self._server = smtplib.SMTP(settings.SALMON_SERVER["host"], settings.SALMON_SERVER["port"]) elif settings.SALMON_SERVER["type"] == "lmtp": self._server = smtplib.LMTP(settings.SALMON_SERVER["path"]) self._server.ehlo_or_helo_if_needed() # will "lhlo" for lmtp return self._server
def lmtp_deliver_mail(self, email_message): logging.info( "LMTP deliver: start -- LMTP host: %s:%s" % (self.lmtp_hostname, self.lmtp_port)) try: try: lmtp = smtplib.LMTP(self.lmtp_hostname, self.lmtp_port) except ConnectionRefusedError as e: logging.error("LMTP deliver (ConnectionRefusedError): %s" % (e)) return False except socket.gaierror as e: logging.error("LMTP deliver (LMTP-Server is not reachable): %s" % (e)) return False if self.lmtp_debug: lmtp.set_debuglevel(1) email_message['X-getmail-retrieved-from-mailbox-user'] = self.imap_username try: #https://docs.python.org/3/library/smtplib.html#smtplib.SMTP.send_message lmtp.send_message(email_message, to_addrs=self.lmtp_recipient) except Exception as e: logging.error("LMTP deliver (Exception - send_message #1): %s" % (e)) traceback.print_exc() try: email_from = email_message.get('From') lmtp.send_message(email_message, from_addr=email_from, to_addrs=self.lmtp_recipient) except Exception as e: logging.error("LMTP deliver (Exception - send_message #2): %s" % (e)) return False #return False finally: lmtp.quit() try: email_from_decoded = email.header.make_header(email.header.decode_header(email_message.get('From'))) email_subject_decoded = email.header.make_header(email.header.decode_header(email_message.get('Subject'))) #logging.info(u'LMTP deliver: new eMail from: [%s], subject: [%s] ----> LMTP recipient: %s' % (email_from_decoded, email_subject_decoded, self.lmtp_recipient)) logging.info(u'LMTP deliver: new eMail from: [%s], subject: [%s]' % (email_from_decoded, email_subject_decoded)) except Exception as e: logging.error("LMTP deliver (Exception - decode error): %s" % (e)) #logging.info(u'LMTP deliver: new eMail ----> LMTP recipient: %s' % (self.lmtp_recipient)) logging.info(u'LMTP deliver: new eMail') return True except Exception as e: logging.error("LMTP deliver (Exception): %s" % (e)) logging.error(traceback.format_exc()) return False
def __init_socket(self): host = self.config.get(self.section, 'host') if host.startswith('/'): # unix socket port = None if not os.path.exists(host): raise Exception("unix socket %s not found" % host) else: # tcp port try: port = int(self.config.get(self.section, 'port')) except ValueError: port = None # use default port s = smtplib.LMTP(host, port) return s
def configure_relay(self, hostname): if self.ssl: relay_host = smtplib.SMTP_SSL(hostname, self.port) elif self.lmtp: relay_host = smtplib.LMTP(hostname, self.port) else: relay_host = smtplib.SMTP(hostname, self.port) relay_host.set_debuglevel(self.debug) if self.starttls: relay_host.starttls() if self.username and self.password: relay_host.login(self.username, self.password) return relay_host
def main(path): loop = get_event_loop() m = inotify.Monitor(loop) server = smtplib.LMTP(os.path.join(path, "dovecot", "lmtp")) server.set_debuglevel(1) def callback(e): deliver(server, path, e.name) m.register(os.path.join(path, 'new'), inotify.IN_MOVED_TO, callback) for name in os.listdir(os.path.join(path, 'new')): deliver(server, path, name) run_loop(loop) server.quit()
def configure_relay(self, hostname): if self.ssl: relay_host = smtplib.SMTP_SSL(hostname, self.port) elif self.lmtp: relay_host = smtplib.LMTP(hostname, self.port) else: relay_host = smtplib.SMTP(hostname, self.port) # Uncomment only if you want to see that relaying is working # relay_host.set_debuglevel(self.debug) if self.starttls: relay_host.starttls() if self.username and self.password: relay_host.login(self.username, self.password) assert relay_host, 'Code error, file a bug.' return relay_host
import smtplib gmail_user = '******' gmail_password = input('PLEASE ENETET YOUR PASSWORD:'******'smtp.gmail.com', 465) server.ehlo() server.set_debuglevel(1) server.ehlo() server.login(gmail_user, gmail_password) print(' welcome email , the password is :%s' % gmail_password) #except: print('Something went wrong...The password is%s' % gmail_password)
import smtplib host = "junho85.pe.kr" port = 2525 fromaddr = "*****@*****.**" toaddr = "*****@*****.**" lmtp = smtplib.LMTP(host, port) lmtp.set_debuglevel(1) msg = """Subject: this is test mail from: [email protected] this is test mail """ lmtp.sendmail(fromaddr, toaddr, msg) lmtp.quit()
def lmtpclient(): return smtplib.LMTP(os.getenv('KOPANO_TEST_DAGENT_HOST'), os.getenv('KOPANO_TEST_DAGENT_PORT', 2003))
# Модуль smtplib определяет объект сеанса клиента SMTP, который # можно использовать для отправки почты на любой Интернет-компьютер # с помощью SMTP или ESMTP. # :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: # документация на английском: # https://docs.python.org/3/library/smtplib.html # :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: import smtplib # SMTP Objects ::::::::::::::::::::::::::::::::::::::::::::::::::::: smtplib.LMTP() # протокол LMTP, который очень похож на ESMTP, в значительной степени # основан на стандартном SMTP-клиенте. Обычно используется Unix-сокеты # для LMTP, поэтому наш метод connect() должен поддерживать это, а # также обычный сервер хоста: port. smtplib.SMTP('smtp.gmail.com', 587) # объект SMTP представляет соединение с почтовым SMTP-сервером и # располагает методами для отправки сообщений электроной почты. # Соеденение с сервером по протоколу TLS. m = smtplib.SMTP_SSL('smtp.gmail.com', 465) # Соеденение с сервером по протоколу SSL m.set_debuglevel(level) # установливает выходной уровень отладки. Значение 1 или True для
def __init__(self, host="localhost", port=24, user=None, password=None): self.hostname = host self.port = port self.user = user self.password = password self.lmtp = smtplib.LMTP(host=host, port=port)#, user=user, password=password)
'''This is a TEST body in text/plain. Best regards, John ''') msg['From'] = sys.argv[1] msg['To'] = sys.argv[2].split('@',1)[0].replace('#','@') msg['Subject'] = sys.argv[3] try: if sys.argv[4]: msg.add_header(sys.argv[4], sys.argv[5]) except: pass print( ''' Message length is %s Sending message... ''' % repr(len(msg.as_string()))) server = smtplib.LMTP('localhost', 10024) server.set_debuglevel(1) server.sendmail(sys.argv[1], sys.argv[2], msg.as_string()) server.quit()