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()
def send_mail_via_smtp_(config, payload): """ Send email via SMTP :param config: :param payload: :return: """ 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['html']) message.rich = payload['html'] mailer.send(message) mailer.stop()
def send_report_mail(plain_text, settings): """Send anomalies report by email Arguments: plain_text {string} -- Report body in plain text settings (dict) -- Settings from the class Config.Smtp Returns: """ mailer = Mailer({ 'manager.use': 'futures', 'transport.use': 'smtp', 'transport.host': settings['host'], 'transport.tls': settings['ssl'], 'transport.debug': False, 'transport.username': settings['username'], 'transport.password': settings['password'], 'transport.max_messages_per_connection': 5 }) mailer.start() message = Message(author=settings['mailfrom'], to=settings['mailto']) message.subject = settings['subject'] message.plain = plain_text mailer.send(message) mailer.stop() return True
def test_send(self): message = Bunch(id='foo') interface = Mailer(base_config) with pytest.raises(MailerNotRunning): interface.send(message) interface.start() #logging.getLogger().handlers[0].truncate() #messages = logging.getLogger().handlers[0].buffer assert interface.send(message) == (message, True) #assert messages[0] == "Attempting delivery of message foo." #assert messages[-1] == "Message foo delivered." message_fail = Bunch(id='bar', die=True) with pytest.raises(Exception): interface.send(message_fail) #assert messages[-4] == "Attempting delivery of message bar." #assert messages[-3] == "Acquired existing transport instance." #assert messages[-2] == "Shutting down transport due to unhandled exception." #assert messages[-1] == "Delivery of message bar failed." interface.stop()
def test_send(self): message = Bunch(id="foo") interface = Mailer(base_config) with pytest.raises(MailerNotRunning): interface.send(message) interface.start() # logging.getLogger().handlers[0].truncate() # messages = logging.getLogger().handlers[0].buffer assert interface.send(message) == (message, True) # assert messages[0] == "Attempting delivery of message foo." # assert messages[-1] == "Message foo delivered." message_fail = Bunch(id="bar", die=True) with pytest.raises(Exception): interface.send(message_fail) # assert messages[-4] == "Attempting delivery of message bar." # assert messages[-3] == "Acquired existing transport instance." # assert messages[-2] == "Shutting down transport due to unhandled exception." # assert messages[-1] == "Delivery of message bar failed." interface.stop()
class SendEmail(object): def __init__(self, username='', password='', use='smtp', host='smtp.exmail.qq.com', port='25'): self.username = username self.mailer = Mailer({ 'transport': { 'use': use, 'host': host, 'port': port, 'username': username, 'password': password }, 'manager': {} }) def send_email(self, to, subject, content, author=''): self.mailer.start() self.mailer.send( Message(author=author or self.username, to=to, subject=subject, plain=content)) self.mailer.stop()
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)
def passwdreset(self): """Render password reset page""" c.came_from = '/' c.login_counter = 0 c.form = ResetPwForm(request.POST, csrf_context=session) if request.method == 'POST' and c.form.validate(): key_seed = '%s%s' % (c.form.email.data, arrow.utcnow().ctime()) token = hashlib.sha1(key_seed).hexdigest() user = Session.query(User)\ .filter(User.email == c.form.email.data)\ .one() if not user.local: flash( _('The account %s is an external account, use your' ' External systems to change the password. ' 'Contact your system adminstrator if you do not ' 'know which external systems you authenticate to') % user.email) redirect(url('/accounts/login')) rtoken = Session\ .query(ResetToken.used)\ .filter(ResetToken.used == false())\ .filter(ResetToken.user_id == user.id)\ .all() if not rtoken: rtoken = ResetToken(token, user.id) Session.add(rtoken) Session.commit() host = URL_PREFIX_RE.sub('', request.host_url) c.username = user.username c.firstname = user.firstname or user.username c.reset_url = url('accounts-pw-token-reset', token=token, host=host) text = self.render('/email/pwreset.txt') mailer = Mailer(get_conf_options(config)) mailer.start() sdrnme = config.get('baruwa.custom.name', 'Baruwa') email = Msg(author=[(sdrnme, config.get('baruwa.reports.sender'))], to=[('', c.form.email.data)], subject=_("[%s] Password reset request") % sdrnme) email.plain = text mailer.send(email) mailer.stop() flash( _('An email has been sent to the address provided, ' 'please follow the instructions in that email to ' 'reset your password.')) redirect(url('/accounts/login')) return self.render('/accounts/login.html')
def send(subject, text, html, sender, recipient, cc=None, bcc=None): mailer = Mailer(dict(transport=dict(use='smtp', host='localhost'))) mailer.start() message = Message(author=sender, to=recipient, cc=cc, bcc=bcc) message.subject = subject message.rich = html message.plain = text mailer.send(message) mailer.stop()
class SendEmail(object): def __init__(self, username="", password="", use="smtp", host="smtp.exmail.qq.com", port="25"): self.username = username self.mailer = Mailer( { "transport": {"use": use, "host": host, "port": port, "username": username, "password": password}, "manager": {}, } ) def send_email(self, to, subject, content, author=""): self.mailer.start() self.mailer.send(Message(author=author or self.username, to=to, subject=subject, plain=content)) self.mailer.stop()
def send_local_mail(user: str): mailer = Mailer(dict(transport=dict(use="sendmail", host="localhost"))) mailer.start() otp = generate_otp_for_user(user) print(otp) message = Message(author="*****@*****.**", to=user) message.sendmail_f = "*****@*****.**" message.subject = "Login for Twit" message.plain = f"Your OTP is {otp}" mailer.send(message) mailer.stop()
def passwdreset(self): """Render password reset page""" c.came_from = '/' c.login_counter = 0 c.form = ResetPwForm(request.POST, csrf_context=session) if request.method == 'POST' and c.form.validate(): key_seed = '%s%s' % (c.form.email.data, arrow.utcnow().ctime()) token = hashlib.sha1(key_seed).hexdigest() user = Session.query(User)\ .filter(User.email == c.form.email.data)\ .one() if not user.local: flash(_('The account %s is an external account, use your' ' External systems to change the password. ' 'Contact your system adminstrator if you do not ' 'know which external systems you authenticate to') % user.email) redirect(url('/accounts/login')) rtoken = Session\ .query(ResetToken.used)\ .filter(ResetToken.used == false())\ .filter(ResetToken.user_id == user.id)\ .all() if not rtoken: rtoken = ResetToken(token, user.id) Session.add(rtoken) Session.commit() host = URL_PREFIX_RE.sub('', request.host_url) c.username = user.username c.firstname = user.firstname or user.username c.reset_url = url('accounts-pw-token-reset', token=token, host=host) text = self.render('/email/pwreset.txt') mailer = Mailer(get_conf_options(config)) mailer.start() sdrnme = config.get('baruwa.custom.name', 'Baruwa') email = Msg(author=[(sdrnme, config.get('baruwa.reports.sender'))], to=[('', c.form.email.data)], subject=_("[%s] Password reset request") % sdrnme) email.plain = text mailer.send(email) mailer.stop() flash(_('An email has been sent to the address provided, ' 'please follow the instructions in that email to ' 'reset your password.')) redirect(url('/accounts/login')) return self.render('/accounts/login.html')
def command(self): "command" self.init() if self.options.email is None: print "\nA valid email is required\n" print self.parser.print_help() sys.exit(2) starttime = arrow.utcnow() if self.options.report_period == 'daily': endtime = starttime - datetime.timedelta(days=1) elif self.options.report_period == 'weekly': endtime = starttime - datetime.timedelta(weeks=1) else: endtime = starttime - datetime.timedelta(weeks=4) params = dict(spamscore=self.options.spamscore, num=self.options.num, starttime=starttime.datetime, endtime=endtime.datetime) sql = text("""SELECT clientip, COUNT(clientip) a FROM messages WHERE sascore <= :spamscore AND (timestamp BETWEEN :endtime AND :starttime) GROUP BY clientip HAVING COUNT(clientip) >= :num ORDER BY a DESC;""") results = Session.execute(sql, params=params) if results.rowcount: if self.options.include_count is False: records = [result.clientip for result in results] else: records = ["%s\t%d" % tuple(result) for result in results] content = "\n".join(records) if self.options.dry_run is True: print content else: mailer = Mailer(get_conf_options(self.conf)) mailer.start() email = Msg(author=self.conf['baruwa.reports.sender'], to=self.options.email, subject='TWL') email.plain = content try: mailer.send(email) except (TransportFailedException, MessageFailedException), err: print >> sys.stderr, err mailer.stop()
def test_send(self): message = Bunch(id='foo') interface = Mailer(base_config) self.assertRaises(MailerNotRunning, interface.send, message) interface.start() logging.getLogger().handlers[0].truncate() messages = logging.getLogger().handlers[0].buffer self.assertEqual(interface.send(message), (message, True)) self.assertEqual(messages[0].getMessage(), "Attempting delivery of message foo.") self.assertEqual(messages[-1].getMessage(), "Message foo delivered.") message_fail = Bunch(id='bar', die=True) self.assertRaises(Exception, interface.send, message_fail) self.assertEqual(messages[-4].getMessage(), "Attempting delivery of message bar.") self.assertEqual(messages[-3].getMessage(), "Acquired existing transport instance.") self.assertEqual(messages[-2].getMessage(), "Shutting down transport due to unhandled exception.") self.assertEqual(messages[-1].getMessage(), "Delivery of message bar failed.") interface.stop()
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()
class MailingAdapter(object): def __init__(self, host, port): self.mailer = Mailer(dict( manager = dict( use = 'immediate'), transport = dict( use = 'smtp', port = port, host = host))) self.mailer.start() def send_mail(self, to_address, from_address, body): message = Message(author=from_address, to=to_address) message.subject = "Testing Marrow Mailer" message.plain = body self.mailer.send(message)
def send_spam_alert(number, calls_today, calls_per_minute, concurrent_calls): if not config.getboolean('email', 'enabled'): return mailer = Mailer({'transport.use': 'sendmail'}) message = Message(author=config.get('mail', 'from'), to=config.get('mail', 'to')) message.subject = config.get('mail', 'subject') message.plain = EMAIL_MESSAGE.format(number=number, calls_today=calls_today, calls_per_minute=calls_per_minute, concurrent_calls=concurrent_calls) mailer.start() mailer.send(message) mailer.stop()
def command(self): "command" self.init() if self.options.email is None: print "\nA valid email is required\n" print self.parser.print_help() sys.exit(2) starttime = arrow.utcnow().datetime if self.options.report_period == 'daily': endtime = starttime - datetime.timedelta(days=1) elif self.options.report_period == 'weekly': endtime = starttime - datetime.timedelta(weeks=1) else: endtime = starttime - datetime.timedelta(weeks=4) params = dict(spamscore=self.options.spamscore, num=self.options.num, starttime=starttime, endtime=endtime) sql = text("""SELECT clientip, COUNT(clientip) a FROM messages WHERE sascore >= :spamscore AND (timestamp BETWEEN :endtime AND :starttime) GROUP BY clientip HAVING COUNT(clientip) >= :num ORDER BY a DESC;""") results = Session.execute(sql, params=params) if results.rowcount: if self.options.include_count is False: records = [result.clientip for result in results] else: records = ["%s\t%d" % tuple(result) for result in results] content = "\n".join(records) if self.options.dry_run is True: print content else: mailer = Mailer(get_conf_options(self.conf)) mailer.start() email = Msg(author=self.conf['baruwa.reports.sender'], to=self.options.email, subject='TSL') email.plain = content try: mailer.send(email) except (TransportFailedException, MessageFailedException), err: print >> sys.stderr, err mailer.stop()
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()
class Mailer(object): def __init__(self): self.config = Config().config['mail'] self.mailer = MarrowMailer( dict( transport=dict(debug=True, **self.config), manager=dict(), ), ) def send(self, message): self.mailer.start() self.mailer.send(message) self.mailer.stop() pass def send_activation_message_to_mentor(self, mentor): message = ActivationMessage(mentor, Config().config['secret_key']) self.send(message)
def send_email(to, subject, body): host = config.get('email', 'host') author = config.get('email', 'from') username = config.get('email', 'username') password = _get_email_password(username) mailer = Mailer({'manager': {'use': 'immediate'}, 'transport': {'use': 'smtp', 'host': host, 'local_hostname': '[192.168.32.254]', 'username': username, 'password': password, 'tls': 'optional'} }) message = Message(author=author, to=to, subject=subject, plain=body) puts("Sending email to {}".format(to)) mailer.start() mailer.send(message) mailer.stop()
def send_mail_via_smtp_task(config, payload): 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['html']) message.rich = payload['html'] mailer.send(message) logging.info('Message sent via SMTP') mailer.stop()
def send_email(cls, email_contents): """Sends an email via SMTP. :param dict email_contents: from_email - Email address to send FROM to_email - Email address to send TO subject - Subject of email plain_message - Plaintext messgae rich_message - Rich/HTML message """ mailer = Mailer({ 'manager.use': 'immediate', 'transport.use': 'smtp', 'transport.host': config.get('secrets.smtp.host'), 'transport.port': config.get('secrets.smtp.port'), 'transport.username': config.get('secrets.smtp.username'), 'transport.password': config.get('secrets.smtp.password'), 'transport.timeout': 10, }) mailer.start() message = Message( author=email_contents['from_email'], to=email_contents['to_email'], subject=email_contents['subject'], plain=email_contents.get('plain_message') or '-- message not available --', rich=email_contents.get('rich_message'), ) mailer.send(message) mailer.stop()
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()
def pwtokenreset(self, token): """Reset password using token""" try: token = Session.query(ResetToken)\ .filter(ResetToken.token == token)\ .filter(ResetToken.used == false()).one() threshold = token.timestamp + timedelta(minutes=20) if arrow.utcnow().datetime > threshold: Session.delete(token) Session.commit() raise NoResultFound user = self._get_user(token.user_id) if not user or user.is_superadmin: raise NoResultFound passwd = mkpasswd() user.set_password(passwd) Session.add(user) Session.delete(token) Session.commit() c.passwd = passwd c.firstname = user.firstname or user.username text = self.render('/email/pwchanged.txt') mailer = Mailer(get_conf_options(config)) mailer.start() sdrnme = config.get('baruwa.custom.name', 'Baruwa') email = Msg(author=[(sdrnme, config.get('baruwa.reports.sender'))], to=[('', user.email)], subject=_("[%s] Password reset") % sdrnme) email.plain = text mailer.send(email) mailer.stop() flash( _('The password has been reset, check your email for' ' the temporary password you should use to login.')) except NoResultFound: msg = _('The token used is invalid or does not exist') flash_alert(msg) log.info(msg) redirect(url('/accounts/login'))
def main(): mailer = Mailer( dict(transport=dict(use='smtp', host='smtp.163.com', port=25, username='******', password='******', timeout=30, debug=False))) mailer.start() message = mailer.new(author="*****@*****.**", to=["*****@*****.**", "*****@*****.**"]) message.attach(name="/Users/kute/work/logs/a.txt") # 附件 message.subject = "Testing Marrow Mailer" # message.cc = AddressList(addresses=["*****@*****.**", "*****@*****.**"]) # 抄送-1 message.cc = "[email protected],[email protected]" # 抄送-2 message.plain = "hi,你好,请问什么时候去吃饭啊,快饿死了" # 普通文本内容 message.rich = "<h1>为什么不回我</h1>" # html文本 mailer.send(message) mailer.stop()
def send_mail(subject, plain, html): """This function assumes that email is HTML formatted""" mailer = Mailer(dict( transport = dict( use = 'smtp', debug = config.EMAIL_DEBUG, host = config.EMAIL_HOST, port = config.EMAIL_PORT, username = config.EMAIL_FROM, password = config.EMAIL_PASS, tls = config.EMAIL_SSL), manager = dict()) ) mailer.start() message = mailer.new() message.subject = subject message.author = config.EMAIL_FROM message.to = config.EMAIL_TO message.plain = plain message.rich = html mailer.send(message) mailer.stop()
def pwtokenreset(self, token): """Reset password using token""" try: token = Session.query(ResetToken)\ .filter(ResetToken.token == token)\ .filter(ResetToken.used == false()).one() threshold = token.timestamp + timedelta(minutes=20) if arrow.utcnow().datetime > threshold: Session.delete(token) Session.commit() raise NoResultFound user = self._get_user(token.user_id) if not user or user.is_superadmin: raise NoResultFound passwd = mkpasswd() user.set_password(passwd) Session.add(user) Session.delete(token) Session.commit() c.passwd = passwd c.firstname = user.firstname or user.username text = self.render('/email/pwchanged.txt') mailer = Mailer(get_conf_options(config)) mailer.start() sdrnme = config.get('baruwa.custom.name', 'Baruwa') email = Msg(author=[(sdrnme, config.get('baruwa.reports.sender'))], to=[('', user.email)], subject=_("[%s] Password reset") % sdrnme) email.plain = text mailer.send(email) mailer.stop() flash(_('The password has been reset, check your email for' ' the temporary password you should use to login.')) except NoResultFound: msg = _('The token used is invalid or does not exist') flash_alert(msg) log.info(msg) redirect(url('/accounts/login'))
class SendEmail(object): def __init__(self): self.mailer = Mailer(dict( transport=dict( use='smtp', host='smtp.gmail.com', port='587', username=username, password=password, tls='required', debug=False), manager=dict())) self.mailer.start() def send(self, to, html_body, plain_body): message = Message(author=from_email, to=to) message.subject = subject message.plain = plain_body message.rich = html_body self.mailer.send(message) def stop_sending(self): self.mailer.stop() def construct_html_email(self): user_list = get_users_to_send_data() data = get_data_to_send() #create html #f = open("templates/email.html", "r") #text = f.read() #t = Template(unicode(text, errors='ignore')) #text = t.substitute(links=result_map["html"] + "</ol>") #result_map["html"] = text for user in user_list: self.send(to=user["email"], html_body="%s" % data, plain_body="data_plain")
def send(rst, html, email, name, cc): from marrow.mailer import Mailer, Message mailer = Mailer( dict( transport = dict( use = 'smtp', host = 'localhost'))) mailer.start() message = Message( author="*****@*****.**", to=email, cc=cc, bcc='*****@*****.**' ) message.subject = "Karatbars replicated website for {0}".format(name) message.rich = html message.plain = rst mailer.send(message) mailer.stop()
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))
class SendPdfReports(BaseCommand): "Create an admin user account" BaseCommand.parser.add_option('-t', '--report-type', help='Report type [user, domain]', dest='report_type', type='str', default='user', action='callback', callback=check_report_type,) BaseCommand.parser.add_option('-p', '--report-period', help='Report period [daily, weekly, monthly]', dest='report_period', type='str', default='daily', action='callback', callback=check_period,) BaseCommand.parser.add_option('-d', '--number-of-days', help='Restrict to number of days', dest='number_of_days', type='int', default=0,) summary = """Send summary PDF reports""" group_name = 'baruwa' def command(self): "run command" self.init() self.language = 'en' self.host_url = self.conf['baruwa.default.url'] self.send_from = self.conf['baruwa.reports.sender'] self.num_of_days = self.options.number_of_days base = workout_path() path = os.path.join(base, 'baruwa', 'templates') self.logo = os.path.join(base, 'baruwa', 'public', 'imgs', 'logo.png') self.localedir = os.path.join(base, 'baruwa', 'i18n') cache_dir = os.path.join(self.conf['cache_dir'], 'templates') self.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'] ) self.mailer = Mailer(get_conf_options(self.conf)) self.mailer.start() if self.options.report_type == 'user': users = get_users() for user in users: self._process_user_report(user) else: period = REPORTS_MAP[self.options.report_period] domains = Session.query(Domain).filter(Domain.status == True)\ .filter(Domain.report_every == period).all() for domain in domains: admins = set() self.translator = set_lang(domain.language, PKGNAME, self.localedir) for org in domain.organizations: for admin in org.admins: admins.add(admin) pdf_file = self._process_domain_report(domain) for admin in admins: self._send_domain_report(pdf_file, domain.site_url, admin) self.mailer.stop() 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))
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()
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))
import logging from marrow.mailer import Message, Mailer logging.basicConfig(level=logging.INFO) mail = Mailer({'manager.use': 'immediate', 'transport.use': 'maildir', 'transport.directory': 'data/maildir'}) mail.start() message = Message([('Alice Bevan-McGregor', '*****@*****.**')], [('Alice Two', '*****@*****.**')], "This is a test message.", plain="Testing!") mail.send(message) mail.stop()
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))
class MarrjaMailer(object): """ Main wrapper around marrow.mailer and jinja2. This should be run once in the global scope as it inits marrow and Jinja """ def __init__(self, pkg_name, config=None, server='localhost', username=None, password=None, email_port=25, default_sender=None, template_dir='email_templates'): """ Can be created by passing the configuration for sending a mail, pkg_name is required so we can find your email templates but depending on your configuration server, username, password may not be required. Only server, username, password, port, sender and template_dir can be configured. If you need to change other settings such as logging. Please pass a Config object. """ if not config is None: self._config = config else: self._config = Config() if not server is None: self._config.EMAIL_HOST = server if not username is None: self._config.EMAIL_USERNAME = username if not password is None: self._config.EMAIL_PWD = password if not email_port is None: self._config.EMAIL_PORT = email_port if not default_sender is None: self._config.EMAIL_SENDER = default_sender self._config.EMAIL_TEMPLATE_DIR = template_dir #Init log self._log = logging.getLogger(self._config.LOGGER_NAME) self._log.setLevel(self._config.LOGGER_LEVEL) console_handler = logging.StreamHandler() self._log.addHandler(console_handler) #Init Jinja self._jinja_env = Environment(loader=PackageLoader(pkg_name, self._config.EMAIL_TEMPLATE_DIR)) #Init Mailer self._mailer = Mailer(dict( transport = dict( use = 'smtp', host = self._config.EMAIL_HOST, username = self._config.EMAIL_USERNAME, password = self._config.EMAIL_PWD, port = self._config.EMAIL_PORT), manager = dict())) self._mailer.start() def send_email(self, send_to, template, subject, **kwargs): """ Sends an email to the target email with two types 1) HTML 2) Plain We will try the template with .htm 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 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 + '.htm') message.rich = rendered_template.render(**kwargs) self._log.debug('html email generated %s' % message.rich) except TemplateNotFound: self._log.debug('html template not found') self._mailer.send(message)
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()
class PyMassMailer(object): """ Main wrapper around marrow.mailer and jinja2. This should be run once in the global scope as it inits marrow and Jinja """ def __init__(self, pkg_name, config=None, server='localhost', username=None, password=None, email_port=25, default_sender=None, template_dir='email_templates'): """ Can be created by passing the configuration for sending a mail, pkg_name is required so we can find your email templates but depending on your configuration server, username, password may not be required. Only server, username, password, port, sender and template_dir can be configured. If you need to change other settings such as logging. Please pass a Config object. """ if not config is None: self._config = config else: self._config = Config() if not server is None: self._config.EMAIL_HOST = server if not username is None: self._config.EMAIL_USERNAME = username if not password is None: self._config.EMAIL_PWD = password if not email_port is None: self._config.EMAIL_PORT = email_port if not default_sender is None: self._config.EMAIL_SENDER = default_sender self._config.EMAIL_TEMPLATE_DIR = template_dir # Init log self._log = logging.getLogger(self._config.LOGGER_NAME) self._log.setLevel(self._config.LOGGER_LEVEL) console_handler = logging.StreamHandler() self._log.addHandler(console_handler) # Init Jinja self._jinja_env = Environment(loader=PackageLoader(pkg_name, self._config.EMAIL_TEMPLATE_DIR)) # Init Mailer self._mailer = Mailer(dict( transport=dict( use='smtp', host=self._config.EMAIL_HOST, username=self._config.EMAIL_USERNAME, password=self._config.EMAIL_PWD, port=self._config.EMAIL_PORT), manager=dict())) self._mailer.start() def asbase64(msg): return string.replace(base64.encodestring(msg), '\n', '') def connect_to_exchange_as_current_user(self, smtp): # Send the SMTP EHLO command code, response = smtp.ehlo() if code != self._config.SMTP_EHLO_OKAY: raise SMTPException( "Server did not respond as expected to EHLO command") sspiclient = sspi.ClientAuth('NTLM') # Generate the NTLM Type 1 message sec_buffer = None err, sec_buffer = sspiclient.authorize(sec_buffer) ntlm_message = self.asbase64(sec_buffer[0].Buffer) # Send the NTLM Type 1 message -- Authentication Request code, response = smtp.docmd("AUTH", "NTLM " + ntlm_message) # Verify the NTLM Type 2 response -- Challenge Message if code != self._config.SMTP_AUTH_CHALLENGE: raise SMTPException( "Server did not respond as expected to NTLM negotiate message") # Generate the NTLM Type 3 message err, sec_buffer = sspiclient.authorize(base64.decodestring(response)) ntlm_message = self.asbase64(sec_buffer[0].Buffer) # Send the NTLM Type 3 message -- Response Message #code, response = smtp.docmd("", ntlm_message) code, response = smtp.docmd(ntlm_message) if code != self._config.SMTP_AUTH_OKAY: raise SMTPAuthenticationError(code, response) 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)
class PyMassMailer(object): """ Main wrapper around marrow.mailer and jinja2. This should be run once in the global scope as it inits marrow and Jinja """ def __init__(self, pkg_name, config=None, server='localhost', username=None, password=None, email_port=25, default_sender=None, template_dir='email_templates'): """ Can be created by passing the configuration for sending a mail, pkg_name is required so we can find your email templates but depending on your configuration server, username, password may not be required. Only server, username, password, port, sender and template_dir can be configured. If you need to change other settings such as logging. Please pass a Config object. """ if not config is None: self._config = config else: self._config = Config() if not server is None: self._config.EMAIL_HOST = server if not username is None: self._config.EMAIL_USERNAME = username if not password is None: self._config.EMAIL_PWD = password if not email_port is None: self._config.EMAIL_PORT = email_port if not default_sender is None: self._config.EMAIL_SENDER = default_sender self._config.EMAIL_TEMPLATE_DIR = template_dir # Init log self._log = logging.getLogger(self._config.LOGGER_NAME) self._log.setLevel(self._config.LOGGER_LEVEL) console_handler = logging.StreamHandler() self._log.addHandler(console_handler) # Init Jinja self._jinja_env = Environment( loader=PackageLoader(pkg_name, self._config.EMAIL_TEMPLATE_DIR)) # Init Mailer self._mailer = Mailer( dict(transport=dict(use='smtp', host=self._config.EMAIL_HOST, username=self._config.EMAIL_USERNAME, password=self._config.EMAIL_PWD, port=self._config.EMAIL_PORT), manager=dict())) self._mailer.start() def asbase64(msg): return string.replace(base64.encodestring(msg), '\n', '') def connect_to_exchange_as_current_user(self, smtp): # Send the SMTP EHLO command code, response = smtp.ehlo() if code != self._config.SMTP_EHLO_OKAY: raise SMTPException( "Server did not respond as expected to EHLO command") sspiclient = sspi.ClientAuth('NTLM') # Generate the NTLM Type 1 message sec_buffer = None err, sec_buffer = sspiclient.authorize(sec_buffer) ntlm_message = self.asbase64(sec_buffer[0].Buffer) # Send the NTLM Type 1 message -- Authentication Request code, response = smtp.docmd("AUTH", "NTLM " + ntlm_message) # Verify the NTLM Type 2 response -- Challenge Message if code != self._config.SMTP_AUTH_CHALLENGE: raise SMTPException( "Server did not respond as expected to NTLM negotiate message") # Generate the NTLM Type 3 message err, sec_buffer = sspiclient.authorize(base64.decodestring(response)) ntlm_message = self.asbase64(sec_buffer[0].Buffer) # Send the NTLM Type 3 message -- Response Message #code, response = smtp.docmd("", ntlm_message) code, response = smtp.docmd(ntlm_message) if code != self._config.SMTP_AUTH_OKAY: raise SMTPAuthenticationError(code, response) 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)
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()
from marrow.mailer import Mailer, Message mailer = Mailer(dict( transport=dict( use='smtp', host='smtp.mailgun.org', port=25, username='******', password='******', debug=True), manager=dict())) mailer.start() message = Message(author="*****@*****.**", to="*****@*****.**") message.subject = "Testing Marrow Mailer" message.plain = "This is a test." mailer.send(message) mailer.stop()
import logging from marrow.mailer import Message, Mailer logging.basicConfig(level=logging.DEBUG) mail = Mailer({ 'manager.use': 'futures', 'transport.use': 'imap', 'transport.host': '', 'transport.ssl': True, 'transport.username': '', 'transport.password': '', 'transport.folder': 'Marrow' }) mail.start() message = Message([('Alice Bevan-McGregor', '*****@*****.**')], [('Alice Two', '*****@*****.**')], "This is a test message.", plain="Testing!") mail.send(message) mail.stop()
def buildnewsletter(book_list): """Routine to send an HTML newsletter """ logger.info('Pulling together the newsletter.') __tmp__file__loc = "tmpicon.jpg" mailer = Mailer( dict(transport=dict(use='smtp', host=config.settings['SMTPSettings']['host'], port=config.settings['SMTPSettings']['port'], username=config.settings['SMTPSettings']['user'], password=config.settings['SMTPSettings'] ['password'], tls=config.settings['SMTPSettings']['startttls']))) try: # Perform jinja message_template_file = config.settings['TEMPLATE_FILE'] message_banner_img = os.path.join( config.settings['TEMPLATE_DIR'], config.settings['TEMPLATE_BANNER_IMG']) message_unknown_img = os.path.join( config.settings['TEMPLATE_DIR'], config.settings['TEMPLATE_NOCOVER_IMG']) message_intropara_file = os.path.join( config.settings['TEMPLATE_DIR'], config.settings['TEMPLATE_INTROPARA']) cd = config.settings['TEMPLATE_DIR'] logger.info(cd) jinja_env = jinja2.Environment(loader=jinja2.FileSystemLoader(cd)) try: with open(message_intropara_file, 'r') as introparafile: message_intropara = introparafile.read().replace('\n', '') logger.info('Loaded newsletter intro paragraph.') except: message_intropara = "<p>New books added.</p>" logger.exception("Couldn't load intro paragraph.") logger.warn('Loading default newsletter intro paragraph.') messagebody = jinja_env.get_template(message_template_file).render( book_list=book_list, intropara_blk=message_intropara) mailer.start() message = Message(author=config.settings['SMTPSettings']['user']) message.subject = config.settings['SMTPSettings']['subject'] message.plain = "This is only exciting if you use an HTML capable email client. Please disregard." message.rich = messagebody message.embed(message_banner_img) flg_unknown_embedded = False for book in book_list: if book["book_cover_id"] != "Unknown.png": book['cover_thumbnail'].save(__tmp__file__loc, "JPEG") message.embed((book["book_cover_id"]), open(__tmp__file__loc)) elif book[ "book_cover_id"] == "Unknown.png" and not flg_unknown_embedded: message.embed(message_unknown_img) flg_unknown_embedded = True for winner in db_operations.get_dl_list(): message.to = winner if config.settings['DevMode']: mailer.send(message) logger.info('DevMode - Sending email to %s', winner) else: mailer.send(message) logger.info('sending email to %s', winner) except: logger.exception('Error sending email.') mailer.stop() logger.info('Completed newsletter routine.') return