def send_mail(self, files: List[str]) -> None: LOGGER.info(f"Sending mail with: {files}") sender = self._settings.sender_mail receiver = self._settings.receiver_mail try: # implicitly start mail server for dummy delivery # performance should not be an issue in this context with DummyMailServer(user=self._settings.user, password=self._settings.password, port=DummyMailClient._PORT), \ Client(host=self._settings.hostname, port=DummyMailClient._PORT) as client: client.starttls() client.login(self._settings.user, self._settings.password) client.send_message(self._create_msg(sender, receiver, files)) except (TimeoutError, RuntimeError) as server_err: LOGGER.error( f"Error while start mail server: {self._settings.hostname}:{DummyMailClient._PORT}", exc_info=server_err) except (SMTPConnectError, OSError) as client_err: LOGGER.error( f"Error while connecting to mail server: {self._settings.hostname}:{DummyMailClient._PORT}", exc_info=client_err)
def sendemail(self, recipient): try: iplist = self.getmxrecords(recipient) if not iplist: raise Exception( 'Could Not send email to ' + self.email_to + '. There were no IPs returned from DNS lookup, the domain is likely malformed' ) for ip in iplist: try: client = Client(ip, '25', HELOname, 8) #unfortunately, sending the additional options did not work well in SMTPlib. #r = client.sendmail(self.email_from, [recipient], self.data, self.envelope.mail_options, self.envelope.rcpt_options) r = client.sendmail(self.email_from, [recipient], self.data) break except BaseException as e: print(e) self.generateNDR(self.email_from, e) except BaseException as e: print(e) self.generateNDR(self.email_from, e)
def send_mail(self, files: List[str]) -> None: """send mail to configured receiver to notify about recorded files list Args: files (List[str]): the list of already recorded files to inform about """ LOGGER.info(f"Sending mail with: {files}") sender = self.__settings.sender_mail receiver = self.__settings.receiver_mail try: with Client(host=self.__settings.hostname, port=GenericMailClient.__PORT) as client: client.starttls() client.login(self.__settings.user, self.__settings.password) client.send_message( GenericMailClient.__create_msg(sender, receiver, files)) except (SMTPConnectError, OSError) as client_err: LOGGER.error( "Error while connecting to mail server: " f"{self.__settings.hostname}:{GenericMailClient.__PORT}", exc_info=client_err)
from smtplib import SMTP as Client hostname = "localhost" port = 8025 client = Client(hostname, port) r = client.sendmail( '*****@*****.**', ['*****@*****.**'], """\ From: Anne Person <*****@*****.**> To: Bart Person <*****@*****.**> Subject: A test Message-ID: <ant> ... Hi Bart, this is Anne. """)
#!/usr/bin/env python # -*- coding: utf-8 -*- from smtplib import SMTP as Client client = Client('localhost', 2525) r = client.sendmail('*****@*****.**', ['*****@*****.**'], """\ From: Anne Person <*****@*****.**> To: Bart Person <*****@*****.**> Subject: A test Message-ID: <ant> Hi Bart, this is Anne. """)
import sys import email import codecs from email import policy from email.parser import BytesParser def mail_from_file(mail_path): with open(arg, "rb") as in_file: #import ipdb; ipdb.set_trace() ff = in_file.read() msg = BytesParser(policy=policy.default).parsebytes(ff) return msg if __name__ == "__main__": from smtplib import SMTP as Client client = Client('localhost', 8025) for arg in sys.argv: print(f"Sending {arg}") try: msg = mail_from_file(arg) r = client.sendmail('from@localhost', ['to@localhost'], msg.as_bytes()) except UnicodeEncodeError as e: print(f"EEEEEE - Fail to send {arg} - {e}")
from smtplib import SMTP as Client client = Client("localhost", 8025) r = client.sendmail('*****@*****.**', ['*****@*****.**'], """\ From: Anne Person <*****@*****.**> To: Bart Person <*****@*****.**> Subject: A test Message-ID: <ant> Hi Bart, this is Anne. """)
from smtplib import SMTP as Client client = Client('127.0.0.1', 2025) client.ehlo_or_helo_if_needed() client.login('bob', 'sponge') r = client.sendmail( '*****@*****.**', ['*****@*****.**'], """\ From: Anne Person <*****@*****.**> To: Bart Person <*****@*****.**> Subject: A test Message-ID: <ant> Hi Bart, this is Anne. """)