Archive/Old Shell Version/gen_invoices.py
def send_email_with_attachment(subject, body_text, to_emails,
                               cc_emails, bcc_emails, file_to_attach):
    """
    Send an email with an attachment
    """
    header = ['Content-Disposition',
              'attachment; filename="{}"'.format(file_to_attach)]

    # create the message
    msg = MIMEMultipart()
    msg["From"] = FROM
    msg["Subject"] = subject
    msg["Date"] = formatdate(localtime=True)
    if body_text:
        msg.attach(MIMEText(body_text))

    msg["To"] = ', '.join(to_emails)
    msg["cc"] = ', '.join(cc_emails)

    attachment = MIMEBase('application', "octet-stream")
    try:
        with open(file_to_attach, "rb") as fh:
            data = fh.read()
        attachment.set_payload(data)
        encoders.encode_base64(attachment)
        attachment.add_header(*header)
        msg.attach(attachment)
    except IOError:
        log.error("Error opening attachment file {}".format(file_to_attach),
                  exc_info=True)

    emails = to_emails + cc_emails

    server = smtplib.SMTP(HOSTNAME)
    server.sendmail(FROM, emails, msg.as_string())
    server.quit()

    Send an email with an attachment
    
Send an email with an attachment
Send an email with an attachment
def send_email_with_attachment(subject, body_text, to_emails,
                               cc_emails, bcc_emails, file_to_attach):
    header = ['Content-Disposition',
              'attachment; filename="{}"'.format(file_to_attach)]
    msg = MIMEMultipart()
    msg["From"] = FROM
    msg["Subject"] = subject
    msg["Date"] = formatdate(localtime=True)
    if body_text:
        msg.attach(MIMEText(body_text))
    msg["To"] = ', '.join(to_emails)
    msg["cc"] = ', '.join(cc_emails)
    attachment = MIMEBase('application', "octet-stream")
    try:
        with open(file_to_attach, "rb") as fh:
            data = fh.read()
        attachment.set_payload(data)
        encoders.encode_base64(attachment)
        attachment.add_header(*header)
        msg.attach(attachment)
    except IOError:
        log.error("Error opening attachment file {}".format(file_to_attach),
                  exc_info=True)
    emails = to_emails + cc_emails
    server = smtplib.SMTP(HOSTNAME)
    server.sendmail(FROM, emails, msg.as_string())
    server.quit()
def send_email_with_attachment ( subject , body_text , to_emails , cc_emails , bcc_emails , file_to_attach ) : header = [ 'Content-Disposition' , 'attachment; filename="{}"' . format ( file_to_attach ) ] msg = MIMEMultipart ( ) msg [ "From" ] = FROM msg [ "Subject" ] = subject msg [ "Date" ] = formatdate ( localtime = True ) if body_text : msg . attach ( MIMEText ( body_text ) ) msg [ "To" ] = ', ' . join ( to_emails ) msg [ "cc" ] = ', ' . join ( cc_emails ) attachment = MIMEBase ( 'application' , "octet-stream" ) try : with open ( file_to_attach , "rb" ) as fh : data = fh . read ( ) attachment . set_payload ( data ) encoders . encode_base64 ( attachment ) attachment . add_header ( * header ) msg . attach ( attachment ) except IOError : log . error ( "Error opening attachment file {}" . format ( file_to_attach ) , exc_info = True ) emails = to_emails + cc_emails server = smtplib . SMTP ( HOSTNAME ) server . sendmail ( FROM , emails , msg . as_string ( ) ) server . quit ( )
Send an email with an attachment
Send an email with an attachment
"""
    Send an email with an attachment
    """ # create the message
subject
body_text
to_emails
cc_emails
bcc_emails
file_to_attach