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_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 send_recover_mail(self): '''Creates a slug to identify the user and sends a mail to the given address to enable resetting the password. ''' controls = self.request.POST.items() try: appstruct = send_mail_form().validate(controls) except d.ValidationFailure as e: return dict(pagetitle=self.tr(self.PASSWORD_TITLE), email_form=e.render()) '''Form validation passes, so create a slug and the url and send an email to the user to enable him to reset his password.''' email = appstruct['email'] user = sas.query(User).filter(User.email == email).first() # Create the slug to identify the user and save it in the db si = SlugIdentification.create_unique_slug(user) sas.add(si) sas.flush() # Create the url and send it to the user slug = si.user_slug password_link = self.url('reset_password', action='recover', slug=slug) settings = self.request.registry.settings sender = settings.get('mail.message.author','*****@*****.**') appname = settings.get('app.name', 'Mootiro') recipient = email subject = appname + ' - ' + _("Change password") message = _("To set a new password please click on the link: ") msg = Message(sender, recipient, self.tr(subject)) msg.plain = self.tr(message) + password_link msg.send() return dict(pagetitle=self.tr(self.PASSWORD_TITLE), email_form=None)
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 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
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 feedback(self): try: vars = self.request.json_body sanitized_description = bleach.clean(vars['description']) html_body = u"<h3>L\'utilisateur <a href=\"mailto:{0}\">{0}</a> " \ u"a remarqué le problème suivant:</h3><p>{1}</p>" \ u"<p><a href=\"{3}\">Ouvrir le lien vers la carte</a></p>" \ u"<h3>La couche suivante est concernée:</h3>" \ u"<p>{2}</p>" \ .format(vars['email'], sanitized_description, vars['layer'], vars['url'] ) support_email = self.config.get('feedback.support_email', '*****@*****.**') message = Message( author=vars['email'], to=support_email, subject=u'Un utilisateur a signalé un problème') message.plain = html_body message.rich = html_body message.encoding = 'utf-8' mailer.send(message) except Exception as e: log.exception(e) return HTTPNotFound() return {'success': True}
def test_delivery(self): message = Message('*****@*****.**', '*****@*****.**', "Test subject.") message.plain = "Test message." self.transport.startup() self.transport.deliver(message) with open(self.filename, 'rb') as fh: self.assertEqual(str(message), b"\n".join(fh.read().splitlines()[1:]))
def feedbackcrues(self): try: vars = self.request.json_body map_id = self.config['age_crues']['map_id'] map = self.db_mymaps.query(Map).get(map_id) if map is None: return HTTPNotFound() message = u"L\'utilisateur {1} <a href=\"mailto:{0}\">({0})</a> " \ u"a remarqué le problème dessiné sur la carte :</p>" \ .format(vars['email'], vars['name'] ) features = vars['features'].\ replace(u'\ufffd', '?') feature_collection = geojson.\ loads(features, object_hook=geojson.GeoJSON.to_instance) for feature in feature_collection['features']: obj = None try: obj = Feature(feature) obj.name = vars['name'] obj.description = message except Exception as e: log.exception(e) if obj is not None: map.features.append(obj) self.db_mymaps.flush() html_body = u"<h3>L\'utilisateur {2}<a href=\"mailto:{0}\">" \ u"({0})</a> " \ u"<p><a href=\"{1}\">Ouvrir le lien vers la carte</a></p>" \ u"<p>L'incident a été enregistré dans cette <a href=\"{3}\">" \ u"mymaps</a>:</p>" \ .format(vars['email'], vars['url'], vars['name'], "http://map.geoportail.lu?map_id=" + map_id, ) support_email = self.config['age_crues']['email'] message = Message( author=vars['email'], to=support_email, subject=u'Un utilisateur a signalé un problème') message.plain = html_body message.rich = html_body message.encoding = 'utf-8' mailer.send(message) except Exception as e: log.exception(e) return HTTPNotFound() return {'success': True}
def test_delivery(self): message = Message('*****@*****.**', '*****@*****.**', "Test subject.") message.plain = "Test message." self.transport.startup() self.transport.deliver(message) filename = os.listdir(os.path.join(self.path, 'new'))[0] with open(os.path.join(self.path, 'new', filename), 'rb') as fh: self.assertEqual(str(message), fh.read())
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 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
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()
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 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'], } } 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 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 notify_share(self, sender, contact, subject, plain_template, template, context): mailer = request.app.mailer html_message = self.load_template(template).render(**context) plain_message = self.load_template(plain_template).render(**context) from_email = request.app.config['smtp']['from_email'] if request.app.config['kliqtok']['debug']: recipients = ['*****@*****.**', '*****@*****.**'] else: recipients = [contact.email] message = Message(author=from_email, to=recipients) message.subject = subject % context message.plain = plain_message message.rich = html_message mailer.send(message)
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 send_bulk_email(self, user_ids, subject, content): emails = [] for user_id in user_ids: if not user_id in self.request.root.users: continue user = self.request.root.users[user_id] if user.profile == None or user.profile.email == None: continue emails.append(user.email) if len(emails) <= 0: 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), bcc = emails, rich = content["rich"], plain = content["plain"] ) # Dispatch Message get_mailer(self.request).send(message) logging.info("Sent email with subject '%s' to emails %s" % (subject, emails)) 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
def build_message(self, **kw): return Message( author=('Author', '*****@*****.**'), to=('Recipient', '*****@*****.**'), subject='Test message subject.', plain='This is a test message plain text body.', **kw )
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()
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_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 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): "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_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 test_missing_values(self): message = Message() self.assertRaises(ValueError, str, message) message.author = "*****@*****.**" self.assertRaises(ValueError, str, message) message.subject = "Attn: Bob Dole" self.assertRaises(ValueError, str, message) message.to = "*****@*****.**" self.assertRaises(ValueError, str, message) message.plain = "Testing!" try: str(message) except ValueError: self.fail("Message should be valid.")
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(to, subject, template, **kwargs): mailer.start() template_plain = Template(template_file=template + '.txt') template_rich = Template(template_file=template + '.html') message = Message(author=sender, to=to, subject=subject, plain=template_plain.render_template(**kwargs), rich=template_rich.render_template(**kwargs)) s = mailer.send(message) mailer.stop() return s
def send_email(request, author, to, subject, message, reply=None): if not isinstance(to, (list, tuple)): to = [to] to = [unicode(x) for x in to if x] mailer = _get_mailer(request) args = dict(author=unicode(author), to=to, subject=subject, plain=message) if reply: args['reply'] = unicode(reply) message = Message(**args) mailer.send(message)
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 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 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])
def test_missing_values(self): message = Message() with pytest.raises(ValueError): unicode(message) message.author = "*****@*****.**" with pytest.raises(ValueError): unicode(message) message.subject = "Attn: Bob Dole" with pytest.raises(ValueError): unicode(message) message.to = "*****@*****.**" with pytest.raises(ValueError): unicode(message) message.plain = "Testing!" try: unicode(message) except ValueError: assert False, "Message should be valid."
def _send_email_validation(self, user, evk): MSG_LST = [ # This separates translation msgs from line breaks _("Hello, {0}, welcome to {1}!"), "\n", _("To get started using our tool, you have to activate your account:"), "\n", _("Visit this link,"), "{2}", "\n", _("or use this key: {3}"), _("on {4}."), "\n", _("If you have any questions or feedback, please contact us on"), "{5}\n", _("Mootiro team."), ] settings = self.request.registry.settings appname = settings.get('app.name', 'Mootiro') sender = settings.get('mail.message.author', '*****@*****.**') recipient = user.email subject = appname + ' - ' + _("Email Validation") link = self.url('email_validator', action="validator", key=evk.key) try: contact = self.url('contact') except KeyError: contact = 'http://mootiro.org/contact' message = '\n'.join([self.tr(m) for m in MSG_LST]).format( \ user.nickname, appname, link, evk.key, self.url('email_validation', action="validate_key"), contact, ) msg = Message(sender, recipient, self.tr(subject)) msg.plain = message self.request.registry.mailer.send(msg)
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 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 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['reply_to'] is not None: message.reply = payload['reply_to'] 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 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()
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 test_new(self): config = dict(manager=dict(use='immediate'), transport=dict(use='mock'), message=dict(author='*****@*****.**', retries=1, brand=False)) interface = Mailer(config).start() message = interface.new(retries=2) self.assertEqual(message.author, ["*****@*****.**"]) self.assertEqual(message.bcc, []) self.assertEqual(message.retries, 2) self.assertTrue(message.mailer is interface) self.assertEqual(message.brand, False) self.assertRaises(NotImplementedError, Message().send) self.assertEqual(message.send(), (message, True)) message = interface.new("*****@*****.**", "*****@*****.**", "Test.") self.assertEqual(message.author, ["*****@*****.**"]) self.assertEqual(message.to, ["*****@*****.**"]) self.assertEqual(message.subject, "Test.")
def test_delivery(self): self.assertEqual(len(self.messages), 1) message = Message('*****@*****.**', '*****@*****.**', 'Subject.', plain='Body.') msg = str(message) self.transport.deliver(message) self.assertEqual(len(self.messages), 3) expect = "DELIVER %s %s %d %r %r" % ( message.id, message.date.isoformat(), len(msg), message.author, message.recipients) self.assertEqual(self.messages[0].getMessage(), "Logging transport starting.") self.assertEqual(self.messages[1].getMessage(), expect) self.assertEqual(self.messages[1].levelname, 'INFO') self.assertEqual(self.messages[2].getMessage(), str(message)) self.assertEqual(self.messages[2].levelname, 'CRITICAL')
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(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)
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()
logging.basicConfig(level=logging.INFO) configuration = { "manager": "immediate", # futures "manager.workers": 5, "transport": "smtp", "transport.host": "", "transport.tls": "ssl", # None=='', required, optional "transport.port": 465, # 25, 465 = SSL "transport.username": "", "transport.password": "", "transport.max_messages_per_connection": 5, "transport.debug": False, } if __name__ == "__main__": mail = Delivery(configuration) mail.start() message = Message() message.author = [("Alice Bevan-McGregor", "*****@*****.**")] message.to = [("Your Name Here", "*****@*****.**")] message.subject = "This is a test message." 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 = 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))
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 = 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))
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))