Example #1
0
 def sendEmail(self, subject, text):
     message = MIMEMultipart("alternative")
     message["From"] = "*****@*****.**"
     message["To"] = "*****@*****.**"
     message["CC"] = "*****@*****.**" if subject == "Excel" else ""
     message["Subject"] = subject
     message.Subject = subject
     part1 = MIMEText(text, "plain")
     message.attach(part1)
     context = ssl.create_default_context()
     with smtplib.SMTP_SSL(self.smtp_server, self.port,
                           context=context) as server:
         server.login(self.sender_email, self.password)
         server.sendmail(self.sender_email,
                         [self.receiver_email] + [message["CC"]],
                         message.as_string())
Example #2
0
def send_email(subject,
               body,
               to,
               cc="",
               bcc="",
               sender="*****@*****.**",
               attachment="",
               use_amg=False):
    """Wrapper function for sending eMails via MS Outlook Application or AXA Application Mail Gateway (AMG, on Server only)
    Owner: Tobias Ippisch
    Keyword Arguments:
    subject     << str >>
                   eMail subject
    body        << str >>
                   eMail body, either in html or plain format
    to          << list, str >>
                   eMail receipients, separated by '; '
    cc          << list, str (optional) >>
                   eMail cc receipients, separated by '; '
    bcc         << list, str (optional) >>
                   eMail bcc receipients, separated by '; '
    attachment  << str (optional) >>
                   File path to (single) attachment                   
    use_amg     << bool (optional) >>
                   Specifies whether mails should be sent by AXA Application Mail Gateway
                   (AMG, only available on server) or local mail client (e.g. MS Outlook)
                   
    Return Value:
    None
    """
    if use_amg == True or os.name == 'posix':
        #Package Import
        import smtplib
        from email.mime.text import MIMEText
        from email.mime.base import MIMEBase
        from email.mime.multipart import MIMEMultipart
        from email import encoders

        #Create & populate new mail object
        newMail = MIMEMultipart()

        newMail['Subject'] = subject
        newMail['From'] = sender
        newMail['To'] = "; ".join(to) if type(to) == list else to
        newMail['Cc'] = "; ".join(cc) if type(cc) == list else cc
        newMail['Bcc'] = "; ".join(bcc) if type(bcc) == list else bcc

        if "<html>" in body: newMail.attach(MIMEText(body, 'html'))
        else: newMail.attach(MIMEText(body, 'plain'))

        if attachment != "":
            part = MIMEBase('application', "octet-stream")
            part.set_payload(open(attachment, "rb").read())
            encoders.encode_base64(part)
            part.add_header(
                'Content-Disposition', 'attachment; filename="{0}"'.format(
                    os.path.basename(attachment)))
            newMail.attach(part)

        #Define receipient list
        receipient_list = []

        def receipient_creator(receipient_list, receipients):
            if type(receipients) == list:
                receipient_list += receipients
            elif type(receipients) == str:
                receipient_list += [
                    i.strip() for i in receipients.split(";") if i != ""
                ]
            return receipient_list

        receipient_list = receipient_creator(receipient_list, to)
        receipient_list = receipient_creator(receipient_list, cc)
        receipient_list = receipient_creator(receipient_list, bcc)

        #Connet to AMG and send eMail
        #s = smtplib.SMTP(host = 'amg.medc.services.axa-tech.intraxa', port = 25)
        s = smtplib.SMTP(host='10.140.60.6', port=25)
        s.ehlo('axa-winterthur.ch')
        s.sendmail(sender, receipient_list, newMail.as_string())
        s.quit()

    elif (os.name == 'nt'):
        #Package Import
        import win32com.client
        #from win32com.client import Dispatch, constants

        #Create & populate new mail object
        olMailItem = 0x0
        obj = win32com.client.Dispatch("Outlook.Application")
        newMail = obj.CreateItem(olMailItem)

        newMail.To = "; ".join(to) if type(to) == list else to
        newMail.CC = "; ".join(cc) if type(cc) == list else cc
        newMail.BCC = "; ".join(bcc) if type(bcc) == list else bcc
        newMail.Subject = subject

        if attachment != "": newMail.Attachments.Add(attachment)

        if "<html>" in body: newMail.HTMLBody = body
        else: newMail.Body = body

        #Send Mail Object
        newMail.send
    return