Example #1
0
def dialup():
    context = SSLContext(PROTOCOL_SSLv3)
    def _ssl():
        sslsession = SMTP_SSL(context=context)
        ext_log(sslsession.connect(getconf('smtp_server'), getconf('smtp_port')), 'SSL connection')
        return sslsession
    def  _starttls():
        tlssession = SMTP()
        ext_log(tlssession.connect(getconf('smtp_server'), getconf('smtp_port')), 'startTLS connection')
        tlssession.ehlo()
        if tlssession.has_extn('STARTTLS'):
            ext_log(tlssession.starttls(context=context), 'startTLS')
            tlssession.ehlo()
            return tlssession
        else:
            logger.warning('plaintext connection')
            return tlssession

    try:
        if getconf('smtp_use_ssl'):
            session = _ssl()
        else:
            session = _starttls()

        ext_log(session.login(getconf('smtp_user'), getconf('smtp_password')), 'login')
        return session

    except (SMTPException, SSLError) as ex:
            logger.error('SMTP error: %s' %(ex))
Example #2
0
def send_aspsms(to, messagetext, **args):

    originator = args.get('originator', getconf('aspsms_originator'))
    if not originator: originator = getconf('aspsms_originator')
    flashing = args.get('flashing', getconf('aspsms_flashing'))
    maxchars = args.get('maxchars', getconf('aspsms_maxchars'))

    logger.info('~' * 23)
    logger.info('sending new aspsms using %s:\n%d recipients ~ flashing: %s' %(originator, len(to), flashing))

    message = wrap(messagetext, maxchars)

    try:
        for recipient in to:
            for text in message:
                payload = make_xml(recipient, originator, text, flashing)
                response = response_xml(post_xml(payload))
                if not response['ErrorCode'] == '1':
                    raise Exception('aspsms error: %s' %(response['ErrorDescription']))
    except Exception as ex:
        logger.error('error: %s' %(ex))
        return ex
    else:
        logger.info('aspsms sent')
        return True

    logger.info('end aspsms')
Example #3
0
def make_xml(to, originator, text, flashing):
    flashing = str(flashing).lower()
    return xml_base.format(
        userkey=getconf('aspsms_userkey'),
        password=getconf('aspsms_password'),
        originator=originator,
        to=to,
        text=text,
        flashing=flashing
        )
Example #4
0
 def  _starttls():
     tlssession = SMTP()
     ext_log(tlssession.connect(getconf('smtp_server'), getconf('smtp_port')), 'startTLS connection')
     tlssession.ehlo()
     if tlssession.has_extn('STARTTLS'):
         ext_log(tlssession.starttls(context=context), 'startTLS')
         tlssession.ehlo()
         return tlssession
     else:
         logger.warning('plaintext connection')
         return tlssession
Example #5
0
def send_mail(to, messagetext, subject=None, **args):

    cc = args.get('cc', [])
    bcc = args.get('bcc', [])
    recipients = list(chain(to, cc, bcc))
    sender = args.get('sender', getconf('email_sender'))
    if not sender: sender = getconf('email_sender')
    footer = args.get('footer', getconf('email_footer'))
    if not footer: footer = getconf('email_footer')
    subjecttag = args.get('subjecttag', getconf('email_defaulttag'))
    subjectdate = args.get('subjectdate', getconf('email_subject_date'))
    files = args.get('files', [])

    logger.info('~' * 23)
    logger.info('sending new mail using %s:\n%d recipients ~ %d cc, %d bcc, %d files' %(sender, len(recipients), len(cc), len(bcc), len(files)))

    message = make_header(to, sender, cc, subject, subjecttag, subjectdate)
    message.attach(make_mime_text(messagetext, footer))
    [message.attach(make_mime_file(f)) for f in files]

    session = dialup()

    if session is not None:
        try:
            session.sendmail(sender, recipients, message.as_string().encode('UTF-8'))
        except SMTPException as ex:
            logger.error('smtp error: %s' %(ex))
            return ex
        else:
            logger.info('mail sent')
            return True
        finally:
            ext_log(session.quit(), 'quit')

    logger.info('end mail')
Example #6
0
def make_header(to, sender, cc, subject=None, subjecttag=None, subjectdate=None):
    # prevent python to encode utf-8 text in base64. using quoted printables instead
    charset.add_charset('utf-8', charset.QP, charset.QP, 'UTF-8')
    result = MIMEMultipart()
    # result.preamble = 'whatever'
    result.add_header('To', ', '.join(to))
    cc and result.add_header('CC', ', '.join(cc))
    result.add_header('From', sender)
    if not subject: subject = getconf('email_subject')
    if not subject: subject = '☃'
    subject = '[' + subjecttag + '] ' + subject if subjecttag else subject
    subject = subject + ' ' + formatdate(localtime=True) if subjectdate else subject
    result.add_header('Subject', subject)
    result.add_header('Date', formatdate())
    result.add_header('X-Mailer', 'Postbote Willy')

    return result
Example #7
0
 def _ssl():
     sslsession = SMTP_SSL(context=context)
     ext_log(sslsession.connect(getconf('smtp_server'), getconf('smtp_port')), 'SSL connection')
     return sslsession
Example #8
0
def post_xml(xmlpayload):
    headers = {'content-type': 'application/xml'}
    response = rpost(getconf('aspsms_gateway'), headers=headers, data=xmlpayload)
    return response.text