Example #1
0
def send_mail(subj,
              body,
              bifogade_filer,
              recipients=[],
              sender='*****@*****.**'):
    from marrow.mailer import Mailer, Message
    """Send mail to specified recipients."""
    recipients = [*recipients]
    mailer = Mailer(dict(transport=dict(use='smtp', host='smtp.gu.se')))

    mailer.start()
    message = Message(
        subject=f'{subj}',
        plain=f'{body}',
        author=sender,
        to=recipients,
    )
    if bifogade_filer:
        for fil in bifogade_filer:
            if not os.path.isfile(fil):
                message.plain += "\n"
                message.plain += f"Error: attempted attachment {fil} does not appear to exist"
            else:
                message.attach(str(fil))
    nl = "\n"
    message.plain += f"{nl}{nl}If you have any question you can reach us at:"
    message.plain += f"{nl}[email protected]"
    message.plain += f"{nl}"
    message.plain += f"{nl}Best regards,"
    message.plain += f"{nl}Clinical Genomics Gothenburg"
    mailer.send(message)
Example #2
0
def sendMail(mail):

    try:
        message = Message(author="*****@*****.**", to=mail)

        # mail subject, obviously
        message.subject = "Subject"

        # plain text variant:
        message.plain = "Please view this mail with html enabled."

        # when pressing "reply", send mail to:
        message.reply = "*****@*****.**"

        # HTML variant:
        message.rich = """ 
        
        <h1>Header</h1>
        <p>some text</p>

        """

        message.attach(name='path-to-attachment.pdf')

        mailer.send(message)
        print('successfull sent mail to ' + mail)
        return True

    except:
        print('There was an error for mail ' + mail + ", skipping this.")
        return False
Example #3
0
 def send_email(self, user_id, subject, content, pdf_attachment=None):
     if user_id not in self.request.root.users:
         logging.error("When sending email, user ID %s does not exist" % user_id)
         return False
     user = self.request.root.users[user_id]
     if user.profile == None or user.profile.email == None:
         logging.error("When sending email, user ID %s does not have a profile or email address" % user_id)
         return False
     try:
         # Compose message
         send_name = self.request.registry._settings["sender_name"]
         send_email = self.request.registry._settings["sender_email"]
         message = Message(
             author  = '%s <%s>' % (send_name, send_email),
             subject = "%s - %s" % (PROP_KEYS.getProperty(self.request, PROP_KEYS.EVENT_NAME), subject),
             to      = user.profile.email,
             rich    = content["rich"],
             plain   = content["plain"]
         )
         # Attach any available PDFs
         if pdf_attachment != None:
             message.attach("tickets.pdf", data=pdf_attachment, maintype="application", subtype="pdf")
         # Dispatch Message
         get_mailer(self.request).send(message)
         logging.info("Sent email with subject '%s' to email %s" % (subject, user.profile.email))
         return True
     except socket.error:
         logging.exception("Socket error occurred when sending email to user %s" % user_id)
         return False
     except Exception:
         logging.exception("Email send exception occurred to user %s" % user_id)
         return False
Example #4
0
def send_email_task_smtp(payload, smtp_config, headers=None):
    mailer_config = {
        'transport': {
            'use': 'smtp',
            'host': smtp_config['host'],
            'username': smtp_config['username'],
            'password': smtp_config['password'],
            'tls': smtp_config['encryption'],
            'port': smtp_config['port']
        }
    }

    try:
        mailer = Mailer(mailer_config)
        mailer.start()
        message = Message(author=payload['from'], to=payload['to'])
        message.subject = payload['subject']
        message.plain = strip_tags(payload['html'])
        message.rich = payload['html']
        if payload['attachments'] is not None:
            for attachment in payload['attachments']:
                message.attach(name=attachment)
        mailer.send(message)
        logging.info('Message sent via SMTP')
    except urllib.error.HTTPError as e:
        if e.code == 554:
            empty_attachments_send(mailer, message)
    mailer.stop()
Example #5
0
    def send_email(self,
                   send_to,
                   template,
                   subject,
                   content,
                   files=[],
                   **kwargs):
        """
            Sends an email to the target email with two types
                1) HTML
                2) Plain

            We will try the template with .html for rich and .txt for plain,
            if one isn't found we will only send the
            correct one.

            Both will rendered with Jinja2
        """

        message = Message(author=self._config.EMAIL_SENDER, to=send_to)
        message.subject = subject

        if len(files) > 0:
            for f in files:
                #part = MIMEBase('application', "octet-stream")
                #part.set_payload( open(f,"rb").read() )
                # Encoders.encode_base64(part)
                filename = os.path.basename(f)
                #part.add_header('Content-Disposition', 'attachment; filename="%s"' % filename)
                message.attach(filename, data=f)

        if template:
            try:
                rendered_template = self._jinja_env.get_template(template +
                                                                 '.txt')
                message.plain = rendered_template.render(**kwargs)
                self._log.debug('Plain text email is %s', message.plain)
            except TemplateNotFound:
                self._log.debug('txt template not found')

            try:
                rendered_template = self._jinja_env.get_template(template +
                                                                 '.html')
                message.rich = rendered_template.render(**kwargs)
                self._log.debug('html email generated %s' % message.rich)
            except TemplateNotFound:
                self._log.debug('html template not found')
        else:
            if content:
                message.plain = content
            else:
                raise MailConfigurationException(
                    'No Message content or template defined')

        self._mailer.send(message)
Example #6
0
    def send_email(self, send_to, template, subject, content, files=[], **kwargs):
        """
            Sends an email to the target email with two types
                1) HTML
                2) Plain

            We will try the template with .html for rich and .txt for plain,
            if one isn't found we will only send the
            correct one.

            Both will rendered with Jinja2
        """

        message = Message(author=self._config.EMAIL_SENDER, to=send_to)
        message.subject = subject

        if len(files) > 0:
            for f in files:
                #part = MIMEBase('application', "octet-stream")
                #part.set_payload( open(f,"rb").read() )
                # Encoders.encode_base64(part)
                filename = os.path.basename(f)
                #part.add_header('Content-Disposition', 'attachment; filename="%s"' % filename)
                message.attach(filename, data=f)

        if template:
            try:
                rendered_template = self._jinja_env.get_template(
                    template + '.txt')
                message.plain = rendered_template.render(**kwargs)
                self._log.debug('Plain text email is %s', message.plain)
            except TemplateNotFound:
                self._log.debug('txt template not found')

            try:
                rendered_template = self._jinja_env.get_template(
                    template + '.html')
                message.rich = rendered_template.render(**kwargs)
                self._log.debug('html email generated %s' % message.rich)
            except TemplateNotFound:
                self._log.debug('html template not found')
        else:
            if content:
                message.plain = content
            else:
                raise MailConfigurationException(
                    'No Message content or template defined')

        self._mailer.send(message)
Example #7
0
def send_mail(subj, body, bifogad_fil, recipients=[], sender='*****@*****.**'):
    from marrow.mailer import Mailer, Message
    """Send mail to specified recipients."""
    recipients = [*recipients]
    mailer = Mailer(dict(
        transport=dict(use='smtp',
                       host='change.me.se')))

    mailer.start()
    message = Message(
        subject=f'{subj}',
        plain=f'{body}',
        author=sender,
        to=recipients,)
    message.attach(str(bifogad_fil))
    mailer.send(message)
    mailer.stop()
Example #8
0
 def _send_domain_report(self, pdf_file, host_url, admin):
     "Send a domain report"
     _ = self.translator.ugettext
     template = self.mako_lookup.get_template('/email/pdfreports.txt')
     text = template.render(user=admin, url=host_url)
     displayname = '%s %s' % (admin.firstname or '', admin.lastname or '')
     email = Msg(author=[(_('Baruwa Reports'), self.send_from)],
                     to=[(displayname, admin.email)],
                     subject=_('Baruwa usage report'))
     email.plain = text
     email.attach('baruwa-reports.pdf',
                 data=pdf_file,
                 maintype='application',
                 subtype='pdf')
     try:
         self.mailer.send(email)
     except (TransportFailedException, MessageFailedException), err:
         print >> sys.stderr, ("Error sending to: %s, Error: %s" %
                 (admin.email, err))
Example #9
0
    def send_mail_via_smtp(payload):
        """
        Send email via SMTP
        :param config:
        :param payload:
        :return:
        """
        smtp_encryption = current_app.config['SMTP_ENCRYPTION']
        if smtp_encryption == 'tls':
            smtp_encryption = 'required'
        elif smtp_encryption == 'ssl':
            smtp_encryption = 'ssl'
        elif smtp_encryption == 'tls_optional':
            smtp_encryption = 'optional'
        else:
            smtp_encryption = 'none'
        config = {
            'host': current_app.config['SMTP_HOST'],
            'username': current_app.config['SMTP_USERNAME'],
            'password': current_app.config['SMTP_PASSWORD'],
            'encryption': smtp_encryption,
            'port': current_app.config['SMTP_PORT'],
        }
        mailer_config = {
            'transport': {
                'use': 'smtp',
                'host': config['host'],
                'username': config['username'],
                'password': config['password'],
                'tls': config['encryption'],
                'port': config['port']
            }
        }

        mailer = Mailer(mailer_config)
        mailer.start()
        message = Message(author=payload['from'], to=payload['to'])
        message.subject = payload['subject']
        message.plain = strip_tags(payload['message'])
        message.rich = payload['message']
        message.attach(payload['attachment'])
        mailer.send(message)
        mailer.stop()
Example #10
0
def send_email_task_smtp(payload):
    smtp_config = get_smtp_config()
    mailer_config = {'transport': {'use': 'smtp', **smtp_config}}

    mailer = Mailer(mailer_config)
    mailer.start()
    message = Message(author=payload['from'], to=payload['to'])
    message.subject = payload['subject']
    message.plain = strip_tags(payload['html'])
    message.rich = payload['html']
    if payload['bcc'] is not None:
        message.bcc = payload['bcc']
    if payload['attachments'] is not None:
        for attachment in payload['attachments']:
            message.attach(name=attachment)
    try:
        mailer.send(message)
        logging.info('Message sent via SMTP')
    except urllib.error.HTTPError as e:
        if e.code == 554:
            empty_attachments_send(mailer, message)
    mailer.stop()
Example #11
0
def emailClient(mailer,
                userName,
                clientName,
                clientNum,
                clientFileName,
                plain=""):
    """Email the new file to any client."""

    if plain == "":
        plain = "Documents attached."

    mailer.start()
    message = Message(author=userName,
                      to=clientKey[clientName]['client'],
                      cc=emailKey[clientNum],
                      bcc=userName,
                      subject=clientFileName[1],
                      plain=plain)
    message.attach(clientFileName[0])
    mailer.send(message)
    mailer.stop()

    os.remove(clientFileName[0])
Example #12
0
    def command(self):
        "send"
        self.init()

        import baruwa

        pkgname = "baruwa"
        here = os.path.dirname(os.path.dirname(os.path.abspath(baruwa.__file__)))
        path = os.path.join(here, "baruwa", "templates")
        logo = os.path.join(here, "baruwa", "public", "imgs", "logo.png")
        localedir = os.path.join(here, "baruwa", "i18n")
        cache_dir = os.path.join(self.conf["cache_dir"], "templates")
        mako_lookup = TemplateLookup(
            directories=[path],
            error_handler=handle_mako_error,
            module_directory=cache_dir,
            input_encoding="utf-8",
            default_filters=["escape"],
            output_encoding="utf-8",
            encoding_errors="replace",
            imports=["from webhelpers.html import escape"],
        )

        mailer = Mailer(get_conf_options(self.conf))
        mailer.start()
        users = Session.query(User).filter(User.active == True).filter(User.send_report == True).all()
        # localedir = os.path.join(self.conf['here'], 'baruwa', 'i18n')
        for user in users:
            host_url = self.conf["baruwa.default.url"]
            sentry = 0
            language = "en"
            if user.is_domain_admin:
                orgs = [group.id for group in user.organizations]
                domains = (
                    Session.query(Domain.site_url, Domain.language)
                    .join(domain_owners)
                    .filter(Domain.status == True)
                    .filter(domain_owners.c.organization_id.in_(orgs))
                    .all()
                )
                if domains:
                    host_url, language = domains.pop(0)
            if user.is_peleb:
                domains = [(domain.site_url, domain.language) for domain in user.domains if domain.status == True]
                if domains:
                    host_url, language = domains.pop(0)
            if language == "en" and "domains" in locals() and domains:
                while domains:
                    _, language = domains.pop(0)
                    if language != "en":
                        break
            translator = set_lang(language, pkgname, localedir)
            _ = translator.ugettext
            reports = {
                "1": {"address": "from_address", "sort": "count", "title": _("Top Senders by Quantity")},
                "2": {"address": "from_address", "sort": "size", "title": _("Top Senders by Volume")},
                "3": {"address": "from_domain", "sort": "count", "title": _("Top Sender Domains by Quantity")},
                "4": {"address": "from_domain", "sort": "size", "title": _("Top Sender Domains by Volume")},
                "5": {"address": "to_address", "sort": "count", "title": _("Top Recipients by Quantity")},
                "6": {"address": "to_address", "sort": "size", "title": _("Top Recipients by Volume")},
                "7": {"address": "to_domain", "sort": "count", "title": _("Top Recipient Domains By Quantity")},
                "8": {"address": "to_domain", "sort": "size", "title": _("Top Recipient Domains By Volume")},
                "9": {"address": "", "sort": "", "title": _("Spam Score distribution")},
                "10": {"address": "clientip", "sort": "count", "title": _("Top mail hosts by quantity")},
                "11": {"address": "", "sort": "", "title": _("Total messages [ After SMTP ]")},
            }
            pieheadings = ("", _("Address"), _("Count"), _("Volume"), "")
            totalsheaders = dict(
                date=_("Date"),
                mail=_("Mail totals"),
                spam=_("Spam totals"),
                virus=_("Virus totals"),
                volume=_("Mail volume"),
                totals=_("Totals"),
            )
            pdfcreator = PDFReport(logo, _("Baruwa mail report"))
            for reportid in ["1", "2", "3", "4", "5", "6", "7", "8", "10"]:
                sortby = reports[reportid]["sort"]
                if user.account_type == 3 and reportid in ["7", "8"]:
                    data = None
                else:
                    query = ReportQuery(user, reportid)
                    if int(self.options.days) > 0:
                        a_day = datetime.timedelta(days=self.options.days)
                        startdate = now().date() - a_day
                        query = query.get().filter(Message.timestamp > str(startdate))
                        data = query[:10]
                    else:
                        data = query.get()[:10]
                if data:
                    sentry += 1
                    pdfcreator.add(data, reports[reportid]["title"], pieheadings, sortby)
            query = (
                Session.query(
                    Message.date,
                    func.count(Message.date).label("mail_total"),
                    func.sum(case([(Message.virusinfected > 0, 1)], else_=0)).label("virus_total"),
                    func.sum(case([(and_(Message.virusinfected == 0, Message.spam > 0), 1)], else_=0)).label(
                        "spam_total"
                    ),
                    func.sum(Message.size).label("total_size"),
                )
                .group_by(Message.date)
                .order_by(desc(Message.date))
            )
            uquery = UserFilter(Session, user, query)
            query = uquery.filter()
            data = query.all()
            if data:
                if not sentry:
                    sentry += 1
                pdfcreator.add(data, _("Message Totals"), totalsheaders, chart="bar")
            if sentry:
                template = mako_lookup.get_template("/email/pdfreports.txt")
                text = template.render(user=user, url=host_url)
                displayname = "%s %s" % (user.firstname or "", user.lastname or "")
                email = Msg(
                    author=[(_("Baruwa Reports"), self.conf["baruwa.reports.sender"])],
                    to=[(displayname, user.email)],
                    subject=_("Baruwa usage report"),
                )
                email.plain = text
                pdf_file = pdfcreator.build()
                email.attach("baruwa-reports.pdf", data=pdf_file, maintype="application", subtype="pdf")
                try:
                    mailer.send(email)
                except (TransportFailedException, MessageFailedException), err:
                    print >>sys.stderr, ("Error sending to: %s, Error: %s" % (user.email, err))
Example #13
0
    def command(self):
        "run command"
        self.init()
        import baruwa
        here = os.path.dirname(
                    os.path.dirname(os.path.abspath(baruwa.__file__))
                )
        path = os.path.join(here, 'baruwa', 'templates')
        logo = os.path.join(here, 'baruwa', 'public', 'imgs', 'logo.png')
        localedir = os.path.join(here, 'baruwa', 'i18n')
        cache_dir = os.path.join(self.conf['cache_dir'], 'templates')
        pkgname = 'baruwa'

        if not os.path.exists(logo):
            print sys.STDERR ("The logo image: %s does not exist" % logo)
            sys.exit(2)

        with open(logo) as handle:
            logo = base64.b64encode(handle.read())

        mako_lookup = TemplateLookup(
                        directories=[path],
                        error_handler=handle_mako_error,
                        module_directory=cache_dir,
                        input_encoding='utf-8',
                        default_filters=['escape'],
                        output_encoding='utf-8',
                        encoding_errors='replace',
                        imports=['from webhelpers.html import escape']
                        )

        mailer = Mailer(get_conf_options(self.conf))
        mailer.start()

        users = Session.query(User)\
                .filter(User.active == True)\
                .filter(User.send_report == True)

        previous_records = Session\
                        .query(Release.messageid)\
                        .order_by(desc('timestamp'))

        for user in users:
            messages = Session.query(Message.id, Message.timestamp,
                            Message.from_address, Message.to_address,
                            Message.subject,
                            case([(and_(Message.spam > 0,
                                Message.virusinfected == 0,
                                Message.nameinfected == 0,
                                Message.otherinfected == 0,
                                ), True)],
                                else_=False).label('spam'),
                            Message.to_domain)\
                            .filter(Message.isquarantined > 0)\
                            .filter(or_(Message.spam > 0,
                                    Message.nameinfected > 0,
                                    Message.otherinfected > 0))\
                            .order_by(desc('timestamp'))

            query = UserFilter(Session, user, messages)
            messages = query.filter()
            if int(self.options.days) > 0:
                a_day = datetime.timedelta(days=self.options.days)
                startdate = now().date() - a_day
                messages = messages.filter(Message.timestamp > str(startdate))
            messages = messages.filter(~Message.id.in_(previous_records)) 
            messages = messages[:25]

            if messages:
                lang = 'en'
                host_urls = dict([(domain.name, domain.site_url)
                            for domain in user.domains
                            if domain.status == True])
                langs = [domain.language for domain in user.domains
                        if domain.language != 'en']
                if langs:
                    lang = langs.pop(0)
                translator = set_lang(lang, pkgname, localedir)
                _ = translator.ugettext
                def make_release_records(spam):
                    "map function"
                    uuid = gen_uuid(user)
                    spam.uuid = uuid
                    return Release(uuid=uuid, messageid=spam.id)
                if user.is_peleb:
                    torelease = [make_release_records(spam)
                                for spam in messages]
                template = mako_lookup.get_template('/email/quarantine.html')
                html = template.render(messages=messages,
                                host_urls=host_urls, url=url_for,
                                default_url=self.conf['baruwa.default.url'])
                template = mako_lookup.get_template('/email/quarantine.txt')
                text = template.render(messages=messages)
                displayname = "%s %s" % (user.firstname, user.lastname)
                email = Msg(author=[(_('Baruwa Reports'),
                                self.conf['baruwa.reports.sender'])],
                                to=[(displayname, user.email)],
                                subject=_('Baruwa quarantine report'))
                email.plain = text
                email.rich = html
                email.attach('logo.png',
                            data=logo,
                            maintype='image',
                            subtype='png',
                            inline=True)
                mailer.send(email)
                if 'torelease' in locals():
                    Session.add_all(torelease)
                    Session.commit()
        mailer.stop()
Example #14
0
def goAsync():
    try:
        df =     pd.read_csv('./accounts.csv', index_col=0)
        accountNames  = [x.split('@')[-1] for x in df['Account'].tolist()]     
        tapi= twitter.Api(consumer_key='[redacted]', consumer_secret='[redacted]',
                          application_only_auth=True, tweet_mode='extended')
        tmpFile = tempfile.NamedTemporaryFile(mode='wb', suffix='.xlsx')
        tmpFile2 = tempfile.NamedTemporaryFile(mode='wb', suffix='.xlsx')
        workbook = xlsxwriter.Workbook(tmpFile.name, {'nan_inf_to_errors': True, 'default_date_format': 'dd-mm-yyyy', 'strings_to_urls': False})
        workbook2 = xlsxwriter.Workbook(tmpFile2.name, {'nan_inf_to_errors': True, 'default_date_format': 'dd-mm-yyyy', 'strings_to_urls': False})
        a, b = workbook.add_format({'bold': True, 'font_color': 'black', 'bg_color':  '#00b0f0', 'font_name': 'Calibri', 'font_size': 11}), workbook.add_format({'bold': False, 'font_color': 'black', 'font_name': 'Calibri', 'font_size': 11})
        a2, b2, d2 = workbook2.add_format({'bold': True, 'font_color': 'black', 'bg_color':  '#00b0f0', 'font_name': 'Calibri', 'font_size': 11}), workbook2.add_format({'bold': False, 'font_color': 'black', 'font_name': 'Calibri', 'font_size': 11}), workbook2.add_format({'bold': False, 'font_color': 'black', 'font_name': 'Calibri', 'font_size': 11, 'num_format': 'dd-mm-yyyy'})
        columns = ['Tweet Id', 'Text', 'Name', 'Screen Name', 'Created At', 'Media URLs']
        coreData = workbook2.add_worksheet('Core Data')
        mediaUrls = workbook2.add_worksheet('Media URLs Assign')
        mediaText = workbook2.add_worksheet('Text')
        coreDataCols = ['Media_Title', 'Media_Format', 'Media_Author', 'Media_Publication_ID', 'Media_Owner', 'Media_Country', 'Media_State', 'Media_City', 'Media_Date', 'Media_File']
        for c, col in enumerate(coreDataCols):
            width = min(8.43, len(col)*1.2)
            coreData.set_column(c, c, width)
            coreData.write(0, c, col, a2)
        mediaUtlsCols = ['Media_Title', 'Media_URLs']
        for c, col in enumerate(mediaUtlsCols):
            width = min(8.43, len(col)*1.2)
            mediaUrls.set_column(c, c, width)
            mediaUrls.write(0, c, col, a2)
        mediaTextCols = ['Media_Title', 'Media_Text']
        for c, col in enumerate(mediaTextCols):
            width = min(8.43, len(col)*1.2)
            mediaText.set_column(c, c, width)
            mediaText.write(0, c, col, a2)
        curIdx2 = 1
        curIdx3 = 1
        curIdx4 = 1
        for accountName in accountNames:
            worksheet = workbook.add_worksheet(accountName)
            for c, col in enumerate(columns):
                width = min(8.43, len(col)*1.2)
                worksheet.set_column(c, c, width)
                worksheet.write(0, c, col, a)
            max_id = None
            curIdx = 1
            while True:
                try:
                    tweets = tapi.GetUserTimeline(screen_name=accountName, count=200, include_rts=False,trim_user=False,exclude_replies=True, max_id=max_id if max_id is None else max_id-1)
                except:
                    break
                if len(tweets) == 0:
                    break
                for tweet in tweets:
                    max_id = tweet.id
                    ddatetime = datetime.datetime.strptime(tweet.created_at, "%a %b %d %H:%M:%S +0000 %Y") 
                    ddate = ddatetime.strftime('%Y-%m-%d')
                    mediaTitle = '%s_https://twitter.com/%s/status/%s' %(ddate, accountName, tweet.id)
                    coreData.write(curIdx2, 0, mediaTitle, b2)
                    coreData.write(curIdx2, 1, "Twitter", b2)
                    coreData.write(curIdx2, 2, tweet.user.name, b2)
                    coreData.write_datetime(curIdx2, 8, ddatetime, d2)
                    coreData.write(curIdx2, 9, 'https://www.digbybamford/Tweets/'+ mediaTitle, b2)
                    curIdx2 += 1
                    worksheet.write(curIdx, 0, str(tweet.id), b)
                    mediaText.write(curIdx4, 0, mediaTitle,b)
                    if tweet.tweet_mode == 'extended':
                        worksheet.write(curIdx, 1, tweet.full_text, b)
                        mediaText.write(curIdx4, 1, tweet.full_text,b)
                    else:
                        worksheet.write(curIdx, 1, tweet.text, b)
                        mediaText.write(curIdx4, 1, tweet.text,b)
                    curIdx4 += 1
                    worksheet.write(curIdx, 2, tweet.user.name, b)
                    worksheet.write(curIdx, 3, accountName, b)
                    worksheet.write(curIdx, 4, tweet.created_at, b)
                    if tweet.media is not None:
                        for i, media in enumerate(tweet.media):
                            worksheet.write(curIdx, 5+i, media.media_url_https, b)
                            mediaUrls.write(curIdx3, 0, mediaTitle, b2)
                            mediaUrls.write(curIdx3, 1, media.media_url_https, b2)
                            curIdx3 += 1
                    curIdx += 1
        workbook.close()
        workbook2.close()
        zipObj = ZipFile('./tweets.zip', 'w')
        zipObj.write(tmpFile.name, 'Tweets.xlsx')
        zipObj.write(tmpFile2.name, 'CoreData.xlsx')
        zipObj.close()
        mailer = Mailer(dict(
            transport = dict(
                    use = 'smtp',
                    host = 'localhost')))
        mailer.start()
#         message = Message(author="[redacted]", to="[redacted]")
#         message.subject = "Twitter Result"
#         message.plain = " "
#         message.attach('./tweets.zip')
#         mailer.send(message)
        message = Message(author="[redacted]", to="[redacted]")
        message.subject = "Twitter Result"
        message.plain = " "
        message.attach('./tweets.zip')
        mailer.send(message)
        message = Message(author="[redacted]", to="[redacted]")
        message.subject = "Twitter Result"
        message.plain = " "
        message.attach('./tweets.zip')
        mailer.send(message)
        mailer.stop()
    except:
        mailer = Mailer(dict(
            transport = dict(
                    use = 'smtp',
                    host = 'localhost')))
        mailer.start()
        message = Message(author="[redacted]", to="[redacted]")
        message.subject = "Twitter Result"
        message.plain = traceback.format_exc()
        mailer.send(message)
        message = Message(author="[redacted]", to="[redacted]")
        message.subject = "Twitter Result"
        message.plain = "An error occured, the details have been sent to the developer."
        mailer.send(message)
        message = Message(author="[redacted]", to="[redacted]")
        message.subject = "Twitter Result"
        message.plain = "An error occured, the details have been sent to the developer."
        mailer.send(message)
        mailer.stop()
Example #15
0
    def command(self):
        "run command"
        self.init()
        import baruwa
        here = os.path.dirname(
            os.path.dirname(os.path.abspath(baruwa.__file__)))
        path = os.path.join(here, 'baruwa', 'templates')
        logo = os.path.join(here, 'baruwa', 'public', 'imgs', 'logo.png')
        localedir = os.path.join(here, 'baruwa', 'i18n')
        cache_dir = os.path.join(self.conf['cache_dir'], 'templates')
        pkgname = 'baruwa'

        if not os.path.exists(logo):
            print sys.STDERR("The logo image: %s does not exist" % logo)
            sys.exit(2)

        with open(logo) as handle:
            logo = base64.b64encode(handle.read())

        mako_lookup = TemplateLookup(
            directories=[path],
            error_handler=handle_mako_error,
            module_directory=cache_dir,
            input_encoding='utf-8',
            default_filters=['escape'],
            output_encoding='utf-8',
            encoding_errors='replace',
            imports=['from webhelpers.html import escape'])

        mailer = Mailer(get_conf_options(self.conf))
        mailer.start()

        users = Session.query(User)\
                .filter(User.active == True)\
                .filter(User.send_report == True)

        previous_records = Session\
                        .query(Release.messageid)\
                        .order_by(desc('timestamp'))

        for user in users:
            messages = Session.query(Message.id, Message.timestamp,
                            Message.from_address, Message.to_address,
                            Message.subject,
                            case([(and_(Message.spam > 0,
                                Message.virusinfected == 0,
                                Message.nameinfected == 0,
                                Message.otherinfected == 0,
                                ), True)],
                                else_=False).label('spam'),
                            Message.to_domain)\
                            .filter(Message.isquarantined > 0)\
                            .filter(or_(Message.spam > 0,
                                    Message.nameinfected > 0,
                                    Message.otherinfected > 0))\
                            .order_by(desc('timestamp'))

            query = UserFilter(Session, user, messages)
            messages = query.filter()
            if int(self.options.days) > 0:
                a_day = datetime.timedelta(days=self.options.days)
                startdate = now().date() - a_day
                messages = messages.filter(Message.timestamp > str(startdate))
            messages = messages.filter(~Message.id.in_(previous_records))
            messages = messages[:25]

            if messages:
                lang = 'en'
                host_urls = dict([(domain.name, domain.site_url)
                                  for domain in user.domains
                                  if domain.status == True])
                langs = [
                    domain.language for domain in user.domains
                    if domain.language != 'en'
                ]
                if langs:
                    lang = langs.pop(0)
                translator = set_lang(lang, pkgname, localedir)
                _ = translator.ugettext

                def make_release_records(spam):
                    "map function"
                    uuid = gen_uuid(user)
                    spam.uuid = uuid
                    return Release(uuid=uuid, messageid=spam.id)

                if user.is_peleb:
                    torelease = [
                        make_release_records(spam) for spam in messages
                    ]
                template = mako_lookup.get_template('/email/quarantine.html')
                html = template.render(
                    messages=messages,
                    host_urls=host_urls,
                    url=url_for,
                    default_url=self.conf['baruwa.default.url'])
                template = mako_lookup.get_template('/email/quarantine.txt')
                text = template.render(messages=messages)
                displayname = "%s %s" % (user.firstname, user.lastname)
                email = Msg(author=[(_('Baruwa Reports'),
                                     self.conf['baruwa.reports.sender'])],
                            to=[(displayname, user.email)],
                            subject=_('Baruwa quarantine report'))
                email.plain = text
                email.rich = html
                email.attach('logo.png',
                             data=logo,
                             maintype='image',
                             subtype='png',
                             inline=True)
                mailer.send(email)
                if 'torelease' in locals():
                    Session.add_all(torelease)
                    Session.commit()
        mailer.stop()
Example #16
0
    def command(self):
        "send"
        self.init()

        import baruwa
        pkgname = 'baruwa'
        here = os.path.dirname(
            os.path.dirname(os.path.abspath(baruwa.__file__)))
        path = os.path.join(here, 'baruwa', 'templates')
        logo = os.path.join(here, 'baruwa', 'public', 'imgs', 'logo.png')
        localedir = os.path.join(here, 'baruwa', 'i18n')
        cache_dir = os.path.join(self.conf['cache_dir'], 'templates')
        mako_lookup = TemplateLookup(
            directories=[path],
            error_handler=handle_mako_error,
            module_directory=cache_dir,
            input_encoding='utf-8',
            default_filters=['escape'],
            output_encoding='utf-8',
            encoding_errors='replace',
            imports=['from webhelpers.html import escape'])

        mailer = Mailer(get_conf_options(self.conf))
        mailer.start()
        users = Session\
                .query(User)\
                .filter(User.active == True)\
                .filter(User.send_report == True).all()
        #localedir = os.path.join(self.conf['here'], 'baruwa', 'i18n')
        for user in users:
            host_url = self.conf['baruwa.default.url']
            sentry = 0
            language = 'en'
            if user.is_domain_admin:
                orgs = [group.id for group in user.organizations]
                domains = Session\
                        .query(Domain.site_url, Domain.language)\
                        .join(domain_owners)\
                        .filter(Domain.status == True)\
                        .filter(domain_owners.c.organization_id.in_(orgs))\
                        .all()
                if domains:
                    host_url, language = domains.pop(0)
            if user.is_peleb:
                domains = [(domain.site_url, domain.language)
                           for domain in user.domains if domain.status == True]
                if domains:
                    host_url, language = domains.pop(0)
            if language == 'en' and 'domains' in locals() and domains:
                while domains:
                    _, language = domains.pop(0)
                    if language != 'en':
                        break
            translator = set_lang(language, pkgname, localedir)
            _ = translator.ugettext
            reports = {
                '1': {
                    'address': 'from_address',
                    'sort': 'count',
                    'title': _('Top Senders by Quantity')
                },
                '2': {
                    'address': 'from_address',
                    'sort': 'size',
                    'title': _('Top Senders by Volume')
                },
                '3': {
                    'address': 'from_domain',
                    'sort': 'count',
                    'title': _('Top Sender Domains by Quantity')
                },
                '4': {
                    'address': 'from_domain',
                    'sort': 'size',
                    'title': _('Top Sender Domains by Volume')
                },
                '5': {
                    'address': 'to_address',
                    'sort': 'count',
                    'title': _('Top Recipients by Quantity')
                },
                '6': {
                    'address': 'to_address',
                    'sort': 'size',
                    'title': _('Top Recipients by Volume')
                },
                '7': {
                    'address': 'to_domain',
                    'sort': 'count',
                    'title': _('Top Recipient Domains By Quantity')
                },
                '8': {
                    'address': 'to_domain',
                    'sort': 'size',
                    'title': _('Top Recipient Domains By Volume')
                },
                '9': {
                    'address': '',
                    'sort': '',
                    'title': _('Spam Score distribution')
                },
                '10': {
                    'address': 'clientip',
                    'sort': 'count',
                    'title': _('Top mail hosts by quantity')
                },
                '11': {
                    'address': '',
                    'sort': '',
                    'title': _('Total messages [ After SMTP ]')
                }
            }
            pieheadings = ('', _('Address'), _('Count'), _('Volume'), '')
            totalsheaders = dict(date=_('Date'),
                                 mail=_('Mail totals'),
                                 spam=_('Spam totals'),
                                 virus=_('Virus totals'),
                                 volume=_('Mail volume'),
                                 totals=_('Totals'))
            pdfcreator = PDFReport(logo, _('Baruwa mail report'))
            for reportid in ['1', '2', '3', '4', '5', '6', '7', '8', '10']:
                sortby = reports[reportid]['sort']
                if user.account_type == 3 and reportid in ['7', '8']:
                    data = None
                else:
                    query = ReportQuery(user, reportid)
                    if int(self.options.days) > 0:
                        a_day = datetime.timedelta(days=self.options.days)
                        startdate = datetime.date.today() - a_day
                        query = query.get().filter(
                            Message.timestamp > str(startdate))
                        data = query[:10]
                    else:
                        data = query.get()[:10]
                if data:
                    sentry += 1
                    pdfcreator.add(data, reports[reportid]['title'],
                                   pieheadings, sortby)
            query = Session.query(Message.date,
                                func.count(Message.date).label('mail_total'),
                                func.sum(case([(Message.virusinfected > 0, 1)],
                                else_=0)).label('virus_total'),
                                func.sum(case([(and_(Message.virusinfected ==
                                0, Message.spam > 0), 1)], else_=0))\
                                .label('spam_total'), func.sum(Message.size)\
                                .label('total_size')).group_by(Message.date)\
                                .order_by(desc(Message.date))
            uquery = UserFilter(Session, user, query)
            query = uquery.filter()
            data = query.all()
            if data:
                if not sentry:
                    sentry += 1
                pdfcreator.add(data,
                               _('Message Totals'),
                               totalsheaders,
                               chart='bar')
            if sentry:
                template = mako_lookup.get_template('/email/pdfreports.txt')
                text = template.render(user=user, url=host_url)
                displayname = '%s %s' % (user.firstname or '', user.lastname
                                         or '')
                email = Msg(author=[(_('Baruwa Reports'),
                                     self.conf['baruwa.reports.sender'])],
                            to=[(displayname, user.email)],
                            subject=_('Baruwa usage report'))
                email.plain = text
                pdf_file = base64.b64encode(pdfcreator.build())
                email.attach('baruwa-reports.pdf',
                             data=pdf_file,
                             maintype='application/pdf',
                             subtype='application/x-pdf')
                try:
                    mailer.send(email)
                except (TransportFailedException, MessageFailedException), err:
                    print >> sys.stderr, ("Error sending to: %s, Error: %s" %
                                          (user.email, err))
Example #17
0
    def _process_user_report(self, user):
        "Process user report"
        sentry = 0
        language = self.language
        host_url = self.host_url

        if user.is_peleb:
            domains = [(domain.site_url, domain.language)
                        for domain in user.domains
                        if domain.status == True]
            if domains:
                host_url, language = domains.pop(0)
        if language == 'en' and 'domains' in locals() and domains:
            while domains:
                _, language = domains.pop(0)
                if language != 'en':
                    break
        translator = set_lang(language, PKGNAME, self.localedir)
        _ = translator.ugettext
        reports = {
                    '1': {'address': 'from_address', 'sort': 'count',
                        'title': _('Top Senders by Quantity')},
                    '2': {'address': 'from_address', 'sort': 'size',
                        'title': _('Top Senders by Volume')},
                    '3': {'address': 'from_domain', 'sort': 'count',
                        'title': _('Top Sender Domains by Quantity')},
                    '4': {'address': 'from_domain', 'sort': 'size',
                        'title': _('Top Sender Domains by Volume')},
                    '5': {'address': 'to_address', 'sort': 'count',
                        'title': _('Top Recipients by Quantity')},
                    '6': {'address': 'to_address', 'sort': 'size',
                        'title': _('Top Recipients by Volume')},
                    '7': {'address': 'to_domain', 'sort': 'count',
                        'title': _('Top Recipient Domains By Quantity')},
                    '8': {'address': 'to_domain', 'sort': 'size',
                        'title': _('Top Recipient Domains By Volume')},
                    '9': {'address': '', 'sort': '',
                        'title': _('Spam Score distribution')},
                    '10': {'address': 'clientip', 'sort': 'count',
                        'title': _('Top mail hosts by quantity')},
                    '11': {'address': '', 'sort': '',
                        'title': _('Total messages [ After SMTP ]')}
                    }
        pieheadings = ('', _('Address'), _('Count'), _('Volume'), '')
        totalsheaders = dict(date=_('Date'), mail=_('Mail totals'),
                        spam=_('Spam totals'), virus=_('Virus totals'),
                        volume=_('Mail volume'), totals=_('Totals'))
        pdfcreator = PDFReport(self.logo, _('Baruwa mail report'))
        for reportid in ['1', '2', '3', '4', '5', '6', '7', '8', '10']:
            sortby = reports[reportid]['sort']
            if user.account_type == 3 and reportid in ['7', '8']:
                data = None
            else:
                data = pie_report_query(user, reportid, self.num_of_days)
            if data:
                sentry += 1
                pdfcreator.add(data, reports[reportid]['title'],
                            pieheadings, sortby)
        data = message_totals_report(user, self.num_of_days)
        if data:
            if not sentry:
                sentry += 1
            pdfcreator.add(data, _('Message Totals'), totalsheaders, chart='bar')
        if sentry:
            pdf_file = base64.b64encode(pdfcreator.build())
            template = self.mako_lookup.get_template('/email/pdfreports.txt')
            text = template.render(user=user, url=host_url)
            displayname = '%s %s' % (user.firstname or '', user.lastname or '')
            email = Msg(author=[(_('Baruwa Reports'), self.send_from)],
                            to=[(displayname, user.email)],
                            subject=_('Baruwa usage report'))
            email.plain = text
            email.attach('baruwa-reports.pdf',
                        data=pdf_file,
                        maintype='application',
                        subtype='pdf')
            try:
                self.mailer.send(email)
            except (TransportFailedException, MessageFailedException), err:
                print >> sys.stderr, ("Error sending to: %s, Error: %s" %
                        (user.email, err))
Example #18
0
    def command(self):
        "send"
        self.init()

        import baruwa
        pkgname = 'baruwa'
        here = os.path.dirname(
                    os.path.dirname(os.path.abspath(baruwa.__file__))
                )
        path = os.path.join(here, 'baruwa', 'templates')
        logo = os.path.join(here, 'baruwa', 'public', 'imgs', 'logo.png')
        localedir = os.path.join(here, 'baruwa', 'i18n')
        cache_dir = os.path.join(self.conf['cache_dir'], 'templates')
        mako_lookup = TemplateLookup(
                        directories=[path],
                        error_handler=handle_mako_error,
                        module_directory=cache_dir,
                        input_encoding='utf-8',
                        default_filters=['escape'],
                        output_encoding='utf-8',
                        encoding_errors='replace',
                        imports=['from webhelpers.html import escape']
                        )

        mailer = Mailer(get_conf_options(self.conf))
        mailer.start()
        users = Session\
                .query(User)\
                .filter(User.active == True)\
                .filter(User.send_report == True).all()
        #localedir = os.path.join(self.conf['here'], 'baruwa', 'i18n')
        for user in users:
            host_url = self.conf['baruwa.default.url']
            sentry = 0
            language = 'en'
            if user.is_domain_admin:
                orgs = [group.id for group in user.organizations]
                domains = Session\
                        .query(Domain.site_url, Domain.language)\
                        .join(domain_owners)\
                        .filter(Domain.status == True)\
                        .filter(domain_owners.c.organization_id.in_(orgs))\
                        .all()
                if domains:
                    host_url, language = domains.pop(0)
            if user.is_peleb:
                domains = [(domain.site_url, domain.language)
                            for domain in user.domains
                            if domain.status == True]
                if domains:
                    host_url, language = domains.pop(0)
            if language == 'en' and 'domains' in locals() and domains:
                while domains:
                    _, language = domains.pop(0)
                    if language != 'en':
                        break
            translator = set_lang(language, pkgname, localedir)
            _ = translator.ugettext
            reports = {
                        '1': {'address': 'from_address', 'sort': 'count',
                            'title': _('Top Senders by Quantity')},
                        '2': {'address': 'from_address', 'sort': 'size',
                            'title': _('Top Senders by Volume')},
                        '3': {'address': 'from_domain', 'sort': 'count',
                            'title': _('Top Sender Domains by Quantity')},
                        '4': {'address': 'from_domain', 'sort': 'size',
                            'title': _('Top Sender Domains by Volume')},
                        '5': {'address': 'to_address', 'sort': 'count',
                            'title': _('Top Recipients by Quantity')},
                        '6': {'address': 'to_address', 'sort': 'size',
                            'title': _('Top Recipients by Volume')},
                        '7': {'address': 'to_domain', 'sort': 'count',
                            'title': _('Top Recipient Domains By Quantity')},
                        '8': {'address': 'to_domain', 'sort': 'size',
                            'title': _('Top Recipient Domains By Volume')},
                        '9': {'address': '', 'sort': '',
                            'title': _('Spam Score distribution')},
                        '10': {'address': 'clientip', 'sort': 'count',
                            'title': _('Top mail hosts by quantity')},
                        '11': {'address': '', 'sort': '',
                            'title': _('Total messages [ After SMTP ]')}
                        }
            pieheadings = ('', _('Address'), _('Count'), _('Volume'), '')
            totalsheaders = dict(date=_('Date'), mail=_('Mail totals'),
                            spam=_('Spam totals'), virus=_('Virus totals'),
                            volume=_('Mail volume'), totals=_('Totals'))
            pdfcreator = PDFReport(logo, _('Baruwa mail report'))
            for reportid in ['1', '2', '3', '4', '5', '6', '7', '8', '10']:
                sortby = reports[reportid]['sort']
                if user.account_type == 3 and reportid in ['7', '8']:
                    data = None
                else:
                    query = ReportQuery(user, reportid)
                    if int(self.options.days) > 0:
                        a_day = datetime.timedelta(days=self.options.days)
                        startdate = datetime.date.today() - a_day
                        query = query.get().filter(Message.timestamp >
                                str(startdate))
                        data = query[:10]
                    else:
                        data = query.get()[:10]
                if data:
                    sentry += 1
                    pdfcreator.add(data, reports[reportid]['title'],
                                pieheadings, sortby)
            query = Session.query(Message.date,
                                func.count(Message.date).label('mail_total'),
                                func.sum(case([(Message.virusinfected > 0, 1)],
                                else_=0)).label('virus_total'),
                                func.sum(case([(and_(Message.virusinfected ==
                                0, Message.spam > 0), 1)], else_=0))\
                                .label('spam_total'), func.sum(Message.size)\
                                .label('total_size')).group_by(Message.date)\
                                .order_by(desc(Message.date))
            uquery = UserFilter(Session, user, query)
            query = uquery.filter()
            data = query.all()
            if data:
                if not sentry:
                    sentry += 1
                pdfcreator.add(data, _('Message Totals'), totalsheaders,
                                chart='bar')
            if sentry:
                template = mako_lookup.get_template('/email/pdfreports.txt')
                text = template.render(user=user, url=host_url)
                displayname = '%s %s' % (user.firstname or '',
                                        user.lastname or '')
                email = Msg(author=[(_('Baruwa Reports'),
                                self.conf['baruwa.reports.sender'])],
                                to=[(displayname, user.email)],
                                subject=_('Baruwa usage report'))
                email.plain = text
                pdf_file = base64.b64encode(pdfcreator.build())
                email.attach('baruwa-reports.pdf',
                            data=pdf_file,
                            maintype='application/pdf',
                            subtype='application/x-pdf')
                try:
                    mailer.send(email)
                except (TransportFailedException, MessageFailedException), err:
                    print >> sys.stderr, ("Error sending to: %s, Error: %s" %
                            (user.email, err))