def compose_mail( account: Account, to: str = '*****@*****.**', subject: str = None, body: str = None, attachments: list = None ): """ :param account: :param to: :param subject: :param body: :param attachments: :return: """ mail = account.new_message() mail.to.add(to) if subject: mail.subject = subject if body: mail.body = body if attachments: for a in attachments: mail.attachments.add(a) return mail
class TestMailBoxBasicAuth: def setup_class(self): credentials = (USERNAME, PASSWORD) self.account = Account(credentials, auth_method=AUTH_METHOD.BASIC) self.mailbox = self.account.mailbox() self.inbox = self.mailbox.inbox_folder() def test_get_inbox_mails(self): messages = self.inbox.get_messages(5) assert len(messages) != 0 def test_new_email_draft(self): msg = self.account.new_message() msg.subject = 'Test Msg' msg.body = 'A message test' msg.save_draft() drafts = self.mailbox.drafts_folder() q = drafts.new_query('subject').equals('Test Msg') messages = drafts.get_messages(1, query=q) assert len(messages) == 1 def test_update_email(self): drafts = self.mailbox.drafts_folder() q = drafts.new_query('subject').equals('Test Msg') messages = drafts.get_messages(1, query=q) message = messages[0] if messages else None message2 = None if message: message.to.add('*****@*****.**') message.save_draft() messages2 = drafts.get_messages(1, query=q) message2 = messages2[0] if messages2 else None assert messages and message2 and message2.to and message2.to[0].address == '*****@*****.**' def test_delete_email(self): drafts = self.mailbox.drafts_folder() q = drafts.new_query('subject').equals('Test Msg') messages = drafts.get_messages(1, query=q) if messages: messages[0].delete() messages = drafts.get_messages(1, query=q) assert len(messages) == 0
class TestAccount: def setup_class(self): credentials = ("client id", "client secret") self.account = Account(credentials) def teardown_class(self): pass def test_get_message(self): message = self.account.new_message() assert isinstance(message, Message)
def sendingEmail(emailAddress, lastName): # import smtplib from O365 import Account credentials = ('dc5c5f85-b8ff-43fd-b964-6c145fd1cae0', 'yEjGet8pglE/hY:gS/OpFL2oeg4=v81=') emailAccount = Account(credentials) m = emailAccount.new_message() m.to.add(emailAddress) m.subject = 'Sending test e-mail' m.body = "Zgodnie z poleceniem miałem przesłać Twoje nazwisko: " + lastName + "." + "\n\nZ poważaniem,\nAndrzej Kiełbasa" m.send()
def main(): config = read_yaml() credentials = (config["outlook"]["client_id"], config["outlook"]["secret_id"]) account = Account(credentials) if account.authenticate(scopes=['https://graph.microsoft.com/Mail.Send']): print('Authenticated!') m = account.new_message() m.to.add(config["outlook"]["test_email"]) m.subject = 'Testing!' m.body = "Culero Incorparated." m.send()
def main(clientid, secret, recepient, subject, body): credentials = (clientid, secret) scopes = [ 'https://graph.microsoft.com/Mail.ReadWrite', 'https://graph.microsoft.com/Mail.Send' ] con = Connection(credentials, scopes=scopes) account = Account(credentials) m = account.new_message() m.to.add(recepient) m.subject = subject m.body = body m.send()
class Email(): def __init__(self, filePath): self.filePath = filePath json_file = open('credentials.txt', 'r') data = json.load(json_file) self.client_id = data['client_id'] self.client_secret = data['client_secret'] def connect(self): credentials = (self.client_id, self.client_secret) scopes = ['Mail.Send', 'offline_access', 'Mail.ReadWrite', 'User.Read'] self.account = Account(credentials) if not self.account.is_authenticated: # will check if there is a token and has not expired # ask for a login self.account.authenticate(scopes=scopes) def send_email(self, error): self.connect() m = self.account.new_message() m.to.add([ '*****@*****.**', '*****@*****.**', '*****@*****.**' ]) #m.to.add(['*****@*****.**']) m.cc.add(['*****@*****.**']) m.subject = 'Error in R_Forcasting' if error != 'File Not Found in the Path': m.body = """Hi, <br><br> There is an error while running the forecasting code.<br> For more details Please find the attached log file. <br><br> <b> """ + error + """ </b> <br><br>Regards, <br> Code""" m.attachments.add(self.filePath) else: m.body = """Hi, <br><br> There is an error while running the forecasting code.<br> <b> """ + error + """ </b> <br><br>Regards, <br> Code""" m.send()
class MyO365(): def __init__(self, credentials): """ credentials = ('dhasldl-b8ab-4730-93f1-cee730a4044b', 'salhdlghsaldh~ufOj2-4~69sCMZ05D_') """ self.account = Account(credentials) ok = False if not self.account.is_authenticated: ok = self.account.authenticate(scopes=['basic', 'message_all']) else: ok = True if ok: print('Authenticated!') else: print('Not Authenticated!') raise Exception("Email sender not authenticated!") def send_email(self, receiver: str, subject: str, body: str): m = self.account.new_message() m.to.add(receiver) m.subject = subject m.body = body m.send()
from O365 import Account # In this case, you will need to be used of the client_id and cliente_secree generated in your Azure Cloud account credentials = ('client_id', 'client_secret') account = Account(credentials) m = account.new_message() m.to.add('*****@*****.**') m.subject = 'Testing!' m.body = "This message was sent by python 0395 lib" m.send()